You are given a sequence of non-negative integers $A_1, A_2, \ldots, A_N$. At most once, you may choose a non-negative integer $X$ and for each valid $i$, change $A_i$ to $A_i \oplus X$ ($\oplus$ denotes bitwise XOR).
Find the minimum possible value of the sum of all elements of the resulting sequence.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of the input contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.

-----Output-----
For each test case, print a single line containing one integer ― the minimum possible sum.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- the sum of $N$ over all test cases does not exceed $10^6$

-----Subtasks-----
Subtask #1 (50 points):
- $1 \le N \le 10^3$
- $1 \le A_i \le 10^3$ for each valid $i$
Subtask #2 (50 points): original constraints

-----Example Input-----
3
5
2 3 4 5 6
4
7 7 7 7
3
1 1 3

-----Example Output-----
14
0
2

-----Explanation-----
Example case 1: If we choose $X = 6$, the sequence becomes $(4, 5, 2, 3, 0)$.
Example case 2: We can choose $X = 7$ to make all elements of the resulting sequence equal to $0$.
Example case 3: We can choose $X = 1$. The sequence becomes $(0, 0, 2)$, with sum $2$.

Source: codechef | Difficulty: MEDIUM_HARD


Solutions:
from collections import defaultdict
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	nums_bin = []
	bin_count = defaultdict(int)
	total = 0
	max_length = 0
	for i in range(n):
		bin_value = '{0:0b}'.format(arr[i])
		temp = len(bin_value) - 1
		if max_length < len(bin_value):
			max_length = len(bin_value)
		for b in bin_value:
			if b == '1':
				bin_count[temp] += 1
			temp -= 1
	for i in range(max_length):
		total += pow(2, i) * min(bin_count[i], n - bin_count[i])
	print(total)
	t -= 1


