#include <bits/stdc++.h> // All praise is due to Allah alone, and peace and blessings be
using namespace std; // upon him, after whom there is no other Prophet.
template<class T> class SPT_mn { private: vector<vector<T>> mr; function<T(T, T)> ftn;
public: SPT_mn(const vector<T>& arr, function<T(T, T)> func = [](T a, T b) { return min(a, b); }){
int n = arr.size(), k = log2(n) + 1; ftn = func;
mr = vector<vector<T>>(n, vector<T>(k));
for (int i = 0; i < n; i++) mr[i][0] = arr[i];
for (int j = 1; j < k; j++) {
for (int i = 0; i + (1 << j) - 1 < n; i++) {
mr[i][j] = ftn(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
}
}
}
T query(int L, int R) {
int j = 31 - __builtin_clz(R - L + 1);
return ftn(mr[L][j], mr[R - (1 << j) + 1][j]);
}
};
template<class T> class SPT_mx { private: vector<vector<T>> mr; function<T(T, T)> ftn;
public: SPT_mx(const vector<T>& arr, function<T(T, T)> func = [](T a, T b) { return min(a, b); }){
int n = arr.size(), k = log2(n) + 1; ftn = func;
mr = vector<vector<T>>(n, vector<T>(k));
for (int i = 0; i < n; i++) mr[i][0] = arr[i];
for (int j = 1; j < k; j++) {
for (int i = 0; i + (1 << j) - 1 < n; i++) {
mr[i][j] = ftn(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
}
}
}
T query(int L, int R) {
int j = 31 - __builtin_clz(R - L + 1);
return ftn(mr[L][j], mr[R - (1 << j) + 1][j]);
}
};
int32_t main() {
cin.tie(0)->sync_with_stdio(false);
function<void()> M_test_case = [&]() {
int n, k; cin >> n >> k;
vector<int64_t> ar(n), br(n);
for(int i = 0; i < n; i++) {
cin >> ar[i];
}
SPT_mn<int64_t> smn(ar, [](int64_t a, int64_t b) {
return min(a, b);
});
SPT_mx<int64_t> smx(ar, [](int64_t a, int64_t b) {
return max(a, b);
});
partial_sum(ar.begin(), ar.end(), br.begin());
int64_t ans = LLONG_MAX;
for(int i = 0; i < n - k + 1; i++) {
int64_t tm = br[i + k - 1];
int64_t mn = LLONG_MAX;
if(i) {
tm -= br[i - 1];
mn = min(mn, smn.query(0, i - 1));
}
if(i + k < n) {
mn = min(mn, smn.query(i + k, n - 1));
}
int64_t mx = smx.query(i, i + k - 1);
if(mx > mn) {
tm = tm - mx + mn;
}
ans = min(ans, tm);
}
cout << ans << '\n';
};
int32_t _ = 1; cin >> _;
for (int T = 1; T <= _; T++) {
M_test_case();
}
return _ ^ _;
}