/**
* 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 = 3e5 + 9, inf = 2e9;
int a[N];
struct Node { // change this
int mn, mx, sum;
Node() {
sum = 0;
mn = inf;
mx = -inf;
}
};
struct SegmentTree {
#define lc (n << 1)
#define rc ((n << 1) | 1)
#define out LLONG_MIN // change this
vector<Node> t;
SegmentTree() {
t.resize(4 * N);
}
inline void pull(int n) { // change this
t[n].mx = max(t[lc].mx, t[rc].mx);
t[n].mn = min(t[lc].mn, t[rc].mn);
t[n].sum = t[lc].sum + t[rc].sum;
}
inline Node combine(Node a, Node b) { // change this
if(a.sum == out) return b;
if(b.sum == out) return a;
Node n;
n.mx = max(a.mx, b.mx);
n.mn = min(a.mn, b.mn);
n.sum = a.sum + b.sum;
return n;
}
void build(int n, int b, int e) {
if (b == e) {
t[n].mn = t[n].mx = t[n].sum = a[b]; // change this
return;
}
int mid = (b + e) >> 1;
build(lc, b, mid);
build(rc, mid + 1, e);
pull(n);
}
Node query(int n, int b, int e, int i, int j) {
if (b > j || e < i) {
Node x;
x.sum = out;
return x; // return approriate value
}
if (b >= i && e <= j) return t[n];
int mid = (b + e) >> 1;
return combine(query(lc, b, mid, i, j), query(rc, mid + 1, e, i, j));
}
}t;
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];
}
t.build(1, 1, n);
int ans = ps[k];
for (int i = 1; i + k - 1 <= n; i++) {
Node x = t.query(1, 1, n, i, i + k - 1);
int mn = min(t.query(1, 1, n, 1, i - 1).mn, t.query(1, 1, n, i + k, n).mn);
int sum = min(x.sum, x.sum - x.mx + mn);
ans = min(sum, ans);
}
cout << ans << nl;
}
return 0;
}