/**
*
* author: Ayon Das Gupta
*
**/
#include <bits/stdc++.h>
#define ki(x) cout << x << '\n'
#define debug(v) for(auto &i : v) { cout << i << ' '; } cout << '\n';
#define debug2(v) for(auto &[x, y] : v) { cout << x << ' ' << y << '\n'; } cout << '\n';
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
vector<int> pref(n + 5, INT_MAX), suff(n + 5, INT_MAX);
for (int i = 1; i <= n; i++) {
pref[i] = min(pref[i - 1], v[i - 1]);
}
for (int i = n; i > 0; i--) {
suff[i] = min(suff[i + 1], v[i - 1]);
}
map<int, int> mp;
ll sum = 0;
for (int i = 0; i < k; i++) {
sum += v[i];
mp[v[i]]++;
}
ll ans = sum;
for (int i = k; i < n; i++) {
sum -= v[i - k];
mp[v[i - k]]--;
if (mp[v[i - k]] == 0) mp.erase(v[i - k]);
sum += v[i];
ans = min(ans, sum);
mp[v[i]]++;
int x = mp.rbegin()->first;
ans = min({ans, sum - x + pref[i - k + 1], sum - x + suff[i + 2]});
}
cout << ans << '\n';
}
return 0;
}