#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define MOD 1000000007
#define vll vector<ll>
#define endl "\n"
#define all(v) v.begin(), v.end()
#define mem(a,b) memset(a, b, sizeof(a))
#define co(n) cout << n << endl
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define fr(x, n) for (int i = x; i < n; ++i)
#define fraction(x) cout << fixed << setprecision(x)
#define Baba_Yaga ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
const double eps = 1e-9;
const int N = 2e5+123;
ll gcd(ll a,ll b) { return __gcd(a,b); }
ll lcm(ll a,ll b) { return (a*b)/__gcd(a,b); }
int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
void solve()
{
ll n, k; cin >> n >> k;
vll v(n);
fr(0, n) cin >> v[i];
// assiagn min_values in r_l and l_r ...
vll l_r(n), r_l(n);
l_r[0] = v[0];
r_l[n-1] = v[n-1];
for(int i=n-2; i>=0; i--)
{
r_l[i] = min(r_l[i+1], v[i]);
}
fr(1, n)
{
l_r[i] = min(v[i], l_r[i-1]);
}
ll sum = 0;
multiset <ll> st;
for(int i=0; i<k; i++)
{
sum += v[i];
st.insert(v[i]);
}
ll maxi = *(--st.end());
ll ans = sum;
if(k < n)
{
ans = min(sum, sum - maxi + r_l[k]);
}
else ans = sum;
ll j = 0;
for(int i=k; i<n; i++)
{
ll mini_1 = 1e10, mini_2 = 1e10;
mini_1 = l_r[j];
if(i+1 < n)
{
mini_2 = r_l[i+1];
}
sum -= v[j];
auto it = st.find(v[j]);
st.erase(it);
j++;
st.insert(v[i]);
sum += v[i];
maxi = *(--st.end());
ans = min(ans, sum - maxi + min(mini_1, mini_2));
}
cout << ans << endl;
}
// --- Think the problem backwards ---
int main()
{
Baba_Yaga;
ll tc = 1; cin >> tc;
while(tc--) solve();
}