#include <bits/stdc++.h>
using namespace std;
#define SC scanf
#define PF printf
#define ull unsigned long long
#define ld long double
#define D double
#define F first
#define S second
#define pb push_back
#define pf push_front
#define MP make_pair
#define sort_a(a) sort(a.begin(),a.end());
#define sort_d(a) sort(a.rbegin(),a.rend());
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define rev(s) reverse(s.begin(),s.end())
#define it(it,mp) for(auto it=mp.begin() ; it!=mp.end() ; it++)
#define _Heart_ ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define Erase(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end());
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
typedef long long int ll;
typedef pair<int,int> PII;
const int MX = 1e5 + 5 ;
ll arr[MX], n, k;
struct heart
{
ll val ;
} Tree_mx[3 * MX], Tree_mn[3 * MX] , Tree_sum[3 * MX];
void init(int node,ll st,ll en)
{
if(st == en)
{
Tree_mx[node].val = arr[st] ;
Tree_mn[node].val = arr[st] ;
Tree_sum[node].val = arr[st] ;
return;
}
int Left = node * 2 ;
int Right = Left + 1 ;
int Mid = (st + en) / 2 ;
init(Left,st,Mid);
init(Right, Mid+1,en);
Tree_mx[node].val = max(Tree_mx[Left].val , Tree_mx[Right].val) ;
Tree_mn[node].val = min(Tree_mn[Left].val , Tree_mn[Right].val) ;
Tree_sum[node].val = Tree_sum[Left].val + Tree_sum[Right].val ;
}
ll query_mx(int node,int st, int en, int i,int j)
{
if(st> j || i > en)
{
return INT_MIN ;
}
if(st >= i && j >= en)
{
return Tree_mx[node].val ;
}
int Left = node * 2 ;
int Right = Left + 1 ;
int Mid = (st + en) / 2 ;
ll p1= query_mx(Left, st, Mid, i, j);
ll p2= query_mx(Right, Mid+1, en, i, j);
if(p2 >= p1)
{
return p2 ;
}
else
{
return p1 ;
}
}
ll query_mn(int node,int st, int en, int i,int j)
{
if(st > j || i > en)
{
return INT_MAX ;
}
if(st >= i && j >= en)
{
return Tree_mn[node].val ;
}
int Left = node * 2 ;
int Right = Left + 1 ;
int Mid = (st + en) / 2 ;
ll p1= query_mn(Left, st, Mid, i, j);
ll p2= query_mn(Right, Mid+1, en, i, j);
if(p2 <= p1)
{
return p2 ;
}
else
{
return p1 ;
}
}
ll query_sum(int node,int st, int en, int i,int j)
{
if(st > j || i > en)
{
return 0 ;
}
if(st >= i && j >= en)
{
return Tree_sum[node].val ;
}
int Left = node * 2 ;
int Right = Left + 1 ;
int Mid = (st + en) / 2 ;
ll p1= query_sum(Left, st, Mid, i, j);
ll p2= query_sum(Right, Mid+1, en, i, j);
return p1 + p2 ;
}
void solve() {
cin >> n >> k;
ll Ans = 0 , sum = 0, mxWithinRange = 0 , mnWithinRange = 0;
for(int i = 1 ; i <= n ; i++) cin >> arr[i] ;
init(1, 1, n) ;
Ans = query_sum(1 , 1 , n , 1 , k ) ;
for(int i = 1 ; i + k - 1 <= n ; i++){
sum = query_sum(1 , 1 , n , i , i + k - 1) ;
mxWithinRange = query_mx(1 , 1 , n , i , i + k - 1) ;
Ans = min(Ans , sum) ;
mnWithinRange = INT_MAX ;
if(i + k < n) {
mnWithinRange = query_mn(1 , 1 , n , i + k, n) ;
}
if(i - 1 > 0) {
mnWithinRange = min(mnWithinRange , query_mn(1 , 1 , n , 1 , i - 1)) ;
}
if(mnWithinRange < mxWithinRange) {
sum -= mxWithinRange ;
sum += mnWithinRange ;
Ans = min(Ans , sum) ;
}
}
cout << Ans << endl ;
}
int main()
{
_Heart_
int t ; cin >> t ; while(t--) solve() ;
}