#include<bits/stdc++.h>
#define ll long long int
using namespace std;
const int N =3e5+9;
ll t[N][22],t1[N][22],a[N];
void build(ll n) {
for(ll i=1;i<=n;i++) t[i][0]=a[i];
for(ll k=1;k<22;k++) {
for(ll i=1;i+(1<<k)-1<=n;i++) {
t[i][k]=min(t[i][k-1],t[i+(1<<(k-1))][k-1]);
}
}
}
ll qmn(ll l,ll r) {
ll k=31-__builtin_clz(r-l+1);
return min(t[l][k],t[r-(1<<k)+1][k]);
}
void build1(ll n) {
for(ll i=1;i<=n;i++) t1[i][0]=a[i];
for(ll k=1;k<22;k++) {
for(ll i=1;i+(1<<k)-1<=n;i++) {
t1[i][k]=max(t1[i][k-1],t1[i+(1<<(k-1))][k-1]);
}
}
}
ll qmx(ll l,ll r) {
ll k=31-__builtin_clz(r-l+1);
return max(t1[l][k],t1[r-(1<<k)+1][k]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tc=1,tc1=1;
cin>>tc;
up:
while(tc--){
ll n,k;
cin>>n>>k;
for(ll i=1;i<=n;i++){
cin>>a[i];
}
build(n);
build1(n);
vector<ll>pre(n+2);
for(ll i=1;i<=n;i++){
pre[i]=pre[i-1]+a[i];
}
ll mn=1e18;
for(ll i=k;i<=n;i++){
ll here=pre[i]-pre[i-k];
ll hmx=qmx(i-k+1,i),hmn=1e18;
if(i!=k){
hmn=min(hmn,qmn(1,i-k));
}
if(i!=n){
hmn=min(hmn,qmn(i+1,n));
}
if(hmn<hmx){
here-=hmx,here+=hmn;
}
mn=min(mn,here);
}
cout<<mn<<'\n';
}
}