You are given an array $a$ of length $n$. You can perform the following operation several (possibly, zero) times:

Choose $i$, $j$, $b$: Swap the $b$-th digit in the binary representation of $a_i$ and $a_j$.

Find the maximum possible value of $\max(a) - \min(a)$.

In a binary representation, bits are numbered from right (least significant) to left (most significant). Consider that there are an infinite number of leading zero bits at the beginning of any binary representation.

For example, swap the $0$-th bit for $4=100_2$ and $3=11_2$ will result $101_2=5$ and $10_2=2$. Swap the $2$-nd bit for $4=100_2$ and $3=11_2$ will result $000_2=0_2=0$ and $111_2=7$.

Here, $\max(a)$ denotes the maximum element of array $a$ and $\min(a)$ denotes the minimum element of array $a$.

The binary representation of $x$ is $x$ written in base $2$. For example, $9$ and $6$ written in base $2$ are $1001$ and $110$, respectively.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 128$) — the number of testcases.

The first line of each test case contains a single integer $n$ ($3 \le n \le 512$) — the length of array $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i < 1024$) — the elements of array $a$.

It's guaranteed that the sum of $n$ over all testcases does not exceed $512$.


-----Output-----

For each testcase, print one integer — the maximum possible value of $\max(a) - \min(a)$.


-----Examples-----

Input
4
3
1 0 1
4
5 5 5 5
5
1 2 3 4 5
7
20 85 100 41 76 49 36
Output
1
0
7
125


-----Note-----

In the first example, it can be shown that we do not need to perform any operations — the maximum value of $\max(a) - \min(a)$ is $1 - 0 = 1$.

In the second example, no operation can change the array — the maximum value of $\max(a) - \min(a)$ is $5 - 5 = 0$.

In the third example, initially $a = [1, 2, 3, 4, 5]$, we can perform one operation taking $i = 2$, $j = 5$, $b = 1$. The array now becomes $a = [1, 0, 3, 4, 7]$. It can be shown that any further operations do not lead to a better answer — therefore the answer is $\max(a) - \min(a) = 7 - 0 = 7$.

Source: codeforces | Difficulty: EASY
Tags: Bit manipulation

Constructive algorithms

Mathematics

Greedy algorithms


Solutions:
t = int(input())
for t_ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	max_len = max((len(bin(c)) for c in a)) - 2
	b = list(('0' * (max_len + 2 - len(bin(_))) + bin(_)[2:] for _ in a))
	ones = [False for _ in range(max_len)]
	zeros = [True for _ in range(max_len)]
	for i in range(n):
		for j in range(len(b[i])):
			if b[i][j] == '1':
				ones[j] = True
			else:
				zeros[j] = False
	big = 0
	small = 0
	for i in range(max_len):
		big += ones[max_len - i - 1] * 2 ** i
		small += zeros[max_len - i - 1] * 2 ** i
	print(big - small)


t = int(input())
for i in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	a = L[0]
	b = L[0]
	for i in range(1, n):
		a = a | L[i]
		b = b & L[i]
	print(a - b)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(a, b) = (arr[0], arr[0])
	for i in range(1, n):
		a = a | arr[i]
		b = b & arr[i]
	print(a - b)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = arr[0]
	y = arr[0]
	for j in range(1, n):
		x = x | arr[j]
		y = y & arr[j]
	print(x - y)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	mx = arr[0]
	mn = arr[0]
	for i in range(1, len(arr)):
		mx = mx | arr[i]
		mn = mn & arr[i]
	print(mx - mn)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = arr[0]
	mi = arr[0]
	for i in range(1, len(arr)):
		m = m | arr[i]
		mi = mi & arr[i]
	print(m - mi)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ma = mi = l[0]
	for i in range(1, len(l)):
		(ma, mi) = (ma | l[i], mi & l[i])
	print(ma - mi)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	o = 0
	for i in range(n):
		o = o | l[i]
	s = l[0]
	for i in range(1, n):
		s = s & l[i]
	print(o - s)


