#include <bits/stdc++.h>
using namespace std;
// #define ll long long
#define nl '\n'
#define f(i,s,n) for(int i = s; i < n; i++)
#define int long long
void solve(){
int n,k;
cin >> n >> k;
vector<int > arr(n);
multiset<int> ms, msMx;
f(i,0,n) {cin >> arr[i];ms.insert(arr[i]);}
int ptr1 = 0, ptr2 = 0, ans = 1e12, sm = 0;
auto upd = [&](){
ans = min(ans, sm);
sm -= *msMx.rbegin();
// cout << *msMx.rbegin() << nl;
if(ms.size() == 0)return;
sm += *ms.begin();
ans = min(ans, sm);
sm -= *ms.begin();
sm += *msMx.rbegin();
};
while(ptr2 < n){
while(ptr2 < n && ptr2 - ptr1 < k){
ms.erase(ms.find(arr[ptr2]));
sm += arr[ptr2];
msMx.insert(arr[ptr2]);
ptr2++;
}
if(ptr2 > n || ptr2 - ptr1 < k)break;
upd();
ms.insert(arr[ptr1]);
msMx.erase(msMx.find(arr[ptr1]));
sm -= arr[ptr1];
ptr1++;
}
cout<<ans<<nl;
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int T = 1;
cin>>T;
for(int tc = 1; tc <= T; tc++){
solve();
}
}