#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define F first
#define S second
#define pii pair<int, int>
#define sz(x) (int) (x.size())
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T> using o_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int INF = 1e18 + 10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n, k; cin>>n>>k;
vector<int> v(n);
for(auto &x : v) cin>>x;
vector<int> pf(n + 1), sMx(n + 1, INF), pMx(n + 1, INF);
for(int i = 0; i < n; i++) {
pf[i + 1] = pf[i] + v[i];
pMx[i + 1] = min(pMx[i], v[i]);
}
for(int i = n - 1; i >= 0; i--) {
sMx[i] = min(sMx[i + 1], v[i]);
}
// for(auto it : pMx) cerr<<it<<" "; cerr<<endl;
// for(auto it : sMx) cerr<<it<<" "; cerr<<endl;
multiset<int> st;
int sm = 0;
for(int i = 0; i < k; i++) st.insert(v[i]), sm += v[i];
int curMx = *st.rbegin(), curMn = sMx[k];
int ans = sm;
ans = min(ans, sm - curMx + curMn);
for(int i = k; i < n; i++) {
// cerr<<"sm: "<<sm<<endl;
ans = min(ans, sm);
int curMx = *st.rbegin(), curMn = min(pMx[i - k], sMx[i]);
// cerr<<curMx<<" "<<curMn<<endl;
// cerr<<"sm: "<<sm - curMx + curMn<<endl;
ans = min(ans, sm - curMx + curMn);
sm -= v[i - k];
sm += v[i];
// cerr<<"sm: "<<sm<<endl;
st.erase(st.find(v[i - k]));
st.insert(v[i]);
}
ans = min(ans, sm);
curMx = *st.rbegin(), curMn = pMx[n - k];
ans = min(ans, sm - curMx + curMn);
cout<<ans<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1; cin>>t;
for(int tc = 1; tc <= t; tc++) {
// cerr<<"Case "<<tc<<":\n";
solve();
}
}