#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) 42
#endif
const ll N = 1e5 + 9;
ll a[N];
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll T;
cin >> T;
while (T--)
{
ll n, k;
cin >> n >> k;
for (ll i = 1 ; i <= n ; i++) {
cin >> a[i];
}
multiset<ll> st1, st2;
ll l = 1, r = k;
ll s = 0;
for (ll i = 1 ; i <= k ; i++) {
st1.insert(a[i]);
s += a[i];
}
if (n == k) {
cout << s << '\n';
continue;
}
for (ll i = k + 1 ; i <= n ; i++) {
st2.insert(a[i]);
}
ll res = 1e16;
while (r <= n) {
res = min({res, s - *st1.rbegin() + *st2.begin(), s});
if (r == n)
break;
st1.erase(st1.find(a[l]));
st2.insert(a[l]);
st1.insert(a[r + 1]);
st2.erase(st2.find(a[r + 1]));
s = s - a[l] + a[r + 1];
l += 1, r += 1;
}
cout << res << '\n';
}
}