#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define nl "\n"
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define Yes cout<<"YES\n"
#define No cout<<"NO\n"
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define F first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sortv(v) sort(v.begin(), v.end());
#define rsort(v) sort(v.rbegin(), v.rend());
#define revv(v) reverse(v.begin(), v.end());
const int N=1e6+9;
const int mod1=1e9+7, mod2=1e9+9;
const int base1=137, base2=277;
const long long int inf=1e9;
#define saiful_islam_bk
ll a[N];
#define lc (n << 1)
#define rc ((n << 1) | 1)
ll t[4 * N], lazy[4 * N];
inline void push(ll n, ll b, ll e){
if (lazy[n] == 0) return;
t[n] = t[n] + lazy[n] * (e - b + 1);
if (b != e){
lazy[lc] = lazy[lc] + lazy[n];
lazy[rc] = lazy[rc] + lazy[n];
}
lazy[n] = 0;
}
pll combine(ll a,ll b){
return {max(a, b), min(a, b)};
}
inline void pull(ll n) {
t[n] = t[lc] + t[rc];
}
void build(ll n, ll b, ll e){
lazy[n] = 0;
if (b == e){
t[n] = a[b];
return;
}
ll mid = (b + e) >> 1;
build(lc, b, mid);
build(rc, mid + 1, e);
pull(n);
}
void upd(ll n, ll b, ll e, ll i, ll j, ll v){
push(n, b, e);
if (j < b || e < i) return;
if (i <= b && e <= j) {
lazy[n] = v; //set lazy
push(n, b, e);
return;
}
ll mid = (b + e) >> 1;
upd(lc, b, mid, i, j, v);
upd(rc, mid + 1, e, i, j, v);
pull(n);
}
pll query(ll n, ll b, ll e, ll i, ll j){
push(n, b, e);
if (i > e || b > j) return {0, 0}; //return null
if (i <= b && e <= j) return {t[n], t[n]};
ll mid = (b + e) >> 1; pll k;
k.F=combine(query(lc, b, mid, i, j).F, query(rc, mid + 1, e, i, j).F).F;
k.sc=combine(query(lc, b, mid, i, j).sc, query(rc, mid + 1, e, i, j).sc).sc;
return k;
}
void solve(){
ll n, m=0, k=0, ans=LLONG_MAX; cin>>n>>k; ll pre[n]; ll mx=0, mn=ans;
for(ll i=0; i<n; i++){
cin>>a[i]; pre[i]=a[i]; if(i) pre[i]+=pre[i-1];
if(i<k) mx=max(mx, a[i]);
else mn=min(mn, a[i]);
}
// build(1, 0, n-1);
ans=pre[k-1]; //m=query(1, 0, n-1, j, i);
ans=min(ans, pre[k-1]-mx+mn);
for(ll i=k, j=0; i<n; i++, j++){
ans=min(ans, pre[i]-pre[j]);
}
for(ll i=k, j=0; i<n; i++){
if(i==0){
mx=query(1, 0, n-1, j, i).F;
mn=query(1, 0, n-1, i+1, n-1).sc;
ans=min(ans, pre[i]-pre[j]-mx+mn);
}else if(i==n-1){
mx=query(1, 0, n-1, j+1, i).F;
mn=query(1, 0, n-1, 0, j).sc;
ans=min(ans, pre[i]-pre[j]-mx+mn);
}else{
mx=query(1, 0, n-1, j+1, i).F;
mn=min(query(1, 0, n-1, 0, j).sc, query(1, 0, n-1, i+1, n-1).sc);
ans=min(ans, pre[i]-pre[j]-mx+mn);
}
}
cout<<ans<<nl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
saiful_islam_bk
ll test=1;
cin>>test;
for(ll ii=1; ii<=test; ii++){
//cout<<"Case "<<ii<<": ";
solve();
}
}