Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner.

Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner.

Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it?

------ Input Format ------ 

- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- Each test case contains three lines of input.
- The first line contains a single integer N - the number of cards.
- The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards
- The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards

------ Output Format ------ 

For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it.

------ Constraints ------ 

$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} ≤ 2^{30} - 1$
$0 ≤ B_{i} ≤ 2^{30} - 1$
- Sum of $N$ over all testcases does not exceeds $10^{5}$.

----- Sample Input 1 ------ 
3
3
4 6 8
2 1 2
3
0 2 0
2 0 8
3
1 2 1
2 3 6
----- Sample Output 1 ------ 
2 2
0 0
2 2
----- explanation 1 ------ 
Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$.

Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.

Source: codechef | Difficulty: HARD
Tags: Bit manipulation

Mathematics

Greedy algorithms


Solutions:
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	vis = set()
	ans = 0
	for i in range(30, -1, -1):
		f = 1
		h = []
		for j in range(n):
			if j in vis:
				if 1 << i & b[j] == 0:
					f = 0
			elif a[j] & 1 << i == 0:
				if b[j] & 1 << i and b[j] & ans == ans:
					h.append(j)
				else:
					f = 0
		if f:
			ans += 1 << i
			for j in h:
				vis.add(j)
	print(ans, len(vis))


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	flip = set()
	band = 0
	mbits = 0
	for i in range(N):
		mbits = max(mbits, len(bin(A[i])[2:]), len(bin(B[i])[2:]))
	for bit in range(mbits, 0, -1):
		tbt = 2 ** (bit - 1)
		rflip = set()
		aflip = set()
		for i in range(N):
			if i in flip:
				if B[i] & tbt == 0:
					if A[i] & tbt != 0 and (band == 0 or A[i] & band == band):
						rflip.add(i)
					else:
						break
			elif A[i] & tbt == 0:
				if B[i] & tbt != 0 and (band == 0 or B[i] & band == band):
					aflip.add(i)
				else:
					break
		else:
			band += tbt
			for i in aflip:
				flip.add(i)
			for i in rflip:
				flip.remove(i)
	print(str(band) + ' ' + str(len(flip)))


T = int(input())

def check(bit, setBits, array):
	for i in range(N):
		here = 0
		if front[i] == float('inf') or front[i] & 1 << bit == 0:
			here = here + 1
		if back[i] == float('inf') or back[i] & 1 << bit == 0:
			here = here + 1
		if here == 2:
			return False
	for i in range(N):
		if front[i] != float('inf') and front[i] & 1 << bit == 0:
			front[i] = float('inf')
		if back[i] != float('inf') and back[i] & 1 << bit == 0:
			back[i] = float('inf')
	return True
for _ in range(T):
	N = int(input())
	front = list(map(int, input().split()))
	back = list(map(int, input().split()))
	array = []
	bit = 32
	ans = 0
	setBits = set()
	flips = 0
	array = [0 for i in range(N)]
	while bit >= 0:
		value = check(bit, setBits, array)
		if value == True:
			ans = ans + 2 ** bit
		bit = bit - 1
	count = 0
	for i in range(len(front)):
		if front[i] == float('inf'):
			count = count + 1
	print(ans, end=' ')
	print(count)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	state = [-1] * n
	bit = 1 << 29
	while bit > 0:
		poss = True
		for i in range(n):
			if state[i] == 0 and (not a[i] & bit):
				poss = False
			elif state[i] == 1 and (not b[i] & bit):
				poss = False
			elif not a[i] & bit and (not b[i] & bit):
				poss = False
		if not poss:
			bit >>= 1
			continue
		for i in range(n):
			if state[i] != -1:
				continue
			if not a[i] & bit:
				state[i] = 1
			elif not b[i] & bit:
				state[i] = 0
		bit >>= 1
	ans = (1 << 30) - 1
	flip = 0
	for i in range(n):
		if state[i] == 1:
			flip += 1
			ans &= b[i]
		else:
			ans &= a[i]
	print(str(ans) + ' ' + str(flip))
