#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define print(v) for(auto data : v) cout << data << " "; cout << endl;
using namespace std;
void solve()
{
int n, k; cin >> n >> k;
vector<ll> v(n); for(auto &data : v) cin >> data;
// print(v);
int l = 0, r = 0;
ll sum = 0, mn = 1e9;
int L, R;
while (r < n)
{
sum += v[r];
if(r-l+1 == k)
{
if(sum < mn)
{
mn = min(mn, sum);
L = l, R = r;
}
sum -= v[l]; l++;
}
r++;
}
ll mx = -1e9;
for (int i = L; i <= R; i++)
{
mx = max(mx, v[i]);
}
ll min_val = mx;
for (int i = 0; i < n; i++)
{
if(i >= L && i <= R) continue;
min_val = min(min_val, v[i]);
}
if(mx > min_val)
{
mn -= mx;
mn += min_val;
}
cout << mn << endl;
// cout << L << " " << R << " -> " << mn << endl;
}
int main ()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t; cin >> t;
// int t = 1;
while (t--) solve();
return 0;
}