#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define pi 2*acos(0.0)
void solve()
{
ll n, k; cin >> n >> k;
ll a[n + 1]; a[0] = 0;
multiset<ll>out, in;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
out.insert(a[i]);
}
ll ans = LONG_LONG_MAX;
ll sum = 0;
for (ll i = 1; i <= n; i++) {
in.insert(a[i]);
out.erase(out.find(a[i]));
sum += a[i];
if (i >= k) {
if (i > k) {
in.erase(in.find(a[i - k]));
sum -= a[i - k];
out.insert(a[i - k]);
}
ll mx = *in.rbegin();
ll mn = *out.begin();
//cerr << sum << " " << mx << " " << mn << endl;
//cerr << mx << " " << mn << endl;
if (out.size()) {
ll sum2 = min(sum, sum - mx + mn);
ans = min(sum2, ans);
}
else {
ans = min(sum, ans);
}
}
}
cout << ans << endl;
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
ll t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}