t = int(input())
while t:
	solve()
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	st = [-1] * n
	ans = 0
	for i in range(29, -1, -1):
		ps = 1
		bit = 1 << i
		for i in range(n):
			if st[i] == 0 and a[i] & bit == 0:
				ps = 0
				break
			elif st[i] == 1 and b[i] & bit == 0:
				ps = 0
				break
			elif st[i] == -1 and b[i] & bit == 0 and (a[i] & bit == 0):
				ps = 0
				break
		if ps:
			ans += bit
			for i in range(n):
				if st[i] == 1 or st[i] == 0:
					continue
				elif a[i] & bit and b[i] & bit:
					continue
				elif a[i] & bit:
					st[i] = 0
				else:
					st[i] = 1
	print(ans, st.count(1))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	res = [-1 for i in range(n)]
	bcount = 0
	abin = [bin(i) for i in a]
	bbin = [bin(i) for i in b]
	for i in reversed(range(30)):
		currentrow = [-1 for k in range(n)]
		usable = True
		for j in range(n):
			if res[j] == -1:
				aval = a[j] >> i & 1
				bval = b[j] >> i & 1
				if aval == 0 and bval == 0:
					usable = False
					break
				if aval == 1 and bval == 1:
					continue
				elif aval == 1:
					currentrow[j] = 0
				else:
					currentrow[j] = 1
			else:
				aval = a[j] >> i & 1
				bval = b[j] >> i & 1
				if res[j] == 0:
					if aval == 0:
						usable = False
				elif res[j] == 1:
					if bval == 0:
						usable = False
		if usable:
			for l in range(n):
				if res[l] == -1:
					res[l] = currentrow[l]
	start = 2 ** 30 - 1
	for i in range(n):
		if res[i] == 0 or res[i] == -1:
			start &= a[i]
		else:
			start &= b[i]
			bcount += 1
	print(start, bcount)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	pos = [-1] * n
	x = 1 << 29
	while x:
		poss = True
		for y in range(n):
			if pos[y] == 0 and (not a[y] & x):
				poss = False
			elif pos[y] == 1 and (not b[y] & x):
				poss = False
			elif not a[y] & x and (not b[y] & x):
				poss = False
		for y in range(n):
			if poss:
				if not a[y] & x:
					pos[y] = 1
				elif not b[y] & x:
					pos[y] = 0
		x >>= 1
	ans = 0
	anf = (1 << 30) - 1
	for x in range(n):
		if pos[x] == -1 or pos[x] == 0:
			anf = anf & a[x]
		elif pos[x] == 1:
			anf &= b[x]
		ans += pos[x] == 1
	print(anf, ans)


t = int(input())

def log(n):
	x = str(bin(n))[2:]
	return len(x) - 1

def check(n, m):
	x = str(bin(n))[2:]
	if m < len(x):
		return x[-m - 1] == '1'
	return False
for _ in range(t):
	n = int(input())
	f = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mix = [[f[i], b[i]] for i in range(n)]
	m = log(max(f + b))
	val = 0
	flips = 0
	ind = []
	while m >= 0:
		av = True
		tempMix = mix[:]
		tempFlips = flips
		for (i, val) in enumerate(mix):
			if len(val) == 2:
				x = check(val[0], m)
				y = check(val[1], m)
				if not (x or y):
					av = False
					break
				elif x and (not y):
					tempMix[i] = [val[0]]
				elif y and (not x):
					tempFlips += 1
					tempMix[i] = [val[1]]
			elif not check(val[0], m):
				av = False
				break
		if av:
			mix = tempMix
			flips = tempFlips
			ind.append(m)
		m -= 1
	s = 0
	for i in ind:
		s += 2 ** i
	print(s, flips)


t = int(input())
for z in range(t):
	n = int(input())
	state = [-1] * n
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = 1 << 29
	while x > 0:
		chng = True
		x = x // 2
		for i in range(n):
			if state[i] == 0 and (not a[i] & x):
				chng = False
			elif state[i] == 1 and (not b[i] & x):
				chng = False
			elif not a[i] & x and (not b[i] & x):
				chng = False
		if not chng:
			continue
		for i in range(n):
			if state[i] != -1:
				continue
			if not a[i] & x:
				state[i] = 1
			elif not b[i] & x:
				state[i] = 0
	ans = (1 << 30) - 1
	count = 0
	for i in range(n):
		if state[i] == 1:
			count += 1
			ans &= b[i]
		else:
			ans &= a[i]
	print(ans, count)


for tcase in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(p2, flip, canflip) = (2 ** 29, [0] * n, [True] * n)
	result = 0
	while p2 > 0:
		(i, x) = (0, p2)
		while x == p2 and i < n:
			if canflip[i]:
				x = p2 & (a[i] | b[i])
			else:
				x = p2 & a[i]
			i += 1
		if x == p2:
			result += p2
			for i in range(n):
				canflip[i] = canflip[i] and p2 & b[i] == p2
				if canflip[i] and p2 & a[i] == 0:
					(a[i], b[i]) = (b[i], a[i])
					flip[i] = 1
					canflip[i] = False
		p2 //= 2
	print(result, sum(flip))


t = int(input())
for xyz in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	m = max(max(A), max(B))
	b = 0
	while m > 1:
		m = m // 2
		b += 1
	ans = 0
	flips = 0
	while b >= 0:
		f = True
		for i in range(n):
			if (A[i] >> b & 1 == 0 or ans & A[i] != ans) and (B[i] == None or B[i] >> b & 1 == 0 or ans & B[i] != ans):
				f = False
				break
		if f:
			cur = 0
			for i in range(n):
				if A[i] >> b & 1 == 0 and B[i] != None:
					A[i] = B[i]
					B[i] = None
					cur += 1
			flips += cur
			ans += 1 << b
		b -= 1
	print(ans, flips)


import sys
from bisect import bisect_right as lb
from collections import deque
from queue import PriorityQueue as pq
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
lii = lambda : list(map(int, list(ip())))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
for _ in range(ii()):
	n = ii()
	a = il()
	b = il()
	ans = 0
	t = 0
	for i in range(29, -1, -1):
		x = 2 ** i
		fl = 1
		y = []
		for j in range(n):
			if a[j] & x == x:
				continue
			if b[j] & x == x and b[j] & ans == ans:
				y.append(j)
				continue
			else:
				fl = 0
				break
		if fl:
			ans |= x
			for j in y:
				a[j] = b[j]
				t += 1
	print(ans, t)


