#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t; cin >> t;
while (t--) {
ll n, k; cin >> n >> k;
ll a[n + 5];
for (ll i = 1; i <= n; i++) cin >> a[i];
ll mp[n + 5], ms[n + 5];
mp[0] = 1e18, mp[n + 1] = 1e18;
for (ll i = 1; i <= n; i++) {
mp[i] = min(mp[i - 1], a[i]);
}
ms[0] = 1e18; ms[n + 1] = 1e18;
for (ll i = n; i >= 1; i--) {
ms[i] = min(ms[i + 1], a[i]);
}
ll sum = 0;
ll mx = -1e18;
map<ll, ll>mppp; set<ll>se;
for (ll i = 1; i <= k; i++) {
se.insert(a[i]); mppp[a[i]]++;
sum += a[i];
mx = max(mx, a[i]);
} //
ll mini = sum; //cout << mini << '\n';
ll xxx = sum;
ll l = 1, r = k + 1; //mini = min(mini, sum - (ms[r] - mx)); r++; //cout << sum << '\n';
sum -= mx; sum += ms[r];
sum = xxx;
for (int i = k + 1; i <= n; i++) {
ll ult_min;
if (i == n) ult_min = mp[l];
else ult_min = min(mp[l], ms[r]);
if (a[i] < mx) {
mppp[a[l]]--;
//for (auto x: mppp) cout << x.first << ' ' << x.second << '\n'; cout << '\n';
if (mppp[a[l]] <= 0) se.erase(a[l]);
mppp[a[i]]++; se.insert(a[i]);
auto it = se.end(); --it;
mx = *it;
}else {
mx = a[i];
mppp[a[l]]--;
if (mppp[a[l]] <= 0) se.erase(a[l]);
se.insert(a[i]);
}
sum -= a[l];
sum += a[i]; xxx = sum;
mini = min(mini, sum);
sum -= mx;
sum += ult_min;
mini = min(mini, sum);
l++; r++; //break;
sum = xxx;
}
cout << mini << '\n';
}
}