#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define error1(x) cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v) for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;
void solve() {
ll n, k, cur = 0;
cin >> n >> k;
vector<ll> v(n);
multiset<ll> now, etc;
for (int i = 0; i < n; i++) {
ll x; cin >> x;
v[i] = x;
if(i < k) cur += x, now.insert(x);
else etc.insert(x);
}
if(n == k) {
cout << cur << endl;
return;
}
ll ans = cur;
if(*now.rbegin() > *etc.begin()) {
ans = min(ans, cur - *now.rbegin() + *etc.begin());
}
int l = 0, r = k;
while(r < n) {
now.erase(now.find(v[l])); cur -= v[l];
now.insert(v[r]); cur += v[r];
etc.erase(etc.find(v[r]));
etc.insert(v[l]);
ans = min({ans, cur, cur - *now.rbegin() + *etc.begin()});
++l, ++r;
}
return;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}