import sys
import bisect
input = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
sys.setrecursionlimit(10 ** 6)
flsh = lambda : sys.stdout.flush()
sa = lambda : input()
sb = lambda : int(input())
sc = lambda : input().split()
sd = lambda : list(map(int, input().split()))
se = lambda : float(input())
sf = lambda : list(input())
mod = 10 ** 9 + 7

def hnbhai():
	n = sb()
	a = sd()
	b = sd()
	ans = 0
	tot = 0
	for i in range(29, -1, -1):
		bit = 2 ** i
		x = 0
		f = 0
		temp = []
		for j in range(n):
			if a[j] & bit == bit:
				continue
			if b[j] & bit == bit and b[j] & ans == ans:
				x += 1
				temp.append(j)
				continue
			f = 1
			break
		if f:
			continue
		ans |= bit
		for j in temp:
			a[j] = b[j]
			tot += 1
	print(ans, tot)
for _ in range(sb()):
	hnbhai()


T = int(input())
while T != 0:
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	m = max(max(A), max(B))
	index = 0
	while m != 1:
		index += 1
		m //= 2
	ans = (1 << index + 1) - 1
	state = [-1 for t in range(n)]
	i = 1 << index
	while i > 0:
		poss = True
		for j in range(n):
			if state[j] == 1 and (not A[j] & i):
				poss = False
				break
			elif state[j] == 0 and (not B[j] & i):
				poss = False
				break
			elif not A[j] & i and (not B[j] & i):
				poss = False
				break
		if poss:
			for k in range(n):
				if state[k] != -1:
					continue
				if not A[k] & i:
					state[k] = 0
				elif not B[k] & i:
					state[k] = 1
		i >>= 1
	flip = 0
	for p in range(n):
		if state[p] == 0:
			flip += 1
			ans &= B[p]
		else:
			ans &= A[p]
	print(ans, flip, sep=' ')
	T -= 1


for tc in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	lk = list(map(int, input().split()))
	comb = [-1] * len(ls)
	res = []
	for r in range(30, -1, -1):
		pos = True
		for x in range(len(ls)):
			if comb[x] == 0 and ls[x] >> r & 1 == 0:
				pos = False
			elif comb[x] == 1 and lk[x] >> r & 1 == 0:
				pos = False
			elif ls[x] >> r & 1 == 0 and lk[x] >> r & 1 == 0:
				pos = False
		if pos == False:
			continue
		for x in range(len(ls)):
			if comb[x] != -1:
				pass
			elif ls[x] >> r & 1 == 0:
				comb[x] = 1
			elif lk[x] >> r & 1 == 0:
				comb[x] = 0
	flips = 0
	an = -1
	for x in range(len(ls)):
		if comb[x] == 1:
			flips += 1
			if an == -1:
				an = lk[x]
			else:
				an &= lk[x]
		elif an == -1:
			an = ls[x]
		else:
			an &= ls[x]
	print(an, flips)


def intinp():
	return int(input())

def mapinp():
	return map(int, list(input().split()))
T = intinp()
for _ in range(T):
	N = intinp()
	A = list(mapinp())
	B = list(mapinp())
	state = [0] * N
	flipped = [0] * N
	for i in range(29, -1, -1):
		flip = True
		for j in range(N):
			if A[j] & 1 << i == 0:
				if B[j] & 1 << i != 0 and state[j] == 0:
					state[j] = 2
				else:
					flip = False
					break
			elif B[j] & 1 << i == 0 and state[j] == 0:
				state[j] = 3
		for k in range(N):
			if flip and state[k] == 2:
				temp = A[k]
				A[k] = B[k]
				B[k] = temp
				flipped[k] = 1
				state[k] = 1
			if flip and state[k] == 3:
				state[k] = 1
			if not flip and (state[k] == 2 or state[k] == 3):
				state[k] = 0
	bitwiseAnd = A[0]
	numFlip = 0
	for i in range(N):
		bitwiseAnd = bitwiseAnd & A[i]
		if flipped[i] == 1:
			numFlip += 1
	print(bitwiseAnd, end=' ')
	print(numFlip)


