#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug(a) cout<<#a<<" : "<<a<<endl;
#define bug2(a,b) cout<<#a<<" : "<<a<<"\t"<<#b<<" : "<<b<<endl;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll t=1;
cin>>t;
ll cs=1;
while(t--){
ll n,k;cin>>n>>k;
vector<ll>v(n);
for(ll i=0;i<n;i++)cin>>v[i];
multiset<ll>st;
ll ans=1e13;
ll sum=0;
for(ll i=0;i<k;i++){
st.insert(v[i]);
sum+=v[i];
}
if(k==n)
{
cout<<sum<<endl;
continue;
}
ans=min(ans,sum);
vector<ll>pref(n),suff(n);
ll mn=1e9;
for(ll i=0;i<n;i++){
mn=min(mn,v[i]);
pref[i]=mn;
}
mn=1e9;
for(ll i=n-1;i>=0;i--){
mn=min(mn,v[i]);
suff[i]=mn;
}
ll curans=sum+suff[k]-*st.rbegin();
ans=min(ans,curans);
// for(ll i=0;i<n;i++){
// cout<<pref[i]<<' ';
// }
// cout<<endl;
// for(ll i=0;i<n;i++){
// cout<<suff[i]<<' ';
// }
// cout<<endl;
for(ll i=k;i<n-1;i++){
sum+=v[i];
sum-=v[i-k];
ans=min(ans,sum);
ll curlmn=pref[i-1];
ll currmn=suff[i+1];
ll curmn=min(curlmn,currmn);
st.erase(st.find(v[i-k]));
st.insert(v[i]);
ll curans=sum-*st.rbegin()+curmn;
ans=min(ans,curans);
}
st.erase(st.find(v[n-1-k]));
st.insert(v[n-1]);
sum += v[n-1];
sum -= v[n-1-k];
ans = min(ans,sum);
curans=sum+pref[n-k-1]-*st.rbegin();
ans=min(ans,curans);
cout<<ans<<endl;
}
return 0;
}