t = int(input())
for t1 in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	ans = 0
	for j in range(10):
		last = l[0] % 2
		l[0] //= 2
		for i in range(1, len(l)):
			if last == -1:
				l[i] //= 2
				continue
			if l[i] % 2 != last:
				ans += pow(2, j)
				last = -1
			l[i] //= 2
	print(ans)


test = int(input())
while test:
	n = int(input())
	a = [int(x) for x in input().split()]
	maxi = mini = a[0]
	for i in a:
		maxi |= i
		mini &= i
	print(maxi - mini)
	test -= 1


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	maxi = arr[0]
	mini = arr[0]
	for i in arr:
		maxi = maxi & i
		mini = mini | i
	print(mini - maxi)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ma = max(a)
	for i in range(n):
		diff = (ma | a[i]) - ma
		ma += diff
		a[i] -= diff
	a.sort()
	mia = min(a)
	for j in range(1, n - 1):
		mia -= (mia | a[j]) - a[j]
	if mia < 0:
		mia = 0
	print(ma - mia)


for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	L.sort()
	L1 = [0 for _ in range(n)]
	r = len(bin(L[-1])[2:])
	for i in range(n):
		L1[i] = list(bin(L[i])[2:])
		while len(L1[i]) < r:
			L1[i].insert(0, '0')
	for k in range(r):
		if L1[-1][k] == '0':
			j = 0
			while True:
				if L1[j][k] == '1':
					L1[j][k] = '0'
					L1[-1][k] = '1'
					break
				j += 1
				if j == n - 1:
					break
	L3 = [''.join(L1[i]) for i in range(n)]
	L3.sort()
	L4 = []
	for j in range(n):
		L4.append(list(L3[j]))
	for k in range(r):
		if L4[0][k] == '1':
			j = 1
		while True:
			if j == n - 1:
				break
			if L4[j][k] == '0':
				L4[0][k] = '0'
				break
			j += 1
	print(int(''.join(L4[-1]), 2) - int(''.join(L4[0]), 2))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = l[0]
	b = l[0]
	for i in range(1, n):
		x = l[i]
		a = a & x
		b = b | x
	print(b - a)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		or_num = arr[0]
		and_num = arr[0]
		for i in range(1, n):
			or_num = or_num | arr[i]
			and_num = and_num & arr[i]
		print(or_num - and_num)
main()


for _ in range(int(input())):
	mx = 0
	mn = 2 ** 12 - 1
	input()
	for a in map(int, input().split()):
		mx |= a
		mn &= a
	print(mx - mn)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = 0
	c = 1023
	for j in range(n):
		b = b | a[j]
		c = c & a[j]
	print(b - c)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	min = l[0]
	max = min
	for j in range(n):
		min = min & l[j]
		max = max | l[j]
	print(max - min)


for _ in range(int(input())):
	l = int(input())
	nums = list(map(int, input().split()))
	max_ = max(nums)
	min_ = min(nums)
	for num in nums:
		max_ = max(max_, max_ | num)
		min_ = min(min_, min_ & num)
	print(max_ - min_)


