#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <stack>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
/*typedef tree<string, null_type, less_equal<string>, rb_tree_tag, tree_order_statistics_node_update>orderedSet;
void Erase(orderedSet &s, int val) {
int rank = s.order_of_key(val);
auto it = s.find_by_order(rank);
s.erase(it);
}*/
typedef long long ll;
int Fastpow(int Base , int Power , int MOD){
int Result = 1;
while(Power){
if(Power & 1){
Result = (Result * Base) % MOD;
}
Base = (Base * Base) % MOD;
Power >>= 1;
}
return Result;
}
vector<int> Is_prime(1001, 1);
void sieve ()
{
Is_prime[0] = Is_prime[1] = 1;
for(int i = 2; i * i <= 1000; i++){
if(Is_prime[i]){
for(int j = i*i; j <= 1000; j += i){
Is_prime[j] = 0;
}
}
}
}
void Henry()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void solve() {
int n,k; cin >> n >> k;
vector<int> a(n), b(n);
map<int, int> mp, revMP;
for(int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]];
}
for(int i = 0; i < n; i++) {
cin >> b[i];
mp[b[i]];
}
int id = 1;
for(auto &[ _ , v ] : mp) {
v = id++;
}
for(auto &[ _ , v ] : mp) {
revMP[v] = mp[_];
}
vector<int>price(id + 1), badPrices(id + 1);
for(int i = 1; i <= n; i++) {
int l = a[i], r = b[i];
price[1]++;
price[r + 1]--;
badPrices[l + 1]++;
badPrices[r + 1]--;
}
for(int i = 1; i <= id; i++) {
price[i] += price[i - 1];
badPrices[i] += badPrices[i - 1];
}
int ans = 0;
for(int i = 1; i <= id; i++) {
if(badPrices[i] > k)continue;
ans = max(ans, price[i] * revMP[i]);
}
cout << ans << "\n";
}
int main() {
Henry();
int q = 1;
cin >> q;
while (q--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmh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ICAgICAgICAgICBJc19wcmltZVtqXSA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCgp2b2lkIEhlbnJ5KCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgY291dC50aWUobnVsbHB0cik7Cn0KCnZvaWQgc29sdmUoKSB7CiAgICBpbnQgbixrOyBjaW4gPj4gbiA+PiBrOwogICAgdmVjdG9yPGludD4gYShuKSwgYihuKTsKICAgIG1hcDxpbnQsIGludD4gbXAsIHJldk1QOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNpbiA+PiBhW2ldOwogICAgICAgIG1wW2FbaV1dOwogICAgfQogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNpbiA+PiBiW2ldOwogICAgICAgIG1wW2JbaV1dOwogICAgfQoKICAgIGludCBpZCA9IDE7CiAgICBmb3IoYXV0byAmWyBfICwgdiBdIDogbXApIHsKICAgICAgICB2ID0gaWQrKzsKICAgIH0KCiAgICBmb3IoYXV0byAmWyBfICwgdiBdIDogbXApIHsKICAgICAgICByZXZNUFt2XSA9IG1wW19dOwogICAgfQoKICAgIHZlY3RvcjxpbnQ+cHJpY2UoaWQgKyAxKSwgYmFkUHJpY2VzKGlkICsgMSk7CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGludCBsID0gYVtpXSwgciA9IGJbaV07CiAgICAgICAgcHJpY2VbMV0rKzsKICAgICAgICBwcmljZVtyICsgMV0tLTsKCiAgICAgICAgYmFkUHJpY2VzW2wgKyAxXSsrOwogICAgICAgIGJhZFByaWNlc1tyICsgMV0tLTsKICAgIH0KCiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IGlkOyBpKyspIHsKICAgICAgICBwcmljZVtpXSArPSBwcmljZVtpIC0gMV07CiAgICAgICAgYmFkUHJpY2VzW2ldICs9IGJhZFByaWNlc1tpIC0gMV07CiAgICB9CiAgICBpbnQgYW5zID0gMDsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gaWQ7IGkrKykgewogICAgICAgIGlmKGJhZFByaWNlc1tpXSA+IGspY29udGludWU7CiAgICAgICAgYW5zID0gbWF4KGFucywgcHJpY2VbaV0gKiByZXZNUFtpXSk7CiAgICB9CiAgICBjb3V0IDw8IGFucyA8PCAiXG4iOwp9CgoKCmludCBtYWluKCkgewogICAgSGVucnkoKTsKICAgIGludCBxID0gMTsKICAgIGNpbiA+PiBxOwogICAgd2hpbGUgKHEtLSkgewogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICAgICAgcmV0dXJuIDA7Cn0KCg==