import sys

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
from collections import defaultdict
for _ in range(*maps()):
	(n,) = maps()
	a = [*maps()]
	b = [*maps()]
	st = [-1] * n
	ans = (1 << 30) - 1
	bit = 1 << 30
	while bit:
		ok = True
		for i in range(n):
			if st[i] == 0 and (not a[i] & bit):
				ok = False
			elif st[i] == 1 and (not b[i] & bit):
				ok = False
			elif not a[i] & bit and (not b[i] & bit):
				ok = False
		if ok:
			for i in range(n):
				if st[i] != -1:
					continue
				if not a[i] & bit:
					st[i] = 1
				elif not b[i] & bit:
					st[i] = 0
		bit >>= 1
	flips = 0
	for i in range(n):
		if st[i] == 1:
			ans &= b[i]
			flips += 1
		else:
			ans &= a[i]
	if ans == (1 << 30) - 1:
		print(0, 0)
		continue
	print(ans, flips)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	state = [-1] * n
	bit = 1 << 29
	while bit > 0:
		poss = True
		for j in range(n):
			if state[j] == 0 and (not a[j] & bit):
				poss = False
			elif state[j] == 1 and (not b[j] & bit):
				poss = False
			elif not a[j] & bit and (not b[j] & bit):
				poss = False
		temp = bit
		bit >>= 1
		if not poss:
			continue
		for i in range(n):
			if state[i] != -1:
				continue
			if not a[i] & temp:
				state[i] = 1
			elif not b[i] & temp:
				state[i] = 0
	ans = (1 << 30) - 1
	flips = 0
	for i in range(n):
		if state[i] == 1:
			flips += 1
			ans &= b[i]
		else:
			ans &= a[i]
	print(str(ans) + ' ' + str(flips))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	flips = 0
	decided = [-1] * n
	for j in range(29, -1, -1):
		possible = True
		make_decided = [-1] * n
		for i in range(n):
			if decided[i] != -1:
				if decided[i] == 0 and a[i] & 1 << j == 1 << j:
					continue
				elif decided[i] == 1 and b[i] & 1 << j == 1 << j:
					continue
				else:
					possible = False
					break
			elif a[i] & 1 << j != 1 << j and b[i] & 1 << j != 1 << j:
				possible = False
				break
			elif a[i] & 1 << j == 1 << j and b[i] & 1 << j == 1 << j:
				pass
			elif b[i] & 1 << j == 1 << j:
				make_decided[i] = 1
			elif a[i] & 1 << j == 1 << j:
				make_decided[i] = 0
			else:
				possible = False
				break
		if possible:
			for k in range(n):
				if make_decided[k] != -1:
					decided[k] = make_decided[k]
			ans = ans * 10 + 1
		else:
			ans = ans * 10
	anss = 0
	ans = str(ans)
	n_ans = len(ans)
	for i in range(n_ans - 1, -1, -1):
		if ans[i] == '1':
			anss += 1 << n_ans - i - 1
	print(anss, end=' ')
	for i in range(n):
		if decided[i] == 1:
			flips += 1
	print(flips)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	st = [-1] * n
	for k in range(29, -1, -1):
		poss = 1
		for i in range(n):
			if st[i] == 0:
				if not 1 << k & a[i]:
					poss = 0
					break
			elif st[i] == 1:
				if not 1 << k & b[i]:
					poss = 0
					break
			elif not 1 << k & a[i] and (not 1 << k & b[i]):
				poss = 0
				break
		if not poss:
			continue
		else:
			ans += 1 << k
			for i in range(n):
				if st[i] != -1:
					continue
				elif not (a[i] & 1 << k and b[i] & 1 << k):
					if a[i] & 1 << k:
						st[i] = 0
					else:
						st[i] = 1
	print(ans, st.count(1))


import sys
import math
import heapq
from collections import defaultdict as dd
from collections import OrderedDict as od
from collections import deque
from itertools import permutations as pp
from itertools import combinations as cc
from sys import stdin
from functools import cmp_to_key
from functools import lru_cache
from bisect import bisect_left as bs_l
from bisect import bisect_right as bs_r
input = sys.stdin.readline
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	d = dd(int)
	ans = 0
	c = 0
	for i in range(30, -1, -1):
		ok = 1
		flip = []
		fl = []
		for j in range(n):
			if 1 << i & arr[j] and 1 << i & brr[j]:
				continue
			elif 1 << i & arr[j] and 1 << i & brr[j] == 0:
				fl.append(j)
			elif 1 << i & arr[j] == 0 and 1 << i & brr[j]:
				if d[j]:
					ok = 0
					break
				else:
					flip.append(j)
			else:
				ok = 0
				break
		if ok:
			for k in flip:
				if d[k]:
					ok = 0
					break
				else:
					(arr[k], brr[k]) = (brr[k], arr[k])
					d[k] += 1
					c += 1
			for k in fl:
				d[k] += 1
			ans |= 1 << i
		else:
			ans |= 0
	print(ans, c)


def func():
	turn = [-1 for x in range(n)]
	for req in range(31, -1, -1):
		flag = False
		changed = [False for x in range(n)]
		swapped = [False for x in range(n)]
		curr = 1 << req
		for j in range(n):
			if turn[j] != -1:
				if curr & a[j] == 0:
					flag = True
					break
				continue
			if curr & a[j] and curr & b[j]:
				continue
			elif curr & a[j] and curr & b[j] == 0:
				turn[j] = 0
				changed[j] = True
			elif curr & a[j] == 0 and curr & b[j]:
				turn[j] = 1
				(a[j], b[j]) = (b[j], a[j])
				swapped[j] = True
				changed[j] = True
			else:
				flag = True
				break
		if flag:
			for j in range(n):
				if changed[j]:
					turn[j] = -1
				if swapped[j]:
					(a[j], b[j]) = (b[j], a[j])
	calc = (1 << 31) - 1
	ans = 0
	for j in range(n):
		if turn[j] == 1:
			ans += 1
		calc = calc & a[j]
	print(calc, ans)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	func()


for _ in range(int(input())):
	n = int(input())
	ai = list(map(int, input().split()))
	bi = list(map(int, input().split()))
	fi = [1] * n
	for i in range(n):
		x = str(bin(ai[i])[2:])
		ai[i] = '0' * (32 - len(x)) + x
		x = str(bin(bi[i])[2:])
		bi[i] = '0' * (32 - len(x)) + x
	fl = 0
	for i in range(32):
		f = 1
		for j in range(n):
			if ai[j][i] != '1':
				if fi[j] == 1:
					if bi[j][i] != '1':
						f = 0
						break
				else:
					f = 0
					break
		if f == 1:
			for j in range(n):
				if ai[j][i] != '1':
					(ai[j], bi[j]) = (bi[j], ai[j])
					fi[j] = 0
					fl = fl + 1
				elif bi[j][i] != '1':
					fi[j] = -1
	an = int(ai[0], 2)
	for ele in ai:
		an = int(ele, 2) & an
	print(an, fl)