from functools import reduce
from operator import or_
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = reduce(or_, ((sum((c >> i & 1 for c in a)) > n // 2) << i for i in range(30)))
	print(sum((c ^ x for c in a)))


import math
from sys import stdin, stdout

def pnt(x):
	stdout.write(str(x) + '\n')

def pnt(x, end=' '):
	stdout.write(str(x) + end)
inp = lambda : int(stdin.readline())
sip = lambda : input()
mulip = lambda : map(int, input().split())
lst = lambda : list(map(int, stdin.readline().split()))
sarr = lambda : list(map(str, stdin.readline().split()))
slst = lambda : list(sip())
arr2d = lambda N: [[int(x) for x in input().split()] for i in range(N)]
for _ in range(int(input())):
	N = int(input())
	A = [int(x) for x in input().split()]
	B = []
	m = max(A)
	max_bin = format(m, 'b')
	for i in A:
		B.append(bin(i)[2:].zfill(30))
	C = ['0'] * 30
	for j in range(30):
		cnt = 0
		for i in range(N):
			if B[i][j] == '1':
				cnt += 1
		if cnt / N * 100 >= 50:
			C[j] = '1'
	S = ''.join(C)
	num = int(S, 2)
	ss = 0
	for i in A:
		ss += i ^ num
	print(ss)


for t in range(int(input())):
	B = []
	n = int(input())
	l = [int(j) for j in input().split()]
	for i in range(n):
		b = bin(l[i])[2:]
		for j in range(30 - len(b)):
			b = '0' + b
		B.append(b)
	sum_ = sum(l)
	for b in range(30):
		ct = 0
		for i in range(n):
			if B[i][b] == '1':
				ct += 1
		if min(ct, n - ct) == n - ct:
			sum_ -= 2 ** (30 - b - 1) * (2 * ct - n)
	print(sum_)


t = int(input())

def fun(n, a):
	x = 0
	for i in range(32):
		one = 0
		zero = 0
		for j in a:
			if j >> i & 1:
				one += 1
			else:
				zero += 1
		ma = 0
		if one > zero:
			ma = 1
		x += 2 ** i * ma
	s = 0
	for i in a:
		s += i ^ x
	print(s)
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	fun(n, a)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	x = list(map(int, input().split()))
	l = [0 for _ in range(30)]
	for i in x:
		m = str(bin(i))[2:]
		for p in range(1, len(m) + 1):
			if m[-p] == '1':
				l[p - 1] = l[p - 1] + 1
	s = 0
	for i in range(len(l)):
		if l[i] > n / 2:
			s = s + 2 ** i
	su = 0
	for a in x:
		su = su + (a ^ s)
	print(su)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in a:
		b += [bin(i)[2:]]
	maxx = bin(max(a))
	maxx = len(maxx) - 2
	count = [0] * maxx
	for i in b:
		l = len(i)
		for j in range(l):
			if i[j] == '1':
				count[l - j - 1] += 1
	bstr = ''
	for i in count:
		if i > n / 2:
			bstr += '1'
		else:
			bstr += '0'
	bstr_r = bstr[::-1]
	num = int(bstr_r, 2)
	summ = 0
	for i in a:
		summ += i ^ num
	print(summ)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(32):
		c = 0
		for x in l:
			if x & 1 << i:
				c += 1
		if c > n // 2:
			ans |= 1 << i
	x = 0
	for i in l:
		x += i ^ ans
	print(x)


from math import ceil

def check(el, ans, k):
	while el != 0:
		if el & 1:
			ans[k] += 1
		el = el >> 1
		k -= 1
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	ans_l = len(bin(max(l))) - 2
	for j in range(ans_l):
		ans.append(0)
	k = ans_l - 1
	for i in l:
		check(i, ans, k)
	s = ''
	for i in range(ans_l):
		if ans[i] >= ceil(n / 2):
			s += '1'
		else:
			s += '0'
	s = int(s, 2)
	sum_ = 0
	for i in range(n):
		sum_ += l[i] ^ s
	print(sum_)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = len(bin(max(a))) - 2
	b = []
	for i in a:
		r = bin(i)[2:]
		b.append('0' * (m - len(r)) + r)
	c = [0] * m
	for i in range(m):
		c[i] = sum((b[j][i] == '1' for j in range(n)))
	x = ''
	for i in c:
		if n - i > i:
			x += '0'
		else:
			x += '1'
	x = int(x, 2)
	print(sum((i ^ x for i in a)))


def solve():
	twopow = {}
	for i in range(31):
		twopow[i] = 2 ** i
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = dict()
		for i in range(31):
			b[i] = 0
		original_tot = 0
		for i in range(n):
			s = bin(a[i])[2:]
			s = s[::-1]
			original_tot += a[i]
			for i in range(len(s)):
				if s[i] == '1':
					b[i] += 1
		change = 0
		for x in b:
			p = b[x]
			if p > 0:
				if p > n - p:
					change += (p - (n - p)) * twopow[x]
		original_tot -= change
		print(original_tot)
solve()


test = int(input())
for _ in range(test):
	n = int(input())
	a = [bin(int(x))[2:][::-1] + '0' * 32 for x in input().split()]
	res = ''
	mysum = 0
	for i in range(32):
		mycount = 0
		for j in range(n):
			if a[j][i] == '0':
				mycount += 1
		if mycount == n:
			break
		if mycount > n // 2:
			res += '0'
			mysum += (n - mycount) * int(pow(2, i))
		else:
			res += '1'
			mysum += mycount * int(pow(2, i))
	print(mysum)


from math import log2
t = int(input())

def odd(a, n):
	count = 0
	for i in range(n):
		count += a[i] % 2
	if count >= n / 2:
		return 1
	return 0

def divide(a):
	for i in range(len(a)):
		a[i] = a[i] >> 1
	return a
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[:]
	time = int(log2(max(a)) + 1)
	i = 0
	answer = 0
	while i < time:
		answer += odd(a, n) * (1 << i)
		a = divide(a)
		i += 1
	x = answer
	for i in range(n):
		m[i] = m[i] ^ x
	print(sum(m))


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	bitCountOne = [0] * 31
	bitCountZero = [0] * 31
	for a in A:
		binA = bin(a)[2:][::-1]
		placeValue = -1
		for bit in binA:
			if bit == '1':
				bitCountOne[placeValue] += 1
			else:
				bitCountZero[placeValue] += 1
			placeValue -= 1
	binStr = ''
	for i in range(31):
		if bitCountOne[i] > bitCountZero[i]:
			binStr += '1'
		else:
			binStr += '0'
	X = int(binStr, 2)
	minSum = 0
	for a in A:
		minSum += X ^ a
	print(minSum)


ll = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	l = ll()
	bits = []
	summ = 0
	for bit in range(30):
		(c1, c2) = (0, 0)
		for i in l:
			if 1 << bit & i:
				c1 += 1
			else:
				c2 += 1
		summ += (1 << bit) * min(c1, c2)
	print(summ)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		print(min(a))
		continue
	num_bits = len(bin(max(a))[2:])
	bins = []
	for i in a:
		j = bin(i)[2:]
		j = '0' * (num_bits - len(j)) + j
		bins.append(j)
	b = []
	threshold = n // 2
	for i in range(num_bits):
		loc_set = 0
		for j in range(n):
			if bins[j][i] == '1':
				loc_set += 1
		if loc_set > threshold:
			b.append('1')
		else:
			b.append('0')
	b = ''.join(b)
	xor = int(b, 2)
	c = 0
	for i in a:
		c += i ^ xor
	print(c)


def power():
	global t
	t = [1]
	for i in range(1, 31):
		t.append(t[-1] * 2)
power()
tt = int(input())
import collections
for ii in range(tt):
	n = int(input())
	a = [int(x) for x in input().split()]
	ma = max(a)
	d = {}
	b = bin(ma)[2:]
	l = len(b)
	s = ''
	for i in range(l):
		d[i] = 0
	for i in a:
		for j in range(l):
			if i & t[j]:
				d[j] += 1
	ans = sum(a)
	for i in d:
		if d[i] > n - d[i]:
			ans -= (2 * d[i] - n) * t[i]
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	x = list(map(int, input().split()))
	max = []
	for i in x:
		li = []
		for j in x:
			li.append(i ^ j)
		max.append(sum(li))
	print(min(max))
	t = t - 1


for i in range(int(input())):
	N = int(input())
	array = list(map(int, input().split()))
	array = list(map(lambda x: bin(x)[2:], array))
	finalSum = 0
	for j in range(-1, -31, -1):
		count = 0
		for bit in array:
			try:
				if bit[j] == '1':
					count += 1
			except:
				pass
		finalSum += min(count * 2 ** (j * -1 - 1), (N - count) * 2 ** (j * -1 - 1))
	print(finalSum)


for t in range(int(input())):
	n = int(input())
	a_ = list(map(int, input().split()))
	a = list(map(bin, a_))
	for i in range(n):
		a[i] = '0' * (33 - len(a[i])) + a[i][2:]
	count = [0] * 31
	for j in range(31):
		for i in range(n):
			if a[i][j] == '1':
				count[30 - j] += 1
	x = 0
	half = n // 2
	totSum = 0
	for i in range(31):
		totSum += min(n - count[i], count[i]) * 2 ** i
	print(totSum)


T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a) * sum(a)
	for j in range(n):
		q = 0
		k = a[j]
		for t in range(n):
			q = q + (k ^ a[t])
		s = min(q, s)
	print(s)


def find(A):
	M = max(A)
	M = len(bin(M)[2:])
	strings = []
	for i in range(len(A)):
		temp = bin(A[i])[2:]
		strings += ['0' * (M - len(temp)) + temp]
	bits = [0] * M
	for i in range(M):
		count = 0
		for j in range(len(strings)):
			if strings[j][i] == '1':
				count += 1
		if 2 * count >= len(A):
			bits[i] = 1
		else:
			bits[i] = 0
	s = ''
	for x in bits:
		s += str(x)
	X = int(s, 2)
	return sum([A[i] ^ X for i in range(len(A))])
for _ in range(int(input())):
	input()
	A = list(map(int, input().strip().split(' ')))
	print(find(A))


def answer():
	x = 0
	for bit in range(30):
		count = 0
		for i in range(n):
			if a[i] >> bit & 1:
				count += 1
		if count > n // 2:
			x |= 1 << bit
	ans = 0
	for v in a:
		ans += v ^ x
	return ans
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(answer())


import sys
from collections import Counter, defaultdict, deque
from math import gcd, sqrt, ceil

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

def si():
	return sys.stdin.readline().split()

def ii():
	return int(sys.stdin.readline())

def ip():
	return input()
for tastcas in range(int(input())):
	n = ii()
	a = li()
	bits = [0] * 30
	s = 0
	k = 1
	for i in range(n):
		b = bin(a[i])[2:]
		a[i] = '0' * (30 - len(b)) + b
		for j in range(30):
			if a[i][j] == '1':
				bits[j] += 1
	for i in range(30):
		bits[i] = min(bits[i], n - bits[i])
	for i in range(30):
		s += bits[29 - i] * k
		k *= 2
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(lambda x: bin(int(x))[2:], input().split()))
	cnt = max([len(i) for i in ls])
	ls = ['0' * (cnt - len(i)) + i for i in ls]
	X = ''
	for i in range(cnt):
		cnt0 = cnt1 = 0
		for j in ls:
			if j[i] == '1':
				cnt1 += 1
			else:
				cnt0 += 1
		if cnt0 > cnt1:
			X += '0'
		else:
			X += '1'
	X = int(X, 2)
	print(sum([int(i, 2) ^ X for i in ls]))


for z in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	bin_ar = []
	dict_count = {i: 0 for i in range(31)}
	for k in ar:
		bin_ar.append((31 - len(bin(k)[2:])) * '0' + bin(k)[2:])
	for num in bin_ar:
		for i in range(31):
			if num[i] == '0':
				dict_count[i] += 1
	x = ['0' for i in range(31)]
	for key in dict_count:
		if dict_count[key] < n / 2:
			x[key] = '1'
	x_int = int(''.join(x), 2)
	new_ar = [k ^ x_int for k in ar]
	print(sum(new_ar))


for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	p = []
	for i in l:
		y = bin(i)[2:]
		y = '0' * (32 - len(y)) + y
		p.append(y)
	arr = [0 for i in range(32)]
	for k in p:
		for i in range(32):
			if k[i] == '1':
				arr[i] += 1
	ans = ''
	for i in arr:
		if i > x // 2:
			ans += '1'
		else:
			ans += '0'
	s = 0
	g = int(ans, 2)
	for i in l:
		s += g ^ i
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	it = list(map(int, input().split()))
	a = len(bin(max(it))) - 2
	s = [0 for i in range(a)]
	bit = [bin(i)[2:][-1::-1] for i in it]
	for i in range(a):
		x = 0
		for jj in bit:
			try:
				if jj[i] == '1':
					x += 1
			except:
				pass
		s[i] = x
	re = 0
	for (j, i) in enumerate(s):
		if i > n / 2:
			re += 2 ** j
	o = [i ^ re for i in it]
	print(sum(o))


from math import *
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	ele = 0
	num_bits = int(log2(max(l)) + 1)
	for i in range(num_bits):
		count = 0
		e = 1 << i
		for j in range(n):
			if e & l[j]:
				count += 1
		if count < n - count:
			continue
		ele += e
	for i in range(n):
		sum += l[i] ^ ele
	print(sum)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	binary = []
	max_bits = len(bin(max(arr))[2:])
	for i in arr:
		num = bin(i)[2:]
		num = '0' * (max_bits - len(num)) + num
		binary.append(num)
	ans = 0
	for i in range(-1, -(max_bits + 1), -1):
		count1 = 0
		count0 = 0
		for j in binary:
			if j[i] == '1':
				count1 += 1
			else:
				count0 += 1
		if count1 > count0:
			ans += count0 * 2 ** (abs(i) - 1)
		else:
			ans += count1 * 2 ** (abs(i) - 1)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	lsit = [x for x in ls]
	gain = 0
	for bs in range(33):
		cnt1s = 0
		cnt1s = sum((1 for x in lsit if x & 1))
		lsit = list(map(lambda x: x >> 1, lsit))
		if not cnt1s:
			break
		if cnt1s > n // 2:
			gain += cnt1s * 2 - n << bs
	print(sum(ls) - gain)


from math import log2

def findX(n, arr):
	itr = max(arr)
	p = int(log2(itr)) + 1
	X = 0
	for i in range(p):
		if len([j for j in range(n) if arr[j] & 1 << i]) > int(n / 2):
			X += 1 << i
	print(sum([X ^ arr[i] for i in range(n)]))
for _ in range(int(input())):
	findX(int(input()), list(map(int, input().split())))


tc = int(input())
while tc > 0:
	n = int(input())
	sum = []
	s = 0
	lis = list(map(int, input().split()))
	for i in range(n):
		x = lis[i]
		s = 0
		for j in range(n):
			y = lis[j]
			s += y ^ x
		sum.append(s)
	print(min(sum))
	tc -= 1


t = int(input())
import collections
for ii in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ma = max(a)
	d = {}
	b = bin(ma)[2:]
	l = len(b)
	s = ''
	for i in range(l):
		d[i] = 0
	for i in a:
		for j in range(l):
			if i & 1 << j:
				d[j] += 1
	for i in d:
		if d[i] > n - d[i]:
			s += '1'
		else:
			s += '0'
	s = s[::-1]
	s = int(s, 2)
	ans = 0
	for i in a:
		ans += s ^ i
	print(ans)


import numpy as np
t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	S = sum(a)
	A = [[x] for x in a]
	bits = np.unpackbits(np.array(A, dtype='>i8').view(np.uint8), axis=1).astype(int)
	counts = np.flip(bits.sum(axis=0), axis=0)
	diffs = 2 * counts - n
	for i in range(len(diffs)):
		if diffs[i] > 0:
			S -= 2 ** i * diffs[i]
	print(S)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dict1 = {}
	for i in range(31, -1, -1):
		flag = 1 << i
		count = 0
		for i in range(n):
			if flag & arr[i] != 0:
				count += 1
		if count != 0:
			dict1[flag] = count
	xor = 0
	for mask in dict1:
		if dict1[mask] >= n // 2 + 1:
			xor |= mask
	total = 0
	for i in range(n):
		total += arr[i] ^ xor
	print(total)


for T in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	L = len(bin(max(A)))
	ones = [0] * L
	for Ai in A:
		b = bin(Ai)
		b = (L - len(b)) * '0' + b
		for bit in enumerate(b):
			if bit[1] == '1':
				ones[bit[0]] += 1
	b = '0b'
	for Ai in ones:
		if Ai > N // 2:
			b += '1'
		else:
			b += '0'
	b = int(b, 2)
	S = 0
	for Ai in A:
		S += b ^ Ai
	print(S)


def main():
	tc = int(input())
	for _ in range(tc):
		n = int(input())
		a = list(map(int, input().split()))
		a1 = [a[i] for i in range(n)]
		st = set()
		x = ''
		while len(st) != n:
			c = 0
			for i in range(n):
				c += a[i] % 2
				a[i] = a[i] // 2
				if a[i] == 0:
					st.add(i)
			c0 = n - c
			if c0 >= c:
				x = '0' + x
			else:
				x = '1' + x
		x = int(x, 2)
		sm = 0
		for i in a1:
			sm += i ^ x
		print(sm)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().strip().split(' ')))
	st = {}
	bina = []
	male = 0
	for i in s:
		te = '{0:b}'.format(i)
		bina.append(te[::-1])
		male = max(male, len(te))
	sta = list('0' * male)
	for i in range(n):
		temp = bina[i] + '0' * (male - len(bina[i]))
		bina[i] = list(temp[::-1])
	pla = list(zip(*bina))
	psta = sta
	for i in range(male):
		cal = pla[i].count('1')
		if cal > n // 2:
			psta[i] = '1'
	num = int(''.join(psta), 2)
	suma = 0
	for i in s:
		suma += i ^ num
	print(suma)


