#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int LOG = 60;
int n;
ll k , a[maxN] , b[maxN];
struct node{
int nxt[2] , cnt;
node(){
cnt = 0;
nxt[0] = nxt[1] = -1;
}
};
vector<node> trie;
void ins(ll x){
int id = 0;
for (int i = LOG ; i >= 0 ; i--){
int c = ((x>>i)&1);
if (trie[id].nxt[c] == -1){
trie.push_back(node());
trie[id].nxt[c] = sz(trie) - 1;
}
id = trie[id].nxt[c];
trie[id].cnt++;
}
}
int nxt_id(int id , int c){
if (id == -1) return -1;
return trie[id].nxt[c];
}
int get_cnt(int id){
if (id == -1) return 0;
return trie[id].cnt;
}
int get(ll x , ll y){
int id = 0 , ans = 0;
for (int i = LOG ; i >= 0 ; i--){
int c = (((x ^ y)>>i)&1);
if ((y>>i)&1){
ans += get_cnt(nxt_id(id , c ^ 1));
}
id = nxt_id(id , c);
}
ans += get_cnt(id);
return ans;
}
bool check(ll x){
ll ans = 0;
for (int i = 1 ; i <= n ; i++){
ans += 1ll * get(b[i] , x);
}
return ans >= k;
}
int id[maxN];
void solve(){
cin >> n >> k;
trie.push_back(node());
for (int i = 1 ; i <= n ; i++){
cin >> a[i];
ins(a[i]);
}
for (int i = 1 ; i <= n ; i++){
cin >> b[i];
}
for (int i = 1 ; i <= n ; i++) id[i] = 0;
ll ans = 0;
for (int i = LOG ; i >= 0 ; i--){
ll num = 0;
for (int j = 1 ; j <= n ; j++){
int c = (b[j]>>i)&1;
num += 1ll * get_cnt(nxt_id(id[j] , c));
}
if (num >= k){
for (int j = 1 ; j <= n ; j++){
int c = (b[j]>>i)&1;
id[j] = nxt_id(id[j] , c);
}
}
else{
k -= num;
for (int j = 1 ; j <= n ; j++){
int c = (b[j]>>i)&1;
id[j] = nxt_id(id[j] , c ^ 1);
}
ans |= (1ll<<i);
}
}
cout << ans << "\n";
trie.clear();
}
#define name "I"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDJlNSkrNzsKY29uc3QgaW50IExPRyA9IDYwOwoKaW50IG47CmxsIGsgLCBhW21heE5dICwgYlttYXhOXTsKCgpzdHJ1Y3Qgbm9kZXsKICAgIGludCBueHRbMl0gLCBjbnQ7CgogICAgbm9kZSgpewogICAgICAgIGNudCA9IDA7CiAgICAgICAgbnh0WzBdID0gbnh0WzFdID0gLTE7CiAgICB9Cn07Cgp2ZWN0b3I8bm9kZT4gdHJpZTsKCnZvaWQgaW5zKGxsIHgpewogICAgaW50IGlkID0gMDsKICAgIGZvciAoaW50IGkgPSBMT0cgOyBpID49IDAgOyBpLS0pewogICAgICAgIGludCBjID0gKCh4Pj5pKSYxKTsKICAgICAgICBpZiAodHJpZVtpZF0ubnh0W2NdID09IC0xKXsKICAgICAgICAgICAgdHJpZS5wdXNoX2JhY2sobm9kZSgpKTsKICAgICAgICAgICAgdHJpZVtpZF0ubnh0W2NdID0gc3oodHJpZSkgLSAxOwogICAgICAgIH0KICAgICAgICBpZCA9IHRyaWVbaWRdLm54dFtjXTsKICAgICAgICB0cmllW2lkXS5jbnQrKzsKICAgIH0KfQoKaW50IG54dF9pZChpbnQgaWQgLCBpbnQgYyl7CiAgICBpZiAoaWQgPT0gLTEpIHJldHVybiAtMTsKICAgIHJldHVybiB0cmllW2lkXS5ueHRbY107Cn0KCmludCBnZXRfY250KGludCBpZCl7CiAgICBpZiAoaWQgPT0gLTEpIHJldHVybiAwOwogICAgcmV0dXJuIHRyaWVbaWRdLmNudDsKfQoKaW50IGdldChsbCB4ICwgbGwgeSl7CiAgICBpbnQgaWQgPSAwICwgYW5zID0gMDsKICAgIGZvciAoaW50IGkgPSBMT0cgOyBpID49IDAgOyBpLS0pewogICAgICAgIGludCBjID0gKCgoeCBeIHkpPj5pKSYxKTsKICAgICAgICBpZiAoKHk+PmkpJjEpewogICAgICAgICAgICBhbnMgKz0gZ2V0X2NudChueHRfaWQoaWQgLCBjIF4gMSkpOwogICAgICAgIH0KICAgICAgICBpZCA9IG54dF9pZChpZCAsIGMpOwogICAgfQogICAgYW5zICs9IGdldF9jbnQoaWQpOwogICAgcmV0dXJuIGFuczsKfQoKYm9vbCBjaGVjayhsbCB4KXsKICAgIGxsIGFucyA9IDA7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkrKyl7CiAgICAgICAgYW5zICs9IDFsbCAqIGdldChiW2ldICwgeCk7CiAgICB9CiAgICByZXR1cm4gYW5zID49IGs7Cn0KCmludCBpZFttYXhOXTsKCnZvaWQgc29sdmUoKXsKICAgIGNpbiA+PiBuID4+IGs7CiAgICB0cmllLnB1c2hfYmFjayhub2RlKCkpOwogICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKyspewogICAgICAgIGNpbiA+PiBhW2ldOwogICAgICAgIGlucyhhW2ldKTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKXsKICAgICAgICBjaW4gPj4gYltpXTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKSBpZFtpXSA9IDA7CiAgICBsbCBhbnMgPSAwOwogICAgZm9yIChpbnQgaSA9IExPRyA7IGkgPj0gMCA7IGktLSl7CiAgICAgICAgbGwgbnVtID0gMDsKICAgICAgICBmb3IgKGludCBqID0gMSA7IGogPD0gbiA7IGorKyl7CiAgICAgICAgICAgIGludCBjID0gKGJbal0+PmkpJjE7CiAgICAgICAgICAgIG51bSArPSAxbGwgKiBnZXRfY250KG54dF9pZChpZFtqXSAsIGMpKTsKICAgICAgICB9CiAgICAgICAgaWYgKG51bSA+PSBrKXsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDEgOyBqIDw9IG4gOyBqKyspewogICAgICAgICAgICAgICAgaW50IGMgPSAoYltqXT4+aSkmMTsKICAgICAgICAgICAgICAgIGlkW2pdID0gbnh0X2lkKGlkW2pdICwgYyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgayAtPSBudW07CiAgICAgICAgICAgIGZvciAoaW50IGogPSAxIDsgaiA8PSBuIDsgaisrKXsKICAgICAgICAgICAgICAgIGludCBjID0gKGJbal0+PmkpJjE7CiAgICAgICAgICAgICAgICBpZFtqXSA9IG54dF9pZChpZFtqXSAsIGMgXiAxKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhbnMgfD0gKDFsbDw8aSk7CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKICAgIHRyaWUuY2xlYXIoKTsKfQoKI2RlZmluZSBuYW1lICJJIgoKaW50IG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaWYgKGZvcGVuKG5hbWUiLklOUCIgLCAiciIpKXsKICAgICAgICBmcmVvcGVuKG5hbWUiLklOUCIgLCAiciIgLCBzdGRpbik7CiAgICAgICAgZnJlb3BlbihuYW1lIi5PVVQiICwgInciICwgc3Rkb3V0KTsKICAgIH0KICAgIGludCB0ID0gMTsgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSBzb2x2ZSgpOwogICAgcmV0dXJuIDA7Cn0KCg==