INF = pow(2, 31) - 1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	state = [-1] * n
	for i in range(31, -1, -1):
		temp = 1 << i
		possible = True
		for i in range(n):
			if state[i] == 0 and a[i] & temp == 0:
				possible = False
			elif state[i] == 1 and b[i] & temp == 0:
				possible = False
			elif a[i] & temp == 0 and b[i] & temp == 0:
				possible = False
		if possible:
			for i in range(n):
				if state[i] == -1:
					if a[i] & temp == 0:
						state[i] = 1
					elif b[i] & temp == 0:
						state[i] = 0
	ans = INF
	moves = 0
	for i in range(n):
		if state[i] == 1:
			ans &= b[i]
			moves += 1
		else:
			ans &= a[i]
	print(ans, moves)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	flip = [-1] * N
	for i in range(30, -1, -1):
		pos = True
		for j in range(N):
			if flip[j] == 0 and (not A[j] & 1 << i):
				pos = False
				break
			elif flip[j] == 1 and (not B[j] & 1 << i):
				pos = False
				break
			elif flip[j] == -1 and (not A[j] & 1 << i and (not B[j] & 1 << i)):
				pos = False
				break
		if not pos:
			continue
		for j in range(N):
			if flip[j] == -1:
				if A[j] & 1 << i and B[j] & 1 << i:
					continue
				elif A[j] & 1 << i:
					flip[j] = 0
				elif B[j] & 1 << i:
					flip[j] = 1
	count = 0
	if flip[0] == 0 or flip[0] == -1:
		bit = A[0]
	else:
		bit = B[0]
		count += 1
	for i in range(1, N):
		if flip[i] == 0 or flip[i] == -1:
			bit = A[i] & bit
		else:
			bit = B[i] & bit
			count += 1
	print(bit, count)


from heapq import heappop, heappush
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	state = [-1] * n
	bit = 1 << 29
	while bit > 0:
		poss = True
		for i in range(n):
			if state[i] == 0 and (not a[i] & bit):
				poss = False
			elif state[i] == 1 and (not b[i] & bit):
				poss = False
			elif not a[i] & bit and (not b[i] & bit):
				poss = False
		if not poss:
			bit >>= 1
			continue
		for i in range(n):
			if state[i] != -1:
				continue
			if not a[i] & bit:
				state[i] = 1
			elif not b[i] & bit:
				state[i] = 0
		bit >>= 1
	ans = (1 << 30) - 1
	flip = 0
	for i in range(n):
		if state[i] == 1:
			flip += 1
			ans &= b[i]
		else:
			ans &= a[i]
	print(str(ans) + ' ' + str(flip))


import math, os, io, heapq
from sys import stdin, stdout
from collections import Counter, deque, defaultdict
from itertools import product
import bisect

def getList(method=int):
	return list(map(method, input().split()))

def getInt():
	return int(input())

def solve():
	n = getInt()
	(a, b) = (getList(), getList())
	(bda, ans) = ([-1] * n, 0)
	for bit in range(29, -1, -1):
		mp = dict()
		possible = True
		for i in range(n):
			if bda[i] != -1:
				arr = b if bda[i] else a
				if arr[i] & 1 << bit == 0:
					possible = False
					break
			(amatch, bmatch) = (a[i] & 1 << bit > 0, b[i] & 1 << bit > 0)
			if not (amatch or bmatch):
				possible = False
				break
			if amatch and bmatch:
				continue
			mp[i] = 0 if amatch else 1
		if possible:
			ans ^= 1 << bit
			for i in mp:
				bda[i] = mp[i]
	print(ans, bda.count(1))
for _ in range(getInt()):
	solve()


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	state = [-1] * n
	for i in range(30, -1, -1):
		bit = 2 ** i
		is_bit_set = True
		for j in range(n):
			if a[j] & bit == 0 and b[j] & bit == 0:
				is_bit_set = False
			if a[j] & bit == 0 and state[j] == 0:
				is_bit_set = False
			if b[j] & bit == 0 and state[j] == 1:
				is_bit_set = False
		if is_bit_set:
			ans += bit
			for j in range(n):
				if a[j] & bit == 0:
					state[j] = 1
				if b[j] & bit == 0:
					state[j] = 0
	flip = 0
	for i in range(n):
		if state[i] == 1:
			flip += 1
	print(ans, flip)
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	totaland = 2 ** 32
	flag = 0
	overallflip = []
	maxi = 0
	while totaland > 0:
		localflip = []
		j = 0
		while j < n:
			x = a[j] & totaland
			y = b[j] & totaland
			if x == 0 and y == 0:
				break
			elif x == 0:
				localflip.append(1)
			elif y == 0:
				localflip.append(0)
			else:
				localflip.append(2)
			j += 1
		if j == n:
			if overallflip == []:
				overallflip = localflip
				maxi = totaland
			else:
				k = 0
				temp = []
				while k < n:
					if localflip[k] == 2:
						temp.append(overallflip[k])
					elif overallflip[k] != localflip[k] and overallflip[k] == 2:
						temp.append(localflip[k])
					elif overallflip[k] == localflip[k]:
						temp.append(localflip[k])
						k += 1
						continue
					else:
						break
					k += 1
				if k == n:
					overallflip = temp
					maxi += totaland
		totaland >>= 1
	if overallflip == []:
		print(0, 0)
	else:
		print(maxi, overallflip.count(1))


