#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define all(v) v.begin(),v.end()
#define nl "\n"
#define FIO \
ios_base::sync_with_stdio(false);\
cin.tie(NULL);\
cout.tie(NULL);
void solve()
{
ll n,k; cin>>n>>k;
vector<ll>v(n);
for(ll i=0;i<n;i++) cin>>v[i];
ll l=0,sum=0,ans=LLONG_MAX;
ll left=-1,right=-1;
for(ll r=0;r<n;r++)
{
sum+=v[r];
while(r-l+1>k)
{
sum-=v[l];
l++;
}
if(r-l+1==k)
{
if(sum<ans)
{
ans=sum;
left=l,right=r;
}
}
}
// cout<<ans<<nl;
// cout<<left<<" "<<right<<nl;
ll mn=LLONG_MAX,mx=LLONG_MIN;
for(ll i=0;i<n;i++)
{
if(i>=left && i<=right) mx=max(mx,v[i]);
else mn=min(mn,v[i]);
}
if(mn<mx) cout<<ans-mx+mn<<nl;
else cout<<ans<<nl;
// cout<<ans<<nl;
// cout<<mn<<" "<<mx<<nl;
}
int32_t main()
{
FIO
ll t=1;
cin>>t;
for(ll i=1;i<=t;i++)
{
// cout<<"Case "<<i<<": ";
solve();
}
}