#include <bits/stdc++.h>
#define nl '\n'
#define ll long long int
#define all(v) v.begin(),v.end()
#define print(v) for(auto data : v) cout << data << " "; cout << nl
using namespace std;
int cs = 1;
void solve()
{
// cout << "case: " << cs++ << nl;
ll n, k; cin >> n >> k;
vector<ll> v(n); for(auto &data : v) cin >> data;
ll ans = LLONG_MAX, sum = 0;
for (int l = 0, r = 0; r < n; r++)
{
// cout << l << " ";
sum += v[r];
if(r-l+1 == k)
{
ll mxFromWindow, mnBefore = LLONG_MAX, mnAfter = LLONG_MAX;
mxFromWindow = *max_element(v.begin() + l, v.begin() + r + 1);
if(l > 0) mnBefore = *min_element(v.begin(), v.begin() + l);
if(r+2 < n) mnAfter = *min_element(v.begin() + r + 1, v.end());
ll tmp = sum;
if(mxFromWindow > min(mnBefore, mnAfter))
{
// cout << "#######################" << nl;
tmp -= mxFromWindow; tmp += min(mnBefore, mnAfter); // sum e problem
}
// cout << mnBefore << " " << mxFromWindow << " " << mnAfter << nl;
ans = min(ans, tmp);
// cout << "Sum : " << sum << " <-> " << "ans : " << ans << nl;
sum -= v[l]; l++;
}
}
cout << ans << nl;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t; cin >> t;
while (t--) solve();
return 0;
}