from bisect import *
from collections import *
from sys import stdin, stdout
from queue import *
from itertools import *
from heapq import *
from random import *
from statistics import *
from math import *
from re import *
import array
import operator
from copy import *
lines = stdin.readlines()
out = stdout.write
lines.reverse()

def input():
	return lines.pop().strip('\n')

def readIntList():
	return list(map(int, input().split()))

def readStrList():
	return list(map(str, input().split()))

def binaryarr(arr):
	ans = []
	for i in arr:
		k = bin(i)[2:]
		l = '0' * (30 - len(k)) + k
		ans.append(l)
	return ans

def mark(vis, bs, i):
	ans = True
	for j in range(i):
		if vis[j] == 1 and bs[j] == '0':
			ans = False
			break
	return ans
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a1 = binaryarr(a)
	b1 = binaryarr(b)
	vis = [0] * 30
	curr = 0
	minr = 0
	rep = [0] * (n + 1)
	for i in range(30):
		r1 = 0
		ann = []
		for j in a1:
			if j[i] == '1':
				r1 += 1
				ann.append(1)
			else:
				ann.append(0)
		r2 = 0
		for j in range(len(b1)):
			if rep[j] == 1:
				ann[j] = int(b1[j][i])
				continue
			if b1[j][i] == '1' and mark(vis, b1[j], i):
				ann[j] = 1
				r2 += 1
		if r1 == n:
			vis[i] = 1
		elif len(set(ann)) == 1 and ann[0] == 1:
			arr = []
			brr = []
			for j in range(n):
				if a1[j][i] == '1':
					arr.append(a1[j])
				else:
					arr.append(b1[j])
					rep[j] = 1
			a1 = arr[:]
			vis[i] = 1
	answer = 0
	for i in range(30):
		answer += vis[i] * 2 ** (29 - i)
	print(answer, rep.count(1))


