#include <bits/stdc++.h>
using namespace std;
long long solve(vector<int> &v, int k) {
int n = v.size();
vector<long long> p_sum(n);
vector<int> p_min(n), s_min(n);
p_sum[0] = p_min[0] = v[0];
for(int i = 1; i < n; i++) {
p_sum[i] = p_sum[i - 1] + v[i];
p_min[i] = min(p_min[i - 1], v[i]);
}
s_min[n - 1] = v[n - 1];
for(int i = n - 2; i >= 0; i--) {
s_min[i] = min(s_min[i + 1], v[i]);
}
deque<int> dq;
long long best = 0;
for(int i = 0; i < n; i++) {
while(dq.size() > 0 && v[dq.back()] <= v[i]) {
dq.pop_back();
}
dq.push_back(i);
if(i >= k - 1) {
int l = i - k + 1;
int r = i;
long long r_sum = p_sum[r] - (l > 0 ? p_sum[l - 1] : 0);
best = min(best, r_sum);
if(k != n) {
int r_max = v[dq.front()];
int other_min = min(l > 0 ? p_min[l - 1] : INT_MAX, r < n - 1 ? s_min[r + 1] : INT_MAX);
best = min(best, r_sum + other_min - r_max);
}
if(dq.front() == i - k + 1) {
dq.pop_front();
}
}
}
return best;
}
int main() {
int tc;
cin >> tc;
assert(tc >= 1 && tc <= 10000);
int total_n = 0;
while(tc--) {
int n, k;
cin >> n >> k;
total_n += n;
assert(n >= 1 && n <= 100000);
assert(k >= 1 && k <= n);
vector<int> v(n);
for(int i = 0; i < n; i++) {
cin >> v[i];
assert(v[i] >= -100000 && v[i] <= 100000);
}
cout << solve(v, k) << endl;
}
assert(total_n <= 200000);
}