from math import log2

def findX(arr, n):
	itr = arr[0]
	for i in range(len(arr)):
		if arr[i] > itr:
			itr = arr[i]
	p = int(log2(itr)) + 1
	X = 0
	for i in range(p):
		count = 0
		for j in range(n):
			if arr[j] & 1 << i:
				count += 1
		if count > int(n / 2):
			X += 1 << i
	sum = 0
	for i in range(n):
		sum += X ^ arr[i]
	print(sum)
try:
	t = int(input())
	for _ in range(t):
		ss = int(input())
		arr = list(map(int, input().split()))
		n = len(arr)
		findX(arr, n)
except:
	pass


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	sums = 0
	for i in range(30):
		one = 0
		zeros = 0
		for j in l:
			if j & 1 << i:
				zeros += 1
			else:
				one += 1
		if one > zeros:
			sums += pow(2, i) * zeros
		else:
			sums += pow(2, i) * one
	print(sums)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	m = max(l)
	ans = 0
	for i in range(30):
		c = 0
		for j in range(n):
			if l[j] & 1 << i:
				c += 1
		ones = c
		zeroes = n - c
		if zeroes > ones:
			pass
		else:
			ans += 1 << i
	print(sum((ans ^ i for i in l)))


import sys
from sys import setrecursionlimit
from bisect import bisect_left, bisect_right, insort
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10, log2
from itertools import permutations
from heapq import heappush, heappop, heapify
inf = float('inf')
mod = 1000000007
mini = 1000000007