from collections import Counter
import string
import math
import bisect
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
testcases = vary(1)
for _ in range(testcases):
	n = vary(1)
	num = array_int()
	num2 = array_int()
	ans = 0
	visited = [0] * n
	value = 0
	lastset = []
	for i in range(32, -1, -1):
		new = 1
		ct = 0
		pre = []
		maxi = -1
		pos = 0
		flag = 0
		flag2 = 0
		maxi2 = -1
		for j in range(n):
			if num[j] & 1 << i:
				new = 1
			else:
				for kt in lastset:
					if num2[j] & 1 << kt:
						continue
					else:
						flag = 1
						new = 0
						break
				if flag == 1:
					ct = 0
					break
				if num2[j] & 1 << i and visited[j] == 0:
					new = 1
					ct += 1
					visited[j] = 1
					(num[j], num2[j]) = (num2[j], num[j])
					pre.append(j)
				else:
					ct = 0
					new = 0
					flag = 1
					break
		if flag == 1:
			for k in pre:
				visited[k] = 0
				(num[k], num2[k]) = (num2[k], num[k])
		if new == 1:
			lastset.append(i)
		ans += ct
		value += 2 ** i if new == 1 else 0
	print(value, ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mx = max(max(a), max(b))
	l = len(bin(mx).replace('0b', ''))
	count = [3] * n
	flag = False
	for i in range(l, -1, -1):
		if not flag:
			count = [3] * n
		temp = count[:]
		cnt = 0
		for j in range(n):
			if count[j] == 1:
				if a[j] & 1 << i == 0:
					count[j] = 0
			elif count[j] == 2:
				if b[j] & 1 << i == 0:
					count[j] = 0
			elif count[j] == 3:
				if a[j] & 1 << i == 0 and b[j] & 1 << i == 0:
					count[j] = 0
				elif a[j] & 1 << i == 0:
					count[j] = 2
				elif b[j] & 1 << i == 0:
					count[j] = 1
			if count[j] == 0:
				cnt += 1
		if cnt == 0:
			flag = True
		elif flag:
			count = temp[:]
	ans = []
	cnt = 0
	for i in range(n):
		if count[i] == 1:
			ans.append(a[i])
		elif count[i] == 2:
			ans.append(b[i])
			cnt += 1
		elif count[i] == 3:
			ans.append(a[i])
	if len(ans) < n:
		print(0, 0)
	else:
		nd = ans[0]
		for i in ans:
			nd = nd & i
		print(nd, cnt)


mod = 1000000007
from sys import stdin, stdout
from collections import defaultdict, Counter, deque
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	abcd = []
	flips = 0
	for i in range(32, -1, -1):
		shan = 1 << i
		count1 = 0
		count2 = 0
		mps = []
		for j in range(n):
			if shan & a[j]:
				count1 += 1
			elif shan & b[j]:
				count2 += 1
				mps.append(j)
			else:
				break
		if count1 == n:
			abcd.append(shan)
		elif count2 + count1 == n:
			pos = 1
			for e in mps:
				v = 0
				if len(abcd) > 0:
					for q in abcd:
						if q & b[e]:
							v += 1
					if v != len(abcd):
						pos = 0
						break
			if pos:
				abcd.append(shan)
				for e in mps:
					flips += 1
					(a[e], b[e]) = (b[e], a[e])
	answer = a[0]
	for i in range(1, n):
		answer &= a[i]
	print(answer, flips)


mod = 1000000007
from sys import stdin, stdout

def srt(n, l, r):
	m = (l + r) // 2
	if l > r:
		return m
	if m * m > n:
		r = m - 1
		return srt(n, l, r)
	elif m * m < n:
		l = m + 1
		return srt(n, l, r)
	else:
		return m
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	siri = []
	flips = 0
	for i in range(32, -1, -1):
		shan = 1 << i
		count1 = 0
		count2 = 0
		mps = []
		for j in range(n):
			if shan & a[j]:
				count1 += 1
			elif shan & b[j]:
				count2 += 1
				mps.append(j)
			else:
				break
		if count1 == n:
			siri.append(shan)
		elif count2 + count1 == n:
			pos = 1
			for e in mps:
				v = 0
				if len(siri) > 0:
					for q in siri:
						if q & b[e]:
							v += 1
					if v != len(siri):
						pos = 0
						break
			if pos:
				siri.append(shan)
				for e in mps:
					flips += 1
					(a[e], b[e]) = (b[e], a[e])
	sreeshanth = a[0]
	for i in range(1, n):
		sreeshanth &= a[i]
	print(sreeshanth, flips)


def main():
	n = int(input())
	a1 = list(map(int, input().split()))
	b1 = list(map(int, input().split()))
	m = len(bin(max(max(a1), max(b1)))[2:])
	arr = [3] * n
	ans = 0
	for j in range(m - 1, -1, -1):
		b2 = pow(2, j)
		f = True
		for i in range(n):
			if arr[i] == 3 and b2 & (a1[i] | b1[i]) == 0:
				f = False
				break
			if arr[i] == 1 and b2 & a1[i] == 0:
				f = False
				break
			if arr[i] == 0 and b2 & b1[i] == 0:
				f = False
				break
		if f == True:
			ans += b2
			for i in range(n):
				first = a1[i] & b2
				second = b1[i] & b2
				if arr[i] == 3 and first and second:
					continue
				if arr[i] == 3 and first:
					arr[i] = 1
				if arr[i] == 3 and second:
					arr[i] = 0
	count = 0
	for i in range(n):
		if arr[i] == 0:
			count += 1
	print(ans, count)
t = 1
t = int(input())
for i in range(t):
	main()


import sys
from heapq import heapify, heappush, heappop

def get(flips, undecided, idx, a_str, b_str, n):
	for i in range(n):
		if i in undecided:
			if a_str[i][idx] != '1' and b_str[i][idx] != '1':
				return '0'
		elif i in flips:
			if b_str[i][idx] != '1':
				return '0'
		elif a_str[i][idx] != '1':
			return '0'
	return '1'
for _ in range(int(sys.stdin.readline().strip())):
	n = int(sys.stdin.readline().strip())
	a = [int(x) for x in sys.stdin.readline().strip().split(' ')]
	b = [int(x) for x in sys.stdin.readline().strip().split(' ')]
	a_str = []
	b_str = []
	ans = int('1' * 32, 2)
	max_len = 0
	undecided = set()
	flips = set()
	for i in range(n):
		a_str.append(bin(a[i])[2:])
		b_str.append(bin(b[i])[2:])
		undecided.add(i)
		max_len = max(max_len, len(a_str[i]), len(b_str[i]))
	for i in range(n):
		a_str[i] = '0' * (max_len - len(a_str[i])) + a_str[i]
		b_str[i] = '0' * (max_len - len(b_str[i])) + b_str[i]
	for idx in range(max_len):
		if get(flips, undecided, idx, a_str, b_str, n) == '1':
			for i in range(n):
				if i in undecided:
					if a_str[i][idx] == '1' and b_str[i][idx] == '0':
						undecided.remove(i)
					elif a_str[i][idx] == '0' and b_str[i][idx] == '1':
						flips.add(i)
						undecided.remove(i)
	for i in range(n):
		if i in flips:
			ans &= b[i]
		else:
			ans &= a[i]
	sys.stdout.write(str(ans) + ' ' + str(len(flips)) + '\n')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def check(arr, lex, size):
	j = 0
	ans = 0
	k = 0
	rem = 0
	while j < len(arr):
		if k == 0:
			rem = j
		if k < size:
			ans = gcd(ans, arr[j])
			if ans < lex:
				j = rem + 1
				ans = 0
				k = 0
				continue
			j += 1
			k += 1
		if k == size:
			return True
	else:
		if k == size and ans >= lex:
			return True
		return False

def bsearch(a):
	k = 2
	left = 1
	right = len(a)
	ans = 0
	while left <= right:
		mid = left + (right - left) // 2
		if check(a, k, mid):
			ans = mid
			left = mid + 1
		else:
			right = mid - 1
	return ans
mod = 10 ** 9 + 7
for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = max(max(a), max(b))
	x = bin(c)[2:]
	m = len(x)
	arr = [2] * n
	ans = [0] * m
	count = 0
	for j in range(m - 1, -1, -1):
		bit = pow(2, j)
		flag = True
		for i in range(n):
			if arr[i] == 2 and bit & (a[i] | b[i]) == 0:
				flag = False
				break
			if arr[i] == 1 and bit & a[i] == 0:
				flag = False
				break
			if arr[i] == 0 and bit & b[i] == 0:
				flag = False
				break
		if flag:
			count += bit
			for i in range(n):
				x = a[i] & bit
				y = b[i] & bit
				if arr[i] == 2 and x and y:
					continue
				if arr[i] == 2 and x:
					arr[i] = 1
				if arr[i] == 2 and y:
					arr[i] = 0
	c = 0
	for i in range(n):
		if arr[i] == 0:
			c += 1
	print(count, c)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	a = lis()
	b = lis()
	bits = [0] * 30
	flip = [0] * n
	fl = [[] for i in range(30)]
	for i in range(29, -1, -1):
		can = []
		can1 = []
		flag = 0
		for j in range(n):
			if a[j] & 1 << i:
				if b[j] & 1 << i == 0:
					can1.append(j)
				continue
			if b[j] & 1 << i and flip[j] == 0:
				can.append(j)
			else:
				bits[i] = 1
				flag = 1
				break
		if flag == 0:
			for j in can1:
				flip[j] = 1
			for j in can:
				flip[j] = 1
				(a[j], b[j]) = (b[j], a[j])
				fl[i].append(j)
	s = 0
	vis = set()
	for i in range(30):
		if bits[i]:
			continue
		s += 2 ** i
		for j in fl[i]:
			vis.add(j)
	print(s, len(vis))


from collections import defaultdict

def bs(num):
	s = ['0' for i in range(35)]
	b = bin(num)[2:]
	l = len(b)
	c = 0
	for j in b[::-1]:
		s[c] = j
		c += 1
	return s
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a = [bs(j) for j in a]
	b = [bs(j) for j in b]
	maxv = 0
	resp = ''
	pos = [-1 for j in range(n)]
	for bit in range(34, -1, -1):
		poss = True
		for j in range(n):
			if pos[j] == -1:
				if a[j][bit] == '0' and b[j][bit] == '0':
					poss = False
					break
			elif pos[j] == 0:
				if a[j][bit] == '0':
					poss = False
					break
			elif pos[j] == 1:
				if b[j][bit] == '0':
					poss = False
					break
		if poss:
			for j in range(n):
				if pos[j] == -1:
					if a[j][bit] == '0':
						pos[j] = 1
					elif b[j][bit] == '0':
						pos[j] = 0
			maxv += 2 ** bit
			resp += '1'
		else:
			resp += '0'
	ans = 0
	for j in range(n):
		if pos[j] == 1:
			ans += 1
	print(maxv, ans)


T = int(input())
for t in range(T):
	n = int(input())
	arr1 = [bin(int(i))[2:] for i in input().split()]
	arr1 = ['0' * (31 - len(i)) + i for i in arr1]
	arr2 = [bin(int(i))[2:] for i in input().split()]
	arr2 = ['0' * (31 - len(i)) + i for i in arr2]
	count = [-1] * n
	answer = [0] * 31
	for i in range(31):
		flip = [-1] * n
		ans = True
		for j in range(n):
			if count[j] == -1:
				if arr1[j][i] == '1' and arr2[j][i] == '1':
					pass
				elif arr1[j][i] == '1' and arr2[j][i] != '1':
					flip[j] = 0
				elif arr2[j][i] == '1':
					flip[j] = 1
				else:
					ans = False
					break
			elif count[j] == 0:
				if arr1[j][i] == '1':
					pass
				else:
					ans = False
					break
			elif count[j] == 1:
				if arr2[j][i] == '1':
					pass
				else:
					ans = False
					break
		if ans:
			for j in range(n):
				if flip[j] != -1:
					count[j] = flip[j]
			answer[i] = 1
	pow = 30
	tot = 0
	for i in range(31):
		if answer[i] == 1:
			tot += 2 ** pow
		pow -= 1
	flips = 0
	for i in count:
		if i == 1:
			flips += 1
	print(tot, flips)


import math
from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, gcd
import bisect
import sys
import random
import heapq

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def main():
	for _ in range(int(input())):
		n = int(input())
		a = get_array()
		b = get_array()
		bit_ind = 29
		arr = [[0, 1] for i in range(n)]
		max_ans = 0
		for i in range(bit_ind, -1, -1):
			possible_1 = True
			temp_arr = []
			for j in range(n):
				if arr[j][0] == 0:
					if len(arr[j]) == 2:
						a_val = a[j] >> i & 1
						b_val = b[j] >> i & 1
						if a_val == b_val == 1:
							temp_arr.append([0, 1])
						elif a_val == b_val == 0:
							temp_arr.append([0, 1])
							possible_1 = False
							break
						elif a_val == 1:
							temp_arr.append([0])
						else:
							temp_arr.append([1])
					else:
						val = a[j] >> i & 1
						if val == 0:
							possible_1 = False
							break
						else:
							temp_arr.append([0])
				else:
					val = b[j] >> i & 1
					if val == 0:
						possible_1 = False
						break
					else:
						temp_arr.append([1])
			if possible_1:
				max_ans |= 1 << i
				arr = temp_arr.copy()
		k = 0
		for i in arr:
			if i[0] == 1:
				k += 1
		print(max_ans, k)
main()