#include <bits/stdc++.h>
using namespace std;
#define inf 1013161010
#define mod 1000000007
#define mod1 998244353
#define ll long long
#define lf long double
#define sz(x) ((int)x.size())
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,a,b) for(ll i=a;i<=b;i++)
#define fr freopen("x.txt","r",stdin)
#define frc freopen("y.txt","w",stdout)
#define all(x) x.begin(),x.end()
#define set0(x) memset(x,0,sizeof(x))
#define dbg cout<<"yo "<<endl;
#define pset(n) fixed<<showpoint<<setprecision(n)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pair<int,int> >
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define si set<int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
template<class T>
using min_pq = priority_queue<T,vector<T>,greater<T>>;
ll toint(const string &s) { stringstream ss; ss << s; ll x; ss >> x; return x; }
string tostring ( ll number ){ stringstream ss; ss<< number; return ss.str();}
template <typename T>
void print1(T arr[], int n) { rep(i, n) { cout << arr[i] << " "; } cout << endl; }
template <typename T, size_t N, size_t M>
void print2(T (&arr)[N][M], int n, int m) { rep(i, n) { rep(j, m) { cout << arr[i][j] << " "; } cout << endl; } cout << endl; }
template <typename T>
void print1v(const vector<T>& vec, int n) { for (int i = 0; i < n && i < vec.size(); i++) { cout << vec[i] << " "; } cout << endl; }
template <typename T>
void print2v(const vector<vector<T>>& vec, int n, int m) { for (int i = 0; i < n && i < vec.size(); i++) { for (int j = 0; j < m && j < vec[i].size(); j++) { cout << vec[i][j] << " "; } cout << endl; } cout << endl;}
const lf pi = 2*acos(0);
const int nn = 500006;
const lf EPS = 0.000000001;
ll gcd(ll a,ll b){return (b==0)? a:gcd(b,a%b); }
ll fast_pow(ll a,ll m, ll mode) { if(m==0) return 1; else if(m==1) return a; ll h=fast_pow(a,m/2,mode); if(m%2==0) return ((h*h)%mode); else return ((((h*h)%mode)*a)%mode);}
ll mmi(ll b, ll mode){return fast_pow(b, mode-2, mode);}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
ll n, m, k, x, ans=0;
cin >> n >> k;
vi arr(n), mn(n), bmn(n);
rep(i, n) {
cin >> arr[i];
if (i == 0) {
mn[i] = arr[i];
} else {
mn[i] = min(arr[i], mn[i-1]);
}
}
bmn[n-1] = arr[n-1];
for(int i = n-2; i >= 0; i--) {
bmn[i] = min(bmn[i+1], arr[i]);
}
ans = 1000000000000000000;
int replace;
ll sum = 0;
multiset<int> s;
for(int i = 0; i < k; i++) {
s.insert(arr[i]);
sum += arr[i];
}
// cout << sum << " " << ans << endl;
ans = min(sum, ans);
if (k < n) {
replace = bmn[k];
auto it = s.end();
it--;
// cout <<"tt " << replace << " " << sum << " " << *it << endl;
ans = min(ans, sum + replace - *it);
}
// cout << sum << " " << ans << endl;
for(int i = k; i < n; i++) {
s.erase(s.lower_bound(arr[i-k]));
s.insert(arr[i]);
sum += arr[i];
sum -= arr[i-k];
ans = min(ans, sum);
auto it = s.end();
it--;
if (i + 1 < n) {
replace = bmn[i+1];
ans = min(ans, sum + replace - *it);
}
if (i-k > 0) {
replace = mn[i-k-1];
ans = min(ans, sum + replace - *it);
}
}
cout << ans << endl;
}
return 0;
}