#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int __OneWan_2024 = [](){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();
struct Node {
int Lrt, Rrt;
int sum;
} tr[100005 << 5];
int tot;
int a[100005];
int update(int old, int L, int R, int pos) {
int now = ++tot;
tr[now] = tr[old];
tr[now].sum++;
if (L == R) {
return now;
}
int mid = L + R >> 1;
if (pos <= mid) {
tr[now].Lrt = update(tr[now].Lrt, L, mid, pos);
} else {
tr[now].Rrt = update(tr[now].Rrt, mid + 1, R, pos);
}
return now;
}
int query(int QL, int QR, int L, int R, int VL, int VR) {
if (VL > VR) {
return 0;
}
if (VL <= L && R <= VR) {
return tr[QR].sum - tr[QL].sum;
}
int mid = L + R >> 1;
int res = 0;
if (VL <= mid) {
res += query(tr[QL].Lrt, tr[QR].Lrt, L, mid, VL, VR);
}
if (VR >= mid + 1) {
res += query(tr[QL].Rrt, tr[QR].Rrt, mid + 1, R, VL, VR);
}
return res;
}
int rt[100005];
struct Result {
int L, R;
i64 res;
friend bool operator<(const Result &x, const Result &y) {
return x.L < y.L;
}
};
void solve() {
int n;
cin >> n;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
tot = 0;
for (int i = 1 ; i <= n ; i++) {
rt[i] = update(rt[i - 1], 1, n, a[i]);
}
multiset<i64> ans;
set<Result> st;
{
i64 res = 0;
for (int i = 1 ; i <= n ; i++) {
res += query(rt[0], rt[i], 1, n, a[i] + 1, n);
}
ans.insert(res);
st.insert({1, n, res});
}
for (int i = 1 ; i <= n ; i++) {
i64 t = 0;
if (!ans.empty()) {
t = *rbegin(ans);
}
cout << t << " ";
i64 tx;
cin >> tx;
tx ^= t;
int x = tx;
if (i == n) break;
auto it = st.upper_bound({x, x, -1});
if (it == begin(st)) {
continue;
}
it--;
auto [L, R, res] = *it;
st.erase(it);
ans.extract(res);
if (x - L <= R - x) {
i64 resL = 0;
for (int j = L ; j < x ; j++) { // 暴力计算左半部分的逆序对, 主席树快速计算左半对右半的贡献
resL += query(rt[L - 1], rt[j], 1, n, a[j] + 1, n);
res -= query(rt[x], rt[R], 1, n, 1, a[j] - 1);
}
res -= resL; // 减去左半部分的逆序对
res -= query(rt[L - 1], rt[x], 1, n, a[x] + 1, n); // 减去 x 对左半部分的逆序对
res -= query(rt[x], rt[R], 1, n, 1, a[x] - 1); // 减去 x 对右半部分的逆序对
// [L, x - 1]
if (L <= x - 1) {
st.insert({L, x - 1, resL});
ans.insert(resL);
}
// [x + 1, R]
if (x + 1 <= R) {
st.insert({x + 1, R, res});
ans.insert(res);
}
} else {
i64 resR = 0;
for (int j = x + 1 ; j <= R ; j++) { // 暴力计算右半部分的逆序对, 主席树快速计算右半对左半的贡献
resR += query(rt[j], rt[R], 1, n, 1, a[j] - 1);
res -= query(rt[L - 1], rt[x - 1], 1, n, a[j] + 1, n);
}
res -= resR; // 减去右半部分的逆序对
res -= query(rt[L - 1], rt[x], 1, n, a[x] + 1, n); // 减去 x 对左半部分的逆序对
res -= query(rt[x], rt[R], 1, n, 1, a[x] - 1); // 减去 x 对右半部分的逆序对
// [L, x - 1]
if (L <= x - 1) {
st.insert({L, x - 1, res});
ans.insert(res);
}
// [x + 1, R]
if (x + 1 <= R) {
st.insert({x + 1, R, resR});
ans.insert(resR);
}
}
}
cout << "\n";
}
int main() {
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}