#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MX = 2e5 + 5;
ll A[MX], n, k;
ll loog[MX + 5], sparse_table[32][MX], MinF[MX], MinB[MX];
void cal_log() {
loog[1] = 0;
for (ll i = 2; i <= MX; i++) loog[i] = loog[i / 2] + 1;
}
void create_sparse_table(ll cnt) {
for (ll i = 1; i <= cnt; i++) {
for (ll j = 0; (j + ((1 << i) - 1)) < n; j++) {
sparse_table[i][j] = max(sparse_table[i - 1][j], sparse_table[i - 1][j + (1 << (i - 1))]);
}
}
}
ll ret(ll x, ll y) {
ll a = loog[(y - x + 1)];
ll b = x + ((y - x + 1) - (1 << (a)));
ll val = max(sparse_table[a][x], sparse_table[a][b]);
return val;
}
void solve() {
cin >> n >> k;
for (ll i = 0; i < n; i++) {
cin >> A[i];
sparse_table[0][i] = A[i];
}
create_sparse_table(31);
ll res = LLONG_MAX;
MinF[0] = A[0];
MinB[n - 1] = A[n - 1];
for (ll i = 1, j = n - 2; i < n; i++, j--) {
MinF[i] = min(MinF[i - 1], A[i]);
MinB[j] = min(MinB[j + 1], A[j]);
}
ll cur = 0;
for (ll i = 0; i < k; i++)cur += A[i];
res = min(res, cur);
ll p = cur - ret(0, k - 1) + MinB[k];
res = min(res, p);
for (ll i = k, j = 0; i < n; i++, j++) {
cur += A[i];
cur -= A[j];
res = min(res, cur);
p = cur - ret(j + 1, i);
if (i + 1 < n)res = min(res, p + MinB[i + 1]);
res = min(res, p + MinF[j]);
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cal_log();
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}