include<bits/stdc++.h>
using namespace std;
define ll long long
define ld long double
const ll sz=1e12*2+1;
define all(a) a.begin(),a.end()
define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
define int long long
define lop for (int i=0;i<n;i++)
signed main(){ /* the idea is to take the k smallest elements in vector and by prefix array check all the choices to find the mi answer */ Fast int tt; cin>>tt; while(tt--){ int n,k; cin>>n>>k; vector a(n); lop cin>>a[i]; vector b(k); priority_queue pq; map<int , int> mp; lop pq.push(a[i]); for (int i=0;i<n-k;i++){ mp[pq.top()]++; pq.pop(); } int j=0; int s=0; for (int i=0;i<n;i++){ if (mp[a[i]]&&s!=n-k){ s++; mp[a[i]]--; continue; } b[j++]=a[i]; }
int pre[k],suffix[k]; pre[0]=b[0]; suffix[k-1]=b[k-1]; for (int i=1;i<k;i++){ pre[i]=pre[i-1]+b[i]; } for (int i=k-2;i>=0;i--){ suffix[i]=suffix[i+1]+b[i]; } int ans=sz; for (int i=0;i<k;i++){ ll time1=0,time2=0,res; time1=pre[i]; if (i<k-1)time2=suffix[i+1]; res=max(time1,time2); ans=min(ans,res); } cout<<ans<<"\n"; mp.clear(); } } //im sorry that my code isnt clean but i still newbie