def ncr(n, r):
	d = 10 ** 9 + 7
	num = fact(n)
	den = fact(r) * fact(n - r) % d
	den = pow(den, d - 2, d)
	return num * den % d

def sieve(n):
	prime = [True for i in range(n + 1)]
	lst = []
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p = p + 1
	for i in range(2, n + 1):
		if prime[i]:
			lst.append(i)
	return lst

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def calculate_factors(n):
	hh = [1] * (n + 1)
	p = 2
	while p * p < n:
		if hh[p] == 1:
			for i in range(p * 2, n, p):
				hh[i] = 0
		p += 1
	total = 1
	for p in range(2, n + 1):
		if hh[p] == 1:
			count = 0
			if n % p == 0:
				while n % p == 0:
					n = int(n / p)
					count += 1
				total *= count + 1
	return total

def prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			factors.add(n // i)
			n = n // i
			factors.add(i)
	if n > 1:
		factors.add(n)
	return factors

def isPrime(n):
	if n == 2 or n == 3:
		return True
	if n % 2 == 0 or n < 2:
		return False
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True

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()
T = int(input())
while T > 0:
	n = int(input())
	Arr = get_array()
	maxi = max(Arr)
	nobits = len(bin(maxi)[2:])
	lst = []
	half = n // 2
	for i in Arr:
		x = bin(i)[2:]
		if len(x) < nobits:
			x = '0' * (nobits - len(x)) + x
		lst.append(x)
	result = []
	for i in range(nobits):
		total = 0
		for j in range(n):
			if lst[j][i] == '1':
				total += 1
		if total > half:
			result.append('1')
		else:
			result.append('0')
	x_factor = int(''.join(result), 2)
	ans = 0
	for i in Arr:
		ans += x_factor ^ i
	print(ans)
	T -= 1


for _ in range(int(input())):
	n = int(input())
	L = [format(int(i), '030b') for i in input().split()]
	X = ['0' for i in range(30)]
	for i in range(30):
		c = 0
		for j in range(n):
			if L[j][i] == '1':
				c += 1
		if c > n - c:
			X[i] = '1'
	x = int(''.join(X), 2)
	s = 0
	for a in L:
		s += int(a, 2) ^ x
	print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(x, max_bit) = (0, 0)
	bitset = {}
	for ele in arr:
		for (i, bit) in enumerate(reversed(bin(ele)[2:])):
			if bit == '1':
				bitset[i] = bitset.get(i, 0) + 1
			max_bit = max(max_bit, i)
	for b in range(max_bit + 1):
		if b in bitset:
			if bitset[b] > n - bitset[b]:
				x |= 1 << b
	total = 0
	for ele in arr:
		total += ele ^ x
	print(total)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lis2 = [0] * 32
	for i in range(n):
		a = bin(lis[i])[2:]
		for j in range(len(a)):
			if a[j] == '1':
				lis2[len(a) - j - 1] += 1
	lis2.reverse()
	s = ''
	for i in lis2:
		if i > n - i:
			s += '1'
		else:
			s += '0'
	num = int(s, 2)
	cnt = 0
	for i in lis:
		cnt += i ^ num
	print(cnt)


from math import *
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	num_bits = int(log2(max(l)) + 1)
	(ans, X) = (0, 0)
	for i in range(num_bits):
		(oc, zc, ele) = (0, 0, 1 << num_bits - i - 1)
		for j in range(n):
			if l[j] & ele:
				oc += 1
			else:
				zc += 1
		if oc > zc:
			X += ele
	for i in range(n):
		ans += l[i] ^ X
	print(ans)


for i in range(int(input())):
	N = input()
	A = list(map(int, input().split()))
	binary_A = []
	max_len = 0
	for num in A:
		bin_num = bin(num)[2:]
		if len(bin_num) > max_len:
			max_len = len(bin_num)
		binary_A.append(bin_num)
	for j in range(len(binary_A)):
		if len(binary_A[j]) < max_len:
			x = max_len - len(binary_A[j])
			for i in range(x):
				binary_A[j] = '0' + binary_A[j]
	X = ''
	for i in range(max_len):
		count0 = count1 = 0
		for num in binary_A:
			if num[i] == '0':
				count0 += 1
			else:
				count1 += 1
		if count0 > count1:
			X = X + '0'
		else:
			X = X + '1'
	X = int(X, 2)
	min_sum = 0
	for num in A:
		min_sum += num ^ X
	print(min_sum)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = [int(num) for num in input().split()]
	res = 0
	for i in range(31):
		one = zero = 0
		for ele in ar:
			if ele & 1 << i:
				one += 1
			else:
				zero += 1
		if one > zero:
			res += zero * (1 << i)
		else:
			res += one * (1 << i)
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = [0 for i in range(32)]
	for i in range(n):
		c = bin(a[i])[2:]
		c = c[::-1]
		for i in range(len(c) - 1, -1, -1):
			if c[i] == '1':
				k[i] += 1
	ans = ''
	for i in range(32):
		if k[i] > n / 2:
			ans += '1'
		else:
			ans += '0'
	ans = ans[::-1]
	ans = int(ans, 2)
	su = 0
	for i in range(n):
		su += a[i] ^ ans
	print(su)


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	s = []
	for i in a:
		y = bin(i)[2:]
		y = '0' * (32 - len(y)) + y
		s.append(y)
	arr = [0] * 32
	for j in s:
		for i in range(32):
			if j[i] == '1':
				arr[i] += 1
	ans = ''
	for i in arr:
		if i > n // 2:
			ans += '1'
		else:
			ans += '0'
	sum1 = 0
	g = int(ans, 2)
	for i in a:
		sum1 += g ^ i
	print(sum1)


import math
t = int(input())
while t != 0:
	N = int(input())
	A = [f'{int(x):032b}' for x in input().split()]
	min_sum = 0
	for i in range(32):
		count = len(list(filter(lambda x: x[i] == '1', A)))
		min_sum = min_sum + min(count, N - count) * math.pow(2, 31 - i)
	t -= 1
	print(int(min_sum))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 30
	for i in a:
		for (j, val) in enumerate(bin(i)[2:][::-1]):
			if val == '1':
				b[j] += 1
	ans = 0
	for i in range(30):
		ans += min(b[i], n - b[i]) * 2 ** i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	ll = [0] * 32
	for i in l:
		for j in range(32):
			if 1 << j & i > 0:
				ll[j] += 1
	if n % 2 != 0:
		x = n // 2 + 1
	else:
		x = n // 2
	s = ''
	for i in range(31, -1, -1):
		if ll[i] >= x:
			s += str('1')
		else:
			s += str('0')
	ss = int(s, 2)
	ans = 0
	for i in l:
		ans += i ^ ss
	print(ans)


from math import log2
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	m1 = max(A)
	size = int(log2(m1)) + 1
	x = 0
	ans = 0
	for i in range(size):
		(set_bit, mask) = (0, 1 << i)
		for j in A:
			if j & mask:
				set_bit += 1
		ans += min(set_bit, N - set_bit) * mask
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = max(l)
	s = bin(s)
	maxbits = len(s) - 1
	x = []
	for i in range(maxbits + 1):
		count = 0
		for j in range(n):
			if 2 ** i & l[j] == 2 ** i:
				count += 1
		if count > n // 2:
			x.append(1)
		else:
			x.append(0)
	x.reverse()
	ans = ''
	for i in x:
		ans += str(i)
	ans1 = int(ans, 2)
	sm = 0
	for i in l:
		sm += i ^ ans1
	print(sm)


def fun(s):
	su = 0
	for (j, i) in enumerate(s):
		if i == '1':
			su += 2 ** j
	return su
for i in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	a = list((str(bin(int(i)))[2:] for i in b))
	m = str(bin(max(b))[2:])
	l = len(m)
	s = ''
	for i in range(l):
		c1 = c2 = 0
		for j in a:
			l1 = len(j)
			if l1 - i - 1 >= 0:
				if j[l1 - i - 1] == '0':
					c1 += 1
				else:
					c2 += 1
			else:
				c1 += 1
		if c1 > c2:
			s += '0'
		else:
			s += '1'
	s = fun(s)
	su = 0
	for i in b:
		su += s ^ i
	print(su)


def solve():
	n = int(input())
	A = [bin(int(i))[2:] for i in input().split()]
	res = 0
	for j in range(-1, -31, -1):
		count = 0
		for bit in A:
			try:
				if bit[j] == '1':
					count += 1
			except:
				pass
		res += min(count, n - count) * 2 ** (j * -1 - 1)
	print(res)
for t in range(int(input())):
	solve()


for I in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	rn = 0
	for i in range(32):
		d[i] = 0
	for i in range(n):
		s = str(bin(l[i]))[2:]
		s = '0' * (32 - len(s)) + s
		for i in range(32):
			if s[i] == '1':
				d[i] += 1
	for i in range(32):
		if d[i] >= n / 2:
			rn += 2 ** (31 - i)
	s = 0
	for i in range(n):
		s += l[i] ^ rn
	print(s)


pow2 = [1] * 30
for i in range(29):
	pow2[i + 1] = pow2[i] * 2
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = sum(arr)
	v = [0] * 30
	limit = n // 2
	for i in range(n):
		v1 = bin(arr[i])
		lv = len(v1)
		for j in range(lv - 1, 1, -1):
			if v1[j] == '1':
				v[lv - 1 - j] += 1
	for j in range(30):
		if v[j] > limit:
			ans += (n - v[j] - v[j]) * pow2[j]
	print(ans)


from collections import defaultdict
test = int(input())
for _ in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 32
	for i in a:
		t = bin(i)[2:]
		t = t[::-1]
		for i in range(len(t)):
			if t[i] == '1':
				b[i] += 1
	for i in range(32):
		if b[i] > n // 2:
			b[i] = 1
		else:
			b[i] = 0
	b = b[::-1]
	b = ''.join((str(x) for x in b))
	t = int(b, 2)
	ans = 0
	for i in a:
		ans += i ^ t
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	f = 2147483648
	fr = [0] * 32
	for i in range(32):
		for j in a:
			if f & j > 0:
				fr[i] += 1
		f = f >> 1
	nm = ''
	for i in fr:
		if i <= n // 2:
			nm += '0'
		else:
			nm += '1'
	nm = int(nm, 2)
	print(sum([x ^ nm for x in a]))


t = int(input())
for i in range(t):
	n = int(input())
	b = [bin(int(x))[2:] for x in input().split()]
	s = [0] * 31
	for j in range(n):
		l = len(b[j])
		for k in range(l):
			if b[j][k] == '1':
				s[l - k - 1] += 1
	k = 30
	while s[k] == 0:
		k -= 1
	m = 1
	res = 0
	for sk in range(k + 1):
		if s[sk] > n / 2:
			s[sk] = n - s[sk]
		res += m * s[sk]
		m = m * 2
	print(res)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	binary = []
	for i in ar:
		binary.append(bin(i)[2:].zfill(30))
	ans = ''
	for i in range(30):
		(one, zero) = (0, 0)
		for cur in binary:
			if cur[i] == '1':
				one += 1
			else:
				zero += 1
		ans += '1' if one > zero else '0'
	now = int(ans, 2)
	ans = 0
	for i in ar:
		ans += i ^ now
	print(ans)


import operator
t = int(input())
for z in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	lis2 = [0] * 32
	for i in range(n):
		a = bin(lis[i])[2:]
		for i in range(len(a)):
			if a[i] == '1':
				lis2[len(a) - i - 1] += 1
	ss = ''
	lis2.reverse()
	for i in lis2:
		if i > n // 2:
			ss += '1'
		else:
			ss += '0'
	aa = int(ss, 2)
	s = 0
	for i in lis:
		s += i ^ aa
	print(s)


for _ in range(int(input())):
	n = int(input())
	array = [bin(int(x))[2:][::-1] + '0' * 32 for x in input().split()]
	ans = 0
	for i in range(32):
		count = 0
		for j in range(n):
			if array[j][i] == '1':
				count += 1
		if count > n // 2:
			ans += (n - count) * 2 ** i
		else:
			ans += count * 2 ** i
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	m = max(a)
	lgt = len(bin(m)[2:])
	b = []
	for j in range(n):
		val = bin(a[j])[2:]
		val = '0' * (lgt - len(val)) + val
		b.append(val)
	x = ''
	for j in range(lgt):
		(one, zero) = (0, 0)
		for k in range(n):
			if b[k][j] == '1':
				one += 1
			else:
				zero += 1
		if one > zero:
			x = x + '1'
		else:
			x = x + '0'
	x = int(x, 2)
	for j in range(n):
		a[j] = a[j] ^ x
	print(sum(a))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = len(bin(max(a))) - 2
	b = [0] * k
	for i in a:
		p = bin(i)[2:]
		p = '0' * (k - len(p)) + p
		for i in range(len(p)):
			if p[i] == '1':
				b[i] += 1
	s = ''
	for i in b:
		if i > n // 2:
			s += '1'
		else:
			s += '0'
	s = int(s, 2)
	p = 0
	for i in a:
		p += i ^ s
	print(p)


lo = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648]
from collections import defaultdict as dd
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	l1 = list(l)
	le = -1
	for i in range(n):
		l[i] = bin(l[i])[2:]
		if len(l[i]) > le:
			le = len(l[i])
	for i in range(n):
		l[i] = '0' * (le - len(l[i])) + l[i]
	s = 0
	cou = len(l[0]) - 1
	res = 0
	for i in range(len(l[0])):
		cou1 = 0
		cou0 = 0
		for j in range(n):
			if l[j][i] == '1':
				cou1 += 1
			else:
				cou0 += 1
		if cou1 >= cou0:
			s += cou0 * lo[cou]
			cou -= 1
		else:
			s += cou1 * lo[cou]
			cou -= 1
	print(s)
	t -= 1