n = int(input())
for i in range(n):
	l = int(input())
	arr = [int(val) for val in input().split()]
	(a, b) = (arr[0], arr[1])
	for val in arr:
		a = a & val
		b = b | val
	print(b - a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(x, y) = (a[0], a[0])
	for i in a:
		x |= i
		y &= i
	print(x - y)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	ans_or = a[0]
	ans_and = a[0]
	for i in range(1, n):
		ans_or = ans_or | a[i]
		ans_and = ans_and & a[i]
	print(ans_or - ans_and)


import sys

def dfs(adj, vis, i, v):
	vis[i] = True
	for j in adj[i]:
		if not vis[j]:
			dfs(adj, vis, j, v)
	v.append(i)

def solve():
	n = int(input())
	a = [int(x) for x in sys.stdin.readline().split()]
	max_val = a[0]
	min_val = a[0]
	for i in range(1, n):
		max_val = max_val & a[i]
		min_val = min_val | a[i]
	print(min_val - max_val)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import math

def getbit(x, y):
	return x >> y & 1

class Solution:

	def __init__(self):
		pass

	def read(varr):
		varr.n = int(input())
		varr.arr = input().split()
		for index in range(varr.n):
			varr.arr[index] = int(varr.arr[index])

	def solve(varr):
		Count = []
		for index in range(10):
			Count.append(0)
		for index in range(varr.n):
			for bit in range(10):
				if getbit(varr.arr[index], bit):
					Count[bit] += 1
		res = 0
		for bit in range(10):
			if Count[bit] != 0 and Count[bit] != varr.n:
				res += 1 << bit
		print(res)
test = int(input())
while test > 0:
	testcase = Solution()
	testcase.read()
	testcase.solve()
	test -= 1


for i in range(int(input())):
	n = int(input())
	x = -1
	y = 0
	a = list(map(int, input().split()))
	for i in range(n):
		x &= a[i]
		y |= a[i]
	print(y - x)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(sep=' ')))
	minn = 1023
	maxx = 0
	for j in range(n):
		x = minn & a[j]
		minn = x
		y = maxx | a[j]
		maxx = y
	print(maxx - minn)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	max = a[0]
	min = a[0]
	for j in range(n - 1):
		max = max | a[j + 1]
	for k in range(n - 1):
		min = min & a[k + 1]
	print(max - min)


import math
import sys
input = sys.stdin.readline

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

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def print_min(miner):
	if miner == 9999999999999999:
		print(-1, end=' ')
	else:
		print(miner, end=' ')
for _ in range(inp()):
	n = inp()
	arr = inlt()
	min = 0
	max = int('1111111111111111111111111111', 2)
	for i in range(n):
		min = min | arr[i]
		max = max & arr[i]
	print(min - max)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = l[0]
	ans1 = l[0]
	for i in l:
		ans |= i
		ans1 &= i
	print(ans - ans1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ctr = [0] * 32
	for el in arr:
		for b in range(32):
			if el & 1 << b:
				ctr[b] += 1
	maxval = 0
	minval = 0
	for b in range(32):
		if ctr[b] > 0:
			maxval |= 1 << b
		if ctr[b] == n:
			minval |= 1 << b
	print(maxval - minval)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	max_a = 0
	min_a = 1023
	for i in range(n):
		max_a |= a[i]
		min_a &= a[i]
	print(max_a - min_a)


for _ in range(int(input())):
	n = int(input())
	arr = (*map(int, input().split()),)
	mx = mn = arr[0]
	for num in arr:
		mx |= num
		mn &= num
	print(mx - mn)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	max = 0
	min = arr[0]
	for i in arr:
		max |= i
		min &= i
	print(max - min)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	ad = a[0]
	ar = a[0]
	for i in range(1, n):
		ad &= a[i]
		ar |= a[i]
	print(ar - ad)
t = int(input())
for _ in range(t):
	solve()


import sys
t = int(sys.stdin.readline())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	vec = [0] * 32
	for j in lst:
		s = bin(j).replace('0b', '')
		s = '0' * (32 - len(s)) + s
		for h in range(32):
			if s[h] == '1':
				vec[h] += 1
	ma = ''
	mi = ''
	for j in range(32):
		one = vec[j]
		zer = n - one
		if one > 0:
			ma += '1'
		else:
			ma += '0'
		if zer > 0:
			mi += '0'
		else:
			mi += '1'
	print(int(ma, 2) - int(mi, 2))


t = int(input())
for _ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	M = [0] * 12
	for i in range(n):
		for j in range(12):
			if L[i] & 1 << j:
				M[j] += 1
	ans = 0
	for i in range(12):
		if M[i] == n or M[i] == 0:
			pass
		else:
			ans += 2 ** i
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a[0]
	ans = 0
	for i in range(n):
		ans = ans | a[i]
		b = b & a[i]
	print(ans - b)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(x) for x in input().split()]
	n_bits = [0 for i in range(11)]
	for num in nums:
		for i in range(11):
			if num % 2 == 1:
				n_bits[i] += num % 2
			num //= 2
	s = 0
	v = 1
	for i in range(11):
		if n_bits[i] != 0 and n_bits[i] != n:
			s += v
		v *= 2
	print(s)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[0] | a[1]
	c = a[0] & a[1]
	for i in range(2, n):
		b = b | a[i]
		c = c & a[i]
	print(abs(b - c))


