#include <bits/stdc++.h>
#define ll long long
#define db long double
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define fi first
#define se second
#define pb push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fix(x,num) fixed << setprecision(num)<<x
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
string di[] = {"L", "R", "U", "D", "UR", "UL", "DR", "DL"};
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
const ll N =1e6+5,mod =998244353,inf =1e18;
struct sparse{
sparse(vector<ll>&v,ll n){
sset(v,n);
}
vector<vector<pll>>tmx,tmn;
void sset(vector<ll>&v,ll n){
tmx=tmn=vector<vector<pll>>(n,vector<pll>(20));
for(int i=0;i<n;++i){
tmx[i][0]=tmn[i][0]={v[i],i};
}
for(int pw=1;(1<<pw)<=n;++pw){
for (int i = 0; i+(1<<pw) <=n ; ++i) {
tmx[i][pw]=max(tmx[i][pw-1],tmx[i+(1<<(pw-1))][pw-1]);
tmn[i][pw]=min(tmn[i][pw-1],tmn[i+(1<<(pw-1))][pw-1]);
}
}
}
pll getmx(ll l,ll r){
if(r<l) swap(l,r);
ll z=log2(r-l+1);
return max(tmx[l][z],tmx[r-(1<<z)+1][z]);
}
pll getmn(ll l,ll r){
if(r<l) swap(l,r);
ll z=log2(r-l+1);
return min(tmn[l][z],tmn[r-(1<<z)+1][z]);
}
};
vector<ll> calcPre(vector<ll>&v,ll n,ll k){
vector<ll>ret(sz(v));
stack<pll>s;
s.push({inf,k-1});
for (int i = k; i <=n ; ++i) {
ll cur=v[i]-v[i-k];
while(s.top().fi<=cur)
s.pop();
ret[i]=s.top().se-k+1;
s.push({cur,i});
}
return ret;
}
vector<ll> calcSuf(vector<ll>&v,ll n,ll k){
vector<ll>ret(sz(v));
stack<pll>s;
s.push({inf,n+1});
for (int i = n; i >=k ; --i) {
ll cur=v[i]-v[i-k];
while(s.top().fi<=cur)
s.pop();
ret[i]=s.top().se;
s.push({cur,i});
}
return ret;
}
void solve() {
ll n,k; cin>>n>>k;
vector<ll>a(n+5),b(n+5);
for (int i = 1; i <=n ; ++i) cin>>a[i];
for (int i = 1; i <=n ; ++i) {
cin >> b[i];
a[i] = b[i] - a[i];
a[i] += a[i - 1];
b[i] += b[i - 1];
}
vector<ll>pre= calcPre(b,n,k),suf= calcSuf(b,n,k);
sparse s(a,n+5);
ll ans=-inf;
for (int i = k; i <=n ; ++i) {
ll l=pre[i]+1,r=suf[i]-1;
l=s.getmn(l,i-k+1).se,r=s.getmx(i,r).se;
ans=max(ans,a[r]-a[l-1]-(b[i]-b[i-k]));
}
cout<<ans<<'\n';
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
int testCases = 1;
cin >> testCases;
for (int i = 1; i <=testCases ; ++i) {
// cout<<"Case "<<i<<":\n";
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZGIgbG9uZyBkb3VibGUKI2RlZmluZSBzeih2KSAoaW50KXYuc2l6ZSgpCiNkZWZpbmUgYWxsKHYpIHYuYmVnaW4oKSx2LmVuZCgpCiNkZWZpbmUgcmFsbCh2KSB2LnJiZWdpbigpLHYucmVuZCgpCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBwbGwgcGFpcjxsbCxsbD4KI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50PgojZGVmaW5lIGZpeCh4LG51bSkgZml4ZWQgPDwgc2V0cHJlY2lzaW9uKG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ldWzBdPXRtbltpXVswXT17dltpXSxpfTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBwdz0xOygxPDxwdyk8PW47Kytwdyl7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpKygxPDxwdykgPD1uIDsgKytpKSB7CiAgICAgICAgICAgICAgICB0bXhbaV1bcHddPW1heCh0bXhbaV1bcHctMV0sdG14W2krKDE8PChwdy0xKSldW3B3LTFdKTsKICAgICAgICAgICAgICAgIHRtbltpXVtwd109bWluKHRtbltpXVtwdy0xXSx0bW5baSsoMTw8KHB3LTEpKV1bcHctMV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHBsbCBnZXRteChsbCBsLGxsIHIpewogICAgICAgIGlmKHI8bCkgc3dhcChsLHIpOwogICAgICAgIGxsIHo9bG9nMihyLWwrMSk7CiAgICAgICAgcmV0dXJuIG1heCh0bXhbbF1bel0sdG14W3ItKDE8PHopKzFdW3pdKTsKICAgIH0KICAgIHBsbCBnZXRtbihsbCBsLGxsIHIpewogICAgICAgIGlmKHI8bCkgc3dhcChsLHIpOwogICAgICAgIGxsIHo9bG9nMihyLWwrMSk7CiAgICAgICAgcmV0dXJuIG1pbih0bW5bbF1bel0sdG1uW3ItKDE8PHopKzFdW3pdKTsKICAgIH0KCgp9OwoKdmVjdG9yPGxsPiBjYWxjUHJlKHZlY3RvcjxsbD4mdixsbCBuLGxsIGspewogICAgdmVjdG9yPGxsPnJldChzeih2KSk7CiAgICBzdGFjazxwbGw+czsKICAgIHMucHVzaCh7aW5mLGstMX0pOwogICAgZm9yIChpbnQgaSA9IGs7IGkgPD1uIDsgKytpKSB7CiAgICAgICAgbGwgY3VyPXZbaV0tdltpLWtdOwogICAgICAgIHdoaWxlKHMudG9wKCkuZmk8PWN1cikKICAgICAgICAgICAgcy5wb3AoKTsKICAgICAgICByZXRbaV09cy50b3AoKS5zZS1rKzE7CiAgICAgICAgcy5wdXNoKHtjdXIsaX0pOwogICAgfQogICAgcmV0dXJuIHJldDsKfQoKdmVjdG9yPGxsPiBjYWxjU3VmKHZlY3RvcjxsbD4mdixsbCBuLGxsIGspewogICAgdmVjdG9yPGxsPnJldChzeih2KSk7CiAgICBzdGFjazxwbGw+czsKICAgIHMucHVzaCh7aW5mLG4rMX0pOwogICAgZm9yIChpbnQgaSA9IG47IGkgPj1rIDsgLS1pKSB7CiAgICAgICAgbGwgY3VyPXZbaV0tdltpLWtdOwogICAgICAgIHdoaWxlKHMudG9wKCkuZmk8PWN1cikKICAgICAgICAgICAgcy5wb3AoKTsKICAgICAgICByZXRbaV09cy50b3AoKS5zZTsKICAgICAgICBzLnB1c2goe2N1cixpfSk7CiAgICB9CiAgICByZXR1cm4gcmV0Owp9Cgp2b2lkIHNvbHZlKCkgewogICAgbGwgbixrOyAgY2luPj5uPj5rOwogICAgdmVjdG9yPGxsPmEobis1KSxiKG4rNSk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PW4gOyArK2kpICBjaW4+PmFbaV07CiAgICBmb3IgKGludCBpID0gMTsgaSA8PW4gOyArK2kpIHsKICAgICAgICBjaW4gPj4gYltpXTsKICAgICAgICBhW2ldID0gYltpXSAtIGFbaV07CiAgICAgICAgYVtpXSArPSBhW2kgLSAxXTsKICAgICAgICBiW2ldICs9IGJbaSAtIDFdOwogICAgfQoKICAgIHZlY3RvcjxsbD5wcmU9IGNhbGNQcmUoYixuLGspLHN1Zj0gY2FsY1N1ZihiLG4sayk7CiAgICBzcGFyc2UgcyhhLG4rNSk7CgogICAgbGwgYW5zPS1pbmY7CiAgICBmb3IgKGludCBpID0gazsgaSA8PW4gOyArK2kpIHsKICAgICAgICBsbCBsPXByZVtpXSsxLHI9c3VmW2ldLTE7CiAgICAgICAgbD1zLmdldG1uKGwsaS1rKzEpLnNlLHI9cy5nZXRteChpLHIpLnNlOwogICAgICAgIGFucz1tYXgoYW5zLGFbcl0tYVtsLTFdLShiW2ldLWJbaS1rXSkpOwogICAgfQogICAgY291dDw8YW5zPDwnXG4nOwoKfQoKaW50MzJfdCBtYWluKCkgewoKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKDApLGNvdXQudGllKDApOwoKICAgIGludCB0ZXN0Q2FzZXMgPSAxOwogICAgY2luID4+IHRlc3RDYXNlczsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8PXRlc3RDYXNlcyA7ICsraSkgewogICAgICAgIC8vIGNvdXQ8PCJDYXNlICI8PGk8PCI6XG4iOwogICAgICAgIHNvbHZlKCk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K