def main():
	for t in range(int(input())):
		n = int(input())
		array = [bin(int(i))[2:] for i in input().split()]
		final_sum = 0
		for j in range(-1, -31, -1):
			count = 0
			for bit in array:
				try:
					if bit[j] == '1':
						count += 1
				except:
					continue
			final_sum += min(count, n - count) * 2 ** (j * -1 - 1)
		print(final_sum)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	arr = sorted(arr, reverse=True)
	maxm = len(bin(arr[0])[2:])
	grid = [''] * n
	for (index, i) in enumerate(arr):
		b = list(bin(i)[2:])
		ans = ['0'] * (maxm - len(b)) + b
		grid[index] = ans
	grid = list(map(list, zip(*grid)))
	ans = 0
	for (index, i) in enumerate(grid):
		p = i.count('1')
		ans = ans + min(p, n - p) * 2 ** (maxm - 1 - index)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(x, cur_bit) = (0, 0)
	bitset = {}
	for ele in arr:
		while ele:
			if ele & 1:
				bitset[cur_bit] = bitset.get(cur_bit, 0) + 1
			cur_bit += 1
			ele >>= 1
		cur_bit = 0
	for b in bitset:
		if bitset[b] > n - bitset[b]:
			x |= 1 << b
	total = 0
	for ele in arr:
		total += ele ^ x
	print(total)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = ['{0:b}'.format(x).zfill(32)[::-1] for x in l]
	ans = ''
	for i in range(32):
		zcount = 0
		for j in range(n):
			if a[j][i] == '0':
				zcount += 1
		if zcount == n:
			break
		if zcount > n // 2:
			ans += '0'
		else:
			ans += '1'
	ans = int(ans[::-1], 2)
	aans = 0
	for i in range(n):
		aans += l[i] ^ ans
	print(aans)