t = int(input())
for z in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	And = arr[0]
	Or = arr[0]
	for i in arr:
		And = And & i
		Or = Or | i
	print(Or - And)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(mx, mn) = (a[0], a[0])
	for i in range(1, n):
		mx |= a[i]
		mn &= a[i]
	print(mx - mn)


for _ in range(int(input())):
	input()
	mn = 1023
	mx = 0
	for v in map(int, input().split()):
		mn &= v
		mx |= v
	print(mx - mn)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	max_a = 0
	for i in range(n):
		max_a = a[i] | max_a
	min_a = max(a)
	for i in range(n):
		min_a = min_a & a[i]
	print(max_a - min_a)


import sys
import math
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	(o, a) = (l[0], l[0])
	for i in range(1, n):
		o = o | l[i]
		a = a & l[i]
	print(o - a)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	res = 0
	cnt = [0] * 20
	for i in nums:
		for x in range(20):
			if i & 1 << x:
				cnt[x] += 1
	mx = 0
	for i in range(20):
		if cnt[i]:
			mx ^= 1 << i
	mi = float('inf')
	for i in nums:
		for x in range(20):
			if i & 1 << x:
				if cnt[x] < n:
					i ^= 1 << x
		mi = min(mi, i)
	print(mx - mi)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().strip().split()]
	m_a = bin(max(a))[2:]
	mi_a = bin(min(a))[2:]
	m_le = len(m_a)
	mi_a = '0' * (m_le - len(mi_a)) + mi_a
	a = [bin(a[i])[2:] for i in range(n)]
	a = ['0' * (m_le - len(a[i])) + a[i] for i in range(n)]
	for i in range(m_le):
		if m_a[i] == '0':
			for j in range(n):
				if a[j][i] == '1':
					m_a = m_a[:i] + '1' + m_a[i + 1:]
					break
		if mi_a[i] == '1':
			for j in range(n):
				if a[j][i] == '0':
					mi_a = mi_a[:i] + '0' + mi_a[i + 1:]
					break
	print(int(m_a, 2) - int(mi_a, 2))


t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().split()]
	(mi, ma) = (a[0], a[0])
	for i in a:
		mi &= i
		ma |= i
	print(ma - mi)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ad = l[0]
	ar = l[0]
	for x in range(1, n):
		ad = ad & l[x]
		ar = ar | l[x]
	print(ar - ad)


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	cnt = [0] * 30
	for num in a:
		for i in range(30):
			if num >> i & 1:
				cnt[29 - i] += 1
	mx = ['1'] * 30
	mn = ['0'] * 30
	for i in range(30):
		if cnt[i] == n:
			mx[i] = '0'
		elif cnt[i] == 0:
			mn[i] = '1'
	print(int(''.join(mx), 2) - int(''.join(mn), 2))


import os
import math
import collections
import bisect
import functools
import itertools
import heapq

def solve(arr, n):
	K = 10
	(ma, mi) = (0, (1 << 10) - 1)
	for i in range(K):
		for x in arr:
			if x & 1 << i:
				ma |= 1 << i
			else:
				mi &= ~(1 << i)
	return ma - mi
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = l[0] & l[1]
	for i in l[2:]:
		p = p & i
	c = l[0] | l[1]
	for i in l[2:]:
		c = c | i
	print(c - p)


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

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

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

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

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = inp()
for i in range(t):
	n = inp()
	l = li()
	ar = [0] * 10
	for i in l:
		b = bin(i)
		k = 0
		for j in range(len(b) - 1, 1, -1):
			if b[j] == '1':
				ar[k] += 1
			k += 1
	(m1, m2) = (0, 0)
	for i in range(len(ar)):
		if ar[i] >= n:
			m1 += 1 * pow(2, i)
		if ar[i] > 0:
			m2 += 1 * pow(2, i)
	print(m2 - m1)


