#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 Stree_mn { private:
vector<T> Tr; int n; function<T(T, T)> op;
public:
Stree_mn(const vector<T>& ar, function<T(T, T)> opp) {
n = ar.size(); op = opp; Tr.resize(2 * n);
for (int i = 0; i < n; i++) {
Tr[n + i] = ar[i];
} build();
}
void build() {
for (int i = n - 1; i > 0; --i) {
Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
}
}
T query(int l, int r) { l--, r--;
T r_left = INT_MAX, r_right = INT_MAX;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) r_left = op(r_left, Tr[l++]);
if (r & 1) r_right = op(Tr[--r], r_right);
}
return op(r_left, r_right);
}
};
template<class T> class Stree_mx { private:
vector<T> Tr; int n; function<T(T, T)> op;
public:
Stree_mx(const vector<T>& ar, function<T(T, T)> opp) {
n = ar.size(); op = opp; Tr.resize(2 * n);
for (int i = 0; i < n; i++) {
Tr[n + i] = ar[i];
} build();
}
void build() {
for (int i = n - 1; i > 0; --i) {
Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
}
}
T query(int l, int r) { l--, r--;
T r_left = INT_MIN, r_right = INT_MIN;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) r_left = op(r_left, Tr[l++]);
if (r & 1) r_right = op(Tr[--r], r_right);
}
return op(r_left, r_right);
}
};
template<class T> class Stree { private:
vector<T> Tr; int n; function<T(T, T)> op;
public:
Stree(const vector<T>& ar, function<T(T, T)> opp) {
n = ar.size(); op = opp; Tr.resize(2 * n);
for (int i = 0; i < n; i++) {
Tr[n + i] = ar[i];
} build();
}
void build() {
for (int i = n - 1; i > 0; --i) {
Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
}
}
T query(int l, int r) { l--, r--;
// assert(l <= r);
T r_left = 0, r_right = 0;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) r_left = op(r_left, Tr[l++]);
if (r & 1) r_right = op(Tr[--r], r_right);
}
return op(r_left, r_right);
}
};
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);
for(int i = 0; i < n; i++) {
cin >> ar[i];
}
Stree_mn<int64_t> smn(ar, [](int64_t a, int64_t b) {
return min(a, b);
});
Stree_mx<int64_t> smx(ar, [](int64_t a, int64_t b) {
return max(a, b);
});
Stree<int64_t> sum(ar, [](int64_t a, int64_t b) {
return a + b;
});
int64_t ans = LLONG_MAX;
for(int i = 1; i <= n - k + 1; i++) {
int64_t tm = sum.query(i, i + k - 1);
int64_t mn = LLONG_MAX;
if(i > 1) {
mn = min(mn, smn.query(1, i - 1));
}
if(i + k - 1 < n) {
mn = min(mn, smn.query(i + k, n));
}
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 _ ^ _;
}