// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fast_IO ios_base::sync_with_stdio(0), cin.tie(0) //==== Speed
#define ll long long //==== Aliases Or TypeDEf
#define ld long double
#define ull unsigned long long
#define pl pair<ll, ll>
#define ff first //==== Macros
#define ss second
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define all(x) x.begin(), x.end()
#define allr(a) a.rbegin(), a.rend()
#define endl '\n'
#define coutd cout << fixed << setprecision(15) // coutd<<res<<endl;
#define deb(x) cout << #x << " = " << x << endl //==== Debug
#define deb2(x, y) cout << #x << " = " << x << " , " << #y << " = " << y << endl
#define deb3(x, y, z) cout << #x << " = " << x << " , " << #y << " = " << y << " , " << #z << " = " << z << endl
#define deb4(x, y, z, w) cout << #x << " = " << x << " , " << #y << " = " << y << " , " << #z << " = " << z << " , " << #w << " = " << w << endl
namespace io{ //==== Operator overloads
template<typename First, typename Second> istream& operator >> ( istream &is, pair<First, Second> &p ) { return is >> p.first >> p.second; }// cin >> pair<T1, T2>
template<typename First, typename Second> ostream& operator << ( ostream &os, const pair<First, Second> &p ) { return os << p.first << " " << p.second; }// cout << pair<T1, T2>
template<typename First> istream& operator >> ( istream &is, vector<First> &v ) { for( First &x : v ) { is >> x; } return is; }// cin >> vector<T>
template<typename First> ostream& operator << ( ostream &os, const vector<First> &v ) { bool space = false; for( First x : v ) { if( space ) os << " "; space = true; os << x; } return os; }// cout << vector<T>
} using namespace io;
//============================================== EXT. STL
template <typename T> using PQ = priority_queue<T>;
template <typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;// ordered_set<ll> st;st.order_of_key(x) // number of elements < x ; *(st.find_by_order(x)) (x)th largest element
template <typename T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
//============================================== Mathematical functions
inline ll Ceil(ll p, ll q) {return p < 0 ? p / q : p / q + !!(p % q);}
inline ll Floor(ll p, ll q) {return p > 0 ? p / q : p / q - !!(p % q);}
inline double logb(ll base,ll num){ return (double)log(num)/(double)log(base);}
double euclidean_distance(ll x1,ll y1,ll x2,ll y2){double a=(x2-x1)*(x2-x1);double b=(y2-y1)*(y2-y1);double c=(double)sqrt(a+b);return c;}
ll __lcm(ll a, ll b){return (a/__gcd(a,b)*b);}
ll BigMod (ll b, ll p, ll m) {if (p == 0) return 1; if (p % 2 == 0) {ll s = BigMod(b, p / 2, m); return ((s % m) * (s % m)) % m;} return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;}
ll ModInv (ll b, ll m) {return BigMod(b, m - 2, m);}
//>>Check
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}
//============================================== Bits
#define Set(x, k) (x |= (1LL << k))
#define Unset(x, k) (x &= ~(1LL << k))
#define Check(x, k) (x & (1LL << k))
#define Toggle(x, k) (x ^= (1LL << k))
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
//>>Convert
string decToBinary(int n){string s="";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}
//============================================== Global_Constants
const ll N = 500050;
const ll INF = 1e18 + 10;
const ull mod = 1e9 + 7;
//============================================== user define function ==============================================//
/// @brief Segment Tree for ...
const ll NN = 3e5 + 9;
ll arr[NN];
struct SegTree_sum
{
ll t[4 * NN];
ll inf = 1e18;
SegTree()
{
memset(t, 0, sizeof t);
}
void build(ll n, ll b, ll e)
{
if (b == e)
{
t[n] = arr[b];
return;
}
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
build(l, b, mid);
build(r, mid + 1, e);
t[n] = t[l] + t[r];
}
ll query(ll n, ll b, ll e, ll i, ll j)
{
if (b > j || e < i)
return 0;
if (b >= i && e <= j)
return t[n];
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
ll L = query(l, b, mid, i, j);
ll R = query(r, mid + 1, e, i, j);
return L + R;
}
} ST_sum;
struct SegTree_mn
{
ll t[4 * NN];
ll inf = 1e18;
SegTree()
{
memset(t, 0, sizeof t);
}
void build(ll n, ll b, ll e)
{
if (b == e)
{
t[n] = arr[b];
return;
}
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
build(l, b, mid);
build(r, mid + 1, e);
t[n] = min(t[l] , t[r]);
}
ll query(ll n, ll b, ll e, ll i, ll j)
{
if (b > j || e < i)
return inf;
if (b >= i && e <= j)
return t[n];
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
ll L = query(l, b, mid, i, j);
ll R = query(r, mid + 1, e, i, j);
return min(L , R);
}
} ST_mn;
struct SegTree_mx
{
ll t[4 * NN];
ll inf = 1e18;
SegTree()
{
memset(t, 0, sizeof t);
}
void build(ll n, ll b, ll e)
{
if (b == e)
{
t[n] = arr[b];
return;
}
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
build(l, b, mid);
build(r, mid + 1, e);
t[n] = max(t[l] , t[r]);
}
ll query(ll n, ll b, ll e, ll i, ll j)
{
if (b > j || e < i)
return -1 *inf;
if (b >= i && e <= j)
return t[n];
ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
ll L = query(l, b, mid, i, j);
ll R = query(r, mid + 1, e, i, j);
return max(L , R);
}
} ST_mx;
////////// THE END
void Sayem(ll tc)
{
ll n = 0, m = 0, a = 0, b = 0, c = 0, i = 0, j = 0, k = 0, l = 0, r = 0, ans = 0, temp = 0, cnt = 0, mnt = 0, sum = 0;
string s, s1, s2, s3;
cin >> n >> m;
vector<ll> aa, bb, cc, dd;
map<ll, ll> mp;
set<ll> st;
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
ST_sum.build(1, 1, n);
ST_mn.build(1, 1, n);
ST_mx.build(1, 1, n);
ans = INF;
for(int i = 1; i + m - 1 <= n; i++){
l = i , r = i + m - 1;
temp = ST_sum.query(1, 1, n, l, r);
ll mx = ST_mx.query(1, 1, n, l, r);
if(n == m){
ans = min(ans, temp);
continue;
}
ll mn = INF;
if(l != 1){
mn = min(mn, ST_mn.query(1, 1, n, 1, l - 1));
}
if(r != n){
mn = min(mn, ST_mn.query(1, 1, n, r + 1, n));
}
temp -= mx;
temp += mn;
ans = min(ans, temp);
}
cout << ans << endl;
return;
}
//=================================================== STOP =======================================================//
int32_t main()
{
fast_IO;
int _TB = 1, tc = 1;
cin >> _TB;
while (_TB--)
{
Sayem(tc++);
}
return 0;
}