// SEC_Infra_Red
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define nl "\n"
#define pb push_back
#define fi first
#define F first
#define pll pair<ll,ll>
#define sc second
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sortv(v) sort(v.begin(), v.end())
#define revv(v) reverse(v.begin(), v.end())
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vinput ll n; cin >> n; vector<ll>v; for(ll i = 0; i <n; i++){ll in; cin >> in; v.push_back(in);}
#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];
vector<ll>age(n),pore(n);
ll mn=LLONG_MAX;
for(ll i=0;i<n;i++)
{
age[i]=min(mn,v[i]);
mn=min(mn,v[i]);
}
mn=LLONG_MAX;
for(ll i=n-1;i>=0;i--)
{
pore[i]=min(mn,v[i]);
mn=min(mn,v[i]);
}
ll l=0,sum=0,ans=LLONG_MAX;
multiset<ll>mst;
for(ll r=0;r<n;r++)
{
sum+=v[r];
mst.insert(v[r]);
while(r-l+1>k)
{
sum-=v[l];
mst.erase(mst.find(v[l]));
l++;
}
if(r-l+1==k)
{
if(l==0 && r==n-1)
{
ans=min(ans,sum);
}
else if(l==0)
{
if(*(--mst.end())>pore[r+1]) ans=min(ans,(sum-*(--mst.end())+pore[r+1]));
else ans=min(ans,sum);
}
else if(r==n-1)
{
if(*(--mst.end())>age[l-1]) ans=min(ans,(sum-*(--mst.end())+age[l-1]));
else ans=min(ans,sum);
}
else
{
if(*(--mst.end())>min(age[l-1],pore[r+1])) ans=min(ans,(sum-*(--mst.end())+min(age[l-1],pore[r+1])));
else ans=min(ans,sum);
}
}
}
cout<<ans<<nl;
}
int32_t main()
{
FIO
ll t=1;
cin>>t;
for(ll i=1;i<=t;i++)
{
// cout<<"Case "<<i<<": ";
solve();
}
}