/**
* author: Binoy Barman
* created: 2024-12-10 15:28:18
**/
#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Too_Many_Jobs int tts, tc = 1; cin >> tts; hell: while(tts--)
#define Dark_Lord_Binoy ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifdef LOCAL
#include "debug/whereisit.hpp"
#else
#define dbg(...) 42
#endif
#define int long long
const int N = 1e5 + 9, inf = 1e9;
int a[N];
struct SparseTable {
int t[N][18][2], logs[N];
SparseTable() {
memset(t, 0, sizeof t);
memset(logs, 0, sizeof logs);
for (int i = 2; i < N; i++) logs[i] = logs[i >> 1] + 1;
}
void build(int n) { // O(nlogn)
for(int i = 1; i <= n; i++) t[i][0][0] = t[i][0][1] = a[i];
for(int k = 1; k < 18; k++) {
for(int i = 1; i + (1 << k) - 1 <= n; i++) {
t[i][k][0] = min(t[i][k - 1][0], t[i + (1 << (k - 1))][k - 1][0]);
t[i][k][1] = max(t[i][k - 1][1], t[i + (1 << (k - 1))][k - 1][1]);
}
}
}
int minQuery(int l, int r) { // O(1)
// int k = 31 - __builtin_clz(r - l + 1);
if(l > r) return inf;
int k = logs[r - l + 1];
return min(t[l][k][0], t[r - (1 << k) + 1][k][0]);
}
int maxQuery(int l, int r) { // O(1)
if(l > r) return -inf;
int k = logs[r - l + 1];
return max(t[l][k][1], t[r - (1 << k) + 1][k][1]);
}
}st;
int32_t main() {
Dark_Lord_Binoy
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Too_Many_Jobs {
int n, k;
cin >> n >> k;
vector<int> ps(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
}
st.build(n);
int ans = inf;
for (int i = 1; i + k - 1 <= n; i++) {
int sum = ps[i + k - 1] - ps[i - 1];
int mn = st.minQuery(1, i - 1);
mn = min(mn, st.minQuery(i + k, n));
int mx = st.maxQuery(i, i + k - 1);
sum = min(sum, sum - mx + mn);
ans = min(sum, ans);
}
cout << ans << nl;
}
return 0;
}