from itertools import zip_longest

def solve(a: list) -> int:
	nums = [list(map(int, bin(i)[2:][::-1])) for i in a]
	nums_T = list(zip_longest(*nums, fillvalue=0))
	max_str = ''.join(map(str, map(max, nums_T)))[::-1]
	min_str = ''.join(map(str, map(min, nums_T)))[::-1]
	return int(max_str, base=2) - int(min_str, base=2)

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		res = solve(a)
		print(res)
main()


def exe():
	a = min(lst)
	b = min(lst)
	lst.sort()
	for i in range(1, n):
		b = b | lst[i]
	for i in range(1, n):
		a = a & lst[i]
	return b - a
for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(exe())


def solve():
	n = int(input())
	nums = list(map(int, input().split()))
	maxx = [0] * 10
	minn = [1] * 10
	for num in nums:
		for bit in range(10):
			if num & 1 << bit:
				maxx[bit] = 1
			else:
				minn[bit] = 0
	r1 = 0
	r2 = 0
	for i in range(10):
		if maxx[i] == 1:
			r1 = r1 | 1 << i
		if minn[i] == 1:
			r2 = r2 | 1 << i
	print(r1 - r2)
T = int(input())
for _ in range(T):
	solve()


from collections import defaultdict, Counter
graph = defaultdict(set)

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

def solve(array):
	maximum = 0
	minimum = 1023
	if max(array) == min(array):
		return 0
	for num in array:
		maximum |= num
		minimum &= num
	return maximum - minimum
for _ in range(int(input())):
	n = int(input())
	array = listinput()
	print(solve(array))


from functools import reduce
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(reduce(lambda x, y: x | y, a) - reduce(lambda x, y: x & y, a))


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

def stri():
	return list(input().split())
y = number()
while y[0] > 0:
	num = number()
	n = num[0]
	ma = 0
	mi = 2047
	arr = number()
	for x in arr:
		ma = ma | x
		mi = mi & x
	print(ma - mi)
	y[0] -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mina = a[0]
	maxa = a[0]
	for i in range(len(a)):
		mina = mina | a[i]
		maxa = maxa & a[i]
	print(mina - maxa)


t = int(input())
while t:
	n = int(input())
	arr = []
	str = ''
	str = input()
	arr = str.rsplit()
	for i in range(0, n):
		arr[i] = int(arr[i])
	max = arr[0]
	min = arr[0]
	for i in range(1, n):
		max = max | arr[i]
		min = min & arr[i]
	print(max - min)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = min(arr)
	for i in range(9, -1, -1):
		if mini & 1 << i:
			for j in range(n):
				if arr[j] & 1 << i:
					continue
				else:
					mini = mini & ~(1 << i)
					break
	maxi = max(arr)
	for i in range(9, -1, -1):
		if maxi & 1 << i:
			continue
		else:
			for j in range(n):
				if arr[j] & 1 << i:
					maxi = maxi | 1 << i
					break
	print(maxi - mini)


for _ in range(int(input())):
	n = int(input())
	array = [int(i) for i in input().split()]
	maxi = array[0]
	mini = array[0]
	for i in range(1, len(array)):
		maxi = maxi | array[i]
		mini = mini & array[i]
	print(maxi - mini)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans1 = l[0]
	for i in l:
		ans1 = ans1 | i
	ans2 = l[0]
	for i in l:
		ans2 = ans2 & i
	print(ans1 - ans2)


from math import inf
import sys
input = sys.stdin.readline
inp = sys.stdin.readline

def input():
	return inp().strip()

def ii():
	return int(input())

def mi():
	return map(int, input().split())

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

def solve():
	n = ii()
	arr = li()
	min_ = arr[0]
	max_ = arr[0]
	for i in range(1, n):
		max_ = max_ | arr[i]
		min_ = min_ & arr[i]
	print(max_ - min_)

