/*
Author : MishkatIT
Created : Tuesday 10-12-2024 20:27:47
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
const ll linf = 1e18;
class SegtreeMax {
public:
int n;
vector<int> Tree;
SegtreeMax(vector<int>& v) {
n = v.size();
Tree.resize(4 * n);
build(v, 1, 0, n - 1);
}
#define lc (node << 1)
#define rc ((node << 1) | 1)
#define mid ((s + e) >> 1)
void build(vector<int>& v, int node, int s, int e) {
if (s == e) {
Tree[node] = v[s];
return;
}
build(v, lc, s, mid);
build(v, rc, mid + 1, e);
Tree[node] = merge(Tree[lc], Tree[rc]);
}
int merge(int a, int b) {
return max(a, b);
}
int query(int node, int s, int e, int l, int r) {
if (e < l || r < s) {
return 0;
}
if (l <= s && e <= r) {
return Tree[node];
}
int left = query(lc, s, mid, l, r);
int right = query(rc, mid + 1, e, l, r);
return merge(left, right);
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
};
class SegtreeMin {
public:
int n;
vector<int> Tree;
SegtreeMin(vector<int>& v) {
n = v.size();
Tree.resize(4 * n);
build(v, 1, 0, n - 1);
}
#define lc (node << 1)
#define rc ((node << 1) | 1)
#define mid ((s + e) >> 1)
void build(vector<int>& v, int node, int s, int e) {
if (s == e) {
Tree[node] = v[s];
return;
}
build(v, lc, s, mid);
build(v, rc, mid + 1, e);
Tree[node] = merge(Tree[lc], Tree[rc]);
}
int merge(int a, int b) {
return min(a, b);
}
int query(int node, int s, int e, int l, int r) {
if (e < l || r < s) {
return inf;
}
if (l <= s && e <= r) {
return Tree[node];
}
int left = query(lc, s, mid, l, r);
int right = query(rc, mid + 1, e, l, r);
return merge(left, right);
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (auto& i : v) cin >> i;
SegtreeMax mx(v);
SegtreeMin mn(v);
ll sum = 0;
for (int i = 0; i < k; i++) sum += v[i];
ll ans = sum;
for (int i = k; i < n; i++) {
sum -= v[i - k];
sum += v[i];
ans = min(ans, sum);
int rem = mx.query(i - k + 1, i);
if (i - k >= 0) {
ans = min(ans, sum - rem + mn.query(0, i - k));
}
if (i + 1 < n) {
ans = min(ans, sum - rem + mn.query(i + 1, n - 1));
}
}
cout << ans << '\n';
}
return 0;
}