/*
Author : MishkatIT
Created : Tuesday 10-12-2024 15:38:30
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
const ll linf = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (auto& i : v) cin >> i;
map<int, int> cur, out;
ll sum = 0;
ll ans = linf;
for (auto& i : v) {
out[i]++;
}
for (int i = 0; i < n; i++) {
sum += v[i];
cur[v[i]]++;
out[v[i]]--;
if (out[v[i]] == 0) {
out.erase(v[i]);
}
if (i >= k - 1) {
// debug(i);
// debug(out);
ans = min(ans, sum);
if (out.size() > 0) {
sum -= cur.rbegin()->first;
sum += out.begin()->first;
ans = min(ans, sum);
sum += cur.rbegin()->first;
sum -= out.begin()->first;
}
out[v[i - k + 1]]++;
cur[v[i - k + 1]]--;
if (cur[v[i - k + 1]] == 0) {
cur.erase(v[i - k + 1]);
}
sum -= v[i - k + 1];
}
// debug(ans);
}
cout << ans << '\n';
}
return 0;
}