def main():
	for _ in range(ii()):
		solve()
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	for i in range(n):
		a[i] = bin(a[i])[2:]
	ml = len(a[-1])
	(mi, ma) = ('', '')
	for i in range(ml):
		(one, zero) = (0, 0)
		for x in a:
			if i + 1 <= len(x):
				if x[::-1][i] == '0':
					zero += 1
				else:
					one += 1
			else:
				zero += 1
		if one != 0 and zero != 0:
			ma = '1' + ma
			mi = '0' + mi
		elif one == 0:
			mi = '0' + mi
			ma = '0' + ma
		else:
			mi = '1' + mi
			ma = '1' + ma
	print(int(ma, 2) - int(mi, 2))


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	num = lst[0]
	num2 = lst[0]
	for i in lst:
		num = num | i
		num2 = num2 & i
	print(num - num2)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = arr[0]
	b = arr[0]
	for it in arr:
		a = a | it
		b = b & it
	print(a - b)


t = int(input())
while t > 0:
	n = int(input())
	li = list(map(int, input().split()))
	l = []
	s = ''
	for i in range(11):
		l.append(0)
	for i in li:
		s = '{0:011b}'.format(i)
		for j in range(11):
			if s[j] == '1':
				l[j - 1] += 1
	pp = 0
	for i in range(n):
		pp += 1
		pp -= 1
	maxi = ''
	mini = ''
	for i in l:
		if i == 0:
			maxi += '0'
			mini += '0'
		elif i < n:
			maxi += '1'
			mini += '0'
		elif i == n:
			maxi += '1'
			mini += '1'
	print((int(maxi, 2) - int(mini, 2)) // 2)
	t -= 1


def binaryToDecimal(binary):
	(decimal, i) = (0, 0)
	while binary != 0:
		dec = binary % 10
		decimal = decimal + dec * pow(2, i)
		binary = binary // 10
		i += 1
	return decimal
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dp = [format(arr[i], '010b') for i in range(n)]
	ma = [0 for i in range(10)]
	mi = [0 for i in range(10)]
	for i in range(10):
		flag1 = 0
		flag2 = 0
		for j in range(n):
			if dp[j][i] == '1':
				flag1 = 1
			if dp[j][i] == '0':
				flag2 = 1
		if flag1 == 1:
			ma[i] = 1
		if flag2 == 0:
			mi[i] = 1
	ma = [str(ma[i]) for i in range(10)]
	mi = [str(mi[i]) for i in range(10)]
	ma = int(''.join(ma))
	mi = int(''.join(mi))
	ma = binaryToDecimal(ma)
	mi = binaryToDecimal(mi)
	print(ma - mi)


for _ in range(int(input())):
	n34 = int(input())
	arr12 = list(map(int, input().split()))
	(a2, b2) = (arr12[0], arr12[0])
	for i in range(1, n34):
		a2 = a2 | arr12[i]
		b2 = b2 & arr12[i]
	print(a2 - b2)


tests = int(input())
for i in range(tests):
	length = int(input())
	arr = list(map(int, input().split()))
	bits_arr = [0] * 12
	for j in arr:
		for k in range(12):
			if j & 1 << k:
				bits_arr[k] += 1
	maximum = 0
	minimum = 0
	for i in range(12):
		if bits_arr[i] == length:
			minimum |= 1 << i
		if bits_arr[i] > 0:
			maximum |= 1 << i
	print(maximum - minimum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x1 = a[0]
	x2 = a[0]
	for i in range(1, n):
		x1 = x1 | a[i]
		x2 = x2 & a[i]
	if max(a) == min(a):
		print(0)
	else:
		print(x1 - x2)


t = int(input())
while t > 0:
	r = [0] * 10
	n = int(input())
	L = list(map(int, input().split()))
	MIN = min(L)
	MAX = MIN
	tt = []
	ttt = MIN
	while ttt:
		temp = ttt % 2
		tt.append(temp)
		ttt //= 2
	length = len(tt)
	for i in L:
		index = 0
		while i:
			temp = i % 2
			MAX = MAX | temp << index
			if index < length:
				tt[index] = tt[index] & temp
			i = i // 2
			index += 1
	MIN = 0
	for (index, value) in enumerate(tt):
		MIN |= value << index
	print(MAX - MIN)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = a[0]
	temp = a[0]
	for i in range(1, n):
		count = count | a[i]
		temp = temp & a[i]
	result = count - temp
	print(result)


for _ in range(int(input())):
	n = int(input())
	mua = list(map(int, input().split()))
	fre = mua[0]
	ref = mua[0]
	for i in range(1, n):
		fre = fre | mua[i]
		ref = ref & mua[i]
	print(fre - ref)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	bits = 0
	for a in arr:
		bits = max(bits, a.bit_length())
	(mn, mx) = (0, 0)
	for i in range(bits):
		(temp1, temp2) = (1, 0)
		for a in arr:
			temp = a >> i & 1
			temp1 = min(temp, temp1)
			temp2 = max(temp, temp2)
			if temp1 == 0 and temp2 == 1:
				break
		mx = mx | temp2 << i
		mn = mn | temp1 << i
	print(mx - mn)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = l[0]
	y = l[0]
	for i in range(1, n):
		x = x | l[i]
		y = y & l[i]
	print(x - y)


def g(L):
	n = len(L)
	if n == 1:
		return 0
	S = sorted(L)
	for i in range(n):
		S[i] = bin(S[i])[2:]
	x = len(S[-1])
	P = []
	max = list(S[-1])
	min = list(S[0])
	y = len(min)
	for i in range(x):
		if max[i] == '0':
			P.append(i - x)
	for j in P:
		for k in S:
			if len(k) >= -1 * j and k[j] == '1':
				max[j] = '1'
				break
	B = []
	for i in range(y):
		if min[i] == '1':
			B.append(i - y)
	for c in B:
		for w in S:
			if w[c] == '0':
				min[c] = '0'
				break
	maxm = ''
	minm = ''
	for k in max:
		maxm += k
	for u in min:
		minm += u
	return int(maxm, 2) - int(minm, 2)
x = int(input())
for i in range(x):
	y = int(input())
	L = list(map(int, input().split()))
	print(g(L))


t = int(input())
answer1 = 'YES'
answer2 = 'NO'
zero = 0
one = 1
two = 2

def main():
	for q in range(t):
		n = int(input())
		a = [int(i) for i in input().split()]
		m1 = a[zero]
		m2 = a[zero]
		for j in range(one, len(a)):
			m1 = m1 | a[j]
			m2 = m2 & a[j]
			res = m1 - m2
		print(res)
main()


t = int(input())
for i in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	o = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	l = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	for j in range(len(L)):
		a = format(L[j], 'b')
		if len(a) < 11:
			a = '0' * (11 - len(a)) + a
		for m in range(11):
			if a[m] == '0':
				if l[m] != 0:
					l[m] = 0
			if a[m] == '1':
				if o[m] != 1:
					o[m] = 1
	maxi = int('0b' + ''.join(map(str, o)), 2)
	mini = int('0b' + ''.join(map(str, l)), 2)
	print(maxi - mini)


import heapq
import math

def solve():
	n = int(input())
	a = input().split(' ')
	a = [int(x) for x in a]
	res = 0
	h = {}
	for i in range(len(a)):
		j = 0
		v = a[i]
		while v > 0:
			if v % 2 == 1:
				h[j] = h.get(j, 0) + 1
			v //= 2
			j += 1
	if not h:
		print(0)
		return
	k = max(h.keys())
	l = 0
	s = 0
	for i in range(k, -1, -1):
		if i in h:
			l += 2 ** i
			if h[i] == n:
				s += 2 ** i
	print(l - s)
	return
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	mn = a[0]
	mx = a[0]
	for i in a:
		mn = mn & i
		mx = mx | i
	print(mx - mn)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	maxa = a[0]
	mina = a[0]
	for i in range(1, n):
		maxa = maxa | a[i]
		mina = mina & a[i]
	print(maxa - mina)


T = int(input(''))
for i in range(T):
	n = int(input(''))
	str1 = input('')
	l1 = [int(c) for c in str1.split()]
	l2 = [[] for c in range(len(l1))]
	for j in range(len(l1)):
		x = l1[j]
		while x != 0:
			l2[j].insert(0, x % 2)
			x = x // 2
		while len(l2[j]) < 10:
			l2[j].insert(0, 0)
	count = [[0, 1] for c in range(10)]
	for j in range(10):
		for k in range(len(l2)):
			if l2[k][j] == 1:
				count[j][0] = 1
			else:
				count[j][1] = 0
	minNum = 0
	maxNum = 0
	my_dict = {0: 512, 1: 256, 2: 128, 3: 64, 4: 32, 5: 16, 6: 8, 7: 4, 8: 2, 9: 1}
	for k in range(len(count)):
		if count[k][0] == 1:
			maxNum += my_dict[k]
		if count[k][1] == 1:
			minNum += my_dict[k]
	print(maxNum - minNum)


for b in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	la = []
	s = ''
	for k in range(11):
		la.append(0)
	for i in lis:
		s = '{0:011b}'.format(i)
		for j in range(11):
			if s[j] == '1':
				la[j - 1] += 1
	ct = 0
	for i in range(n):
		ct = ct + 1
		ct = ct - 1
	v = 100
	while v > 100:
		v = v - 2
	ma = ''
	mi = ''
	for m in la:
		if m == 0:
			ma += '0'
			mi += '0'
		elif m < n:
			ma += '1'
			mi += '0'
		elif m == n:
			ma += '1'
			mi += '1'
	print((int(ma, 2) - int(mi, 2)) // 2)


import math

def func(n, a):
	t1 = 1023
	t2 = 0
	for j in a:
		t1 = t1 & j
		t2 = t2 | j
	return t2 - t1
q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	print(func(n, a))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = 0
	o = 0
	for j in range(15):
		for k in range(n):
			if l[k] >> j & 1:
				m += 2 ** j
				break
	for j in range(15):
		c = 0
		for k in range(n):
			if l[k] >> j & 1:
				c += 1
		if c == n:
			o += 2 ** j
	print(m - o)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	uray = list(arr)
	marray = list(uray)
	r = 9
	s = 8
	p = arr[0]
	q = arr[-1]
	m = uray[0]
	if 3 > 0:
		x = int(arr[0])
		y = int(arr[0])
		if m >= 0:
			for i in range(n):
				x = x | arr[i]
				y = y & arr[i]
			print(abs(x - y))
		else:
			print('none')
	elif 9 > 0 and 0 < 9:
		x = int(arr[0])
		y = int(arr[0])
		for i in range(n):
			x = x | arr[i]
			y = y & arr[i]
		print(abs(x - y))
		print('hdgdg')
		print('37782')


def balu():
	for vamshi in range(int(input())):
		nnnnnn = int(input())
		arrrrr = [int(i) for i in input().split()]
		maximum = arrrrr[0]
		minimum = arrrrr[0]
		for i in range(1, len(arrrrr)):
			maximum = maximum | arrrrr[i]
			minimum = minimum & arrrrr[i]
		print(maximum - minimum)
balu()


from collections import Counter

def lstRd() -> list:
	return list(map(int, input().split()))

def intRd():
	return int(input())

def lstStrRd():
	return list(input())
for i in range(intRd()):
	n = intRd()
	a = lstRd()
	maxx = a[0]
	minn = a[0]
	for i in range(1, n):
		maxx = maxx | a[i]
		minn = minn & a[i]
	print(maxx - minn)


testcases = int(input())
for i in range(testcases):
	length = int(input())
	array = list(map(int, input().split()))
	tanu = array[0]
	sree = array[0]
	for i in range(1, length):
		tanu = tanu | array[i]
		sree = sree & array[i]
	print(tanu - sree)