#include <bits/stdc++.h>
using namespace std ;
#define int long long
const int N = 2e5+10 ;
const int mod= 1e9+7 ;
const int inf= 1e15+10 ;
#define lc (ind <<1)
#define rc (ind <<1|1)
int seg1[4*N] ;
int seg2 [4*N] ;
int seg3[4*N] ;
int a[N] ;
void build1 (int ind ,int lo ,int hi) {
if (lo==hi) {
seg1[ind]= a[lo] ;
} else {
int mid = (lo+hi) /2 ;
build1 (lc , lo, mid) ;
build1(rc ,mid+1 , hi) ;
seg1[ind]= min (seg1[lc] , seg1[rc]) ;
}
}
void build2 (int ind ,int lo ,int hi) {
if (lo==hi) {
seg2[ind]= a[lo] ;
} else {
int mid = (lo+hi) /2 ;
build2 (lc , lo, mid) ;
build2(rc ,mid+1 , hi) ;
seg2[ind]= max(seg2[lc] , seg2[rc]) ;
}
}
void build3 (int ind ,int lo ,int hi) {
if (lo==hi) {
seg3[ind]= a[lo] ;
} else {
int mid = (lo+hi) /2 ;
build3 (lc , lo, mid) ;
build3(rc ,mid+1 , hi) ;
seg3[ind]= seg3[lc] + seg3[rc];
}
}
int query1(int ind , int lo ,int hi ,int l ,int r) {
if (hi <l || lo >r) return inf ;
else if (l <=lo && hi <=r) return seg1[ind] ;
else {
int mid= (lo+hi)/2 ;
return min (query1 (lc , lo ,mid , l,r),query1 (rc , mid+1 , hi, l,r)) ;
}
}
int query2(int ind , int lo ,int hi ,int l ,int r) {
if (hi <l || lo >r) return -inf ;
else if (l <=lo && hi <=r) return seg2[ind] ;
else {
int mid= (lo+hi)/2 ;
return max (query2 (lc , lo ,mid , l,r),query2 (rc , mid+1 , hi, l,r)) ;
}
}
int query3(int ind , int lo ,int hi ,int l ,int r) {
if (hi <l || lo >r) return 0;
else if (l <=lo && hi <=r) return seg3[ind] ;
else {
int mid= (lo+hi)/2 ;
return query3 (lc , lo ,mid , l,r)+ query3 (rc , mid+1 , hi, l,r) ;
}
}
void solve () {
int n,k ;cin>>n>>k ;
for (int i=1 ;i<=n ;i++) cin>>a[i] ;
build1 (1,1,n) ;
build2 (1,1,n) ;
build3 (1,1,n) ;
int l=1 , r= k;
int ans= inf ;
for (int i=1 ; i<= (n-k+1) ; i++) {
int sm= query3 (1,1,n ,l,k) ;
int mx= query2 (1,1,n ,l,k) ;
int mn1= query1 (1,1,n ,1,l-1) ;
int mn2= query1 (1,1,n ,k+1 ,n) ;
int mn= min (mn1 , mn2) ;
ans= min (ans , sm-mx+mn ) ;
ans= min (ans ,sm) ;
l++ ; k++ ;
}
cout <<ans<<"\n" ;
}
signed main () {
int t;
cin>>t ;
while (t--)
{
solve () ;
}
}