constint N = 1e5 + 10; int n,ans; int arr[N],brr[N];
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inlineboolcheck(int x){ vector<int> a,b; for (re int i = 1;i <= n;i++){ a.push_back(arr[i] & x); b.push_back((brr[i] & x) ^ x); } sort(a.begin(),a.end()); sort(b.begin(),b.end()); return (a == b); }
inlinevoidsolve(){ ans = 0; n = read(); for (re int i = 1;i <= n;i++) arr[i] = read(); for (re int i = 1;i <= n;i++) brr[i] = read(); for (re int i = 30;~i;i--) ans |= (check(ans | (1 << i)) << i); printf("%d\n",ans); }
intmain(){ int T; T = read(); while (T--) solve(); return0; }