Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.

There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). 

Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$.

Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number.

For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same.


-----Input-----

In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. 

In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$.

It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$.


-----Output-----

Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists.


-----Example-----
Input
6
4
1 0 2 3
6
10 7 14 8 3 12
2
0 2
3
1 2 3
6
1 4 6 10 11 12
2
0 1023

Output
1
4
2
-1
-1
1023



-----Note-----

In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.

Source: codeforces | Difficulty: EASY
Tags: Bit manipulation

Complete search


Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		flag = True
		for j in a:
			if i ^ j not in a:
				flag = False
				break
		if flag:
			ans = i
			break
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	if n % 2 == 1:
		print(-1)
		continue
	s = set()
	for i in range(n):
		t = set()
		for j in range(n):
			if i != j:
				t.add(a[i] ^ a[j])
		if i == 0:
			s = t
		else:
			s = s & t
	if len(s) == 0:
		print(-1)
	else:
		print(min(s))


import sys
import os
import heapq
import math
try:
	path = './file/input.txt'
	if os.path.exists(path):
		sys.stdin = open(path, 'r')
except:
	pass
t = int(input())

def printd(value):
	pass

def case():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	answer = -1
	arr.sort()
	for index in range(1, 1025):
		result = []
		for number in arr:
			result.append(number ^ index)
		result.sort()
		isSame = True
		for i in range(n):
			if arr[i] != result[i]:
				isSame = False
		if isSame:
			answer = index
			break
	print(answer)
for _ in range(t):
	case()


t = int(input())
for _ in range(t):
	n = int(input())
	(*arr,) = map(int, input().split())
	orig_s = set(arr)
	res = -1
	for i in range(1, 1025):
		new_s = set()
		for v in orig_s:
			new_s.add(v ^ i)
		if new_s == orig_s:
			res = i
			break
	print(res)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = li()
	x = a[0]
	f = 0
	s = set(a)
	for i in range(1, 1025):
		b = set([])
		for j in range(n):
			b.add(a[j] ^ i)
		if s == b:
			print(i)
			return
	print(-1)
for _ in range(I()):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = set(l)
	m = max(l)
	flag = 0
	for k in range(1, 1024):
		new = [k ^ i for i in l]
		curr = set(new)
		if curr == a:
			p = k
			flag = 1
			break
	if flag == 1:
		print(p)
	else:
		print(-1)


def solve(n, s):
	k = 1
	s.sort()
	track = []
	while k <= 1024:
		for i in range(n):
			track.append(s[i] ^ k)
		if set(track) == set(s):
			print(k)
			return
		else:
			track = []
			k += 1
	print(-1)
	return
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	solve(n, s)


t = int(input())
for T in range(t):
	n = int(input())
	lista = [int(x) for x in input().split()]
	ma = max(lista)
	mi = min(lista)
	listb = [0] * n
	ans = 0
	for k in range(1, 2 * ma + 1):
		temp = 0
		for i in range(n):
			listb[i] = lista[i] ^ k
			tk = 0
			if listb[i] not in lista:
				temp = 1
				break
		if temp == 0:
			ans = 1
			print(k)
			break
	if ans == 0:
		print(-1)


cases = int(input())
for i in range(cases):
	line1 = int(input())
	line2 = input().split(' ')
	arr = []
	for a in line2:
		arr.append(int(a))
	if len(arr) == 2:
		print(arr[0] ^ arr[1])
		continue
	elif len(arr) % 2 == 1:
		print(-1)
		continue
	pos_ans = []
	for b in range(len(arr)):
		ele1 = arr[b]
		for c in range(b + 1, len(arr)):
			ele2 = arr[c]
			op = ele1 ^ ele2
			pos_ans.append(op)
	pos_ans.sort()
	counter = {}
	found = False
	for d in pos_ans:
		if d in counter:
			counter[d] += 1
		else:
			counter[d] = 1
	for key in counter:
		value = counter[key]
		if value == line1 / 2:
			found = True
			ans = key
			break
	if found:
		print(ans)
	else:
		print(-1)


def BS(x):
	for i in range(1, 1024):
		if func(i, x) == x:
			return i
	return -1

def func(i, x):
	d = set()
	for j in list(x):
		d.add(j ^ i)
	return d
t = int(input())
while t > 0:
	t -= 1
	n = input()
	x = set(map(int, input().split()))
	print(BS(x))


import math
t = int(input())
while t != 0:
	n = int(input())
	arrS = list(map(int, input().split()))
	bitArr = [0] * 1024
	for i in range(len(arrS)):
		for j in range(i + 1, len(arrS)):
			bitArr[arrS[i] ^ arrS[j]] += 1
	result = 1024
	for i in range(len(bitArr)):
		if bitArr[i] == math.floor(n / 2):
			result = i
			break
	if n % 2 != 0:
		print(-1)
	elif result == 1024:
		print(-1)
	else:
		print(result)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	S = list(map(int, input().split()))
	if n == 1:
		print(-1)
		continue
	if n == 2:
		print(S[0] ^ S[1])
		continue
	S.sort()
	ans = -1
	for k in range(1, 1024):
		R = sorted([x ^ k for x in S])
		ans = k
		for i in range(n):
			if S[i] != R[i]:
				ans = -1
				break
		if ans > 0:
			break
	print(ans)


t = int(input())

def get_k(a, b):
	return a ^ b

def solve(s, n):
	ans = -1
	if n > 1:
		k_vals = set()
		num1 = s[0]
		for idx in range(1, n):
			k = get_k(num1, s[idx])
			k_vals.add(k)
		s.sort()
		for k_val in sorted(k_vals):
			new_s = []
			for i in s:
				new_s.append(i ^ k_val)
			new_s.sort()
			if new_s == s:
				ans = k_val
				break
	return ans
answers = []
for i in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	ans = solve(s, n)
	answers.append(ans)
for i in answers:
	print(i)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	cnt = 0
	for k in range(1, 1025):
		ans = set()
		for i in s:
			ans.add(i ^ k)
		if s == ans:
			cnt = 1
			break
	if cnt == 1:
		print(k)
	else:
		print(-1)


def tran(arr):
	arr.sort()
	p = 0
	anss = []
	for i in range(1, 1024):
		c = []
		for k in arr:
			b = k ^ i
			c.append(b)
		c.sort()
		if c == arr:
			anss.append(i)
			break
	if len(anss) == 1:
		return anss[0]
	else:
		return -1
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	r = tran(arr)
	ans.append(r)
for i in ans:
	print(i)


import sys
input = sys.stdin.readline
output = sys.stdout

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

def solve(n, l):
	res = -1
	set1 = set(l)
	list1 = []
	for i in range(1, 1025):
		list1 = []
		for j in range(n):
			list1.append(l[j] ^ i)
		if set1 == set(list1):
			res = i
			break
	output.write(str(res) + '\n')
t = int(input())
for i in range(t):
	n = int(input())
	l = inlt()
	solve(n, l)


import sys
import math

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
answers = []

def check_k(presence, arr, k):
	for a in arr:
		if not presence[a ^ k]:
			return False
	return True
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	presence = [False for i in range(2048)]
	for a in arr:
		presence[a] = True
	ans = -1
	for k in range(1, 2048):
		if check_k(presence, arr, k):
			ans = k
			break
	answers.append(ans)
print(*answers, sep='\n')


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temarr = input()
	temarr = temarr.split()
	arr = []
	for i in temarr:
		arr.append(int(i))
	arr = sorted(arr)
	dicts = {}
	for i in arr:
		dicts[i] = 1
	ans = -1
	for i in range(1, 1025):
		newdicts = {}
		flag = 0
		for j in arr:
			new = i ^ j
			if new not in dicts:
				flag = 1
				break
			if new in newdicts:
				flag = 1
				break
			newdicts[new] = 1
		if flag == 0:
			ans = i
			break
	print(ans)


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
	return input()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def powermod(a, b, m):
	a %= m
	res = 1
	if a == 0:
		return 0
	while b > 0:
		if b & 1:
			res = res * a % m
		b = b >> 1
		a = a * a % m
	return res

def isprime(n):
	if n <= 1:
		return 0
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return 0
	return 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		f = 0
		a.sort()
		for i in range(1, 1025):
			x = []
			for j in range(n):
				x.append(a[j] ^ i)
			x.sort()
			if a == x:
				print(i)
				f = 1
				break
		if f == 0:
			print('-1')
solve()


for _ in range(int(input())):
	n = int(input())
	ans = -1
	l = set(map(int, input().split()))
	for i in range(1, 4 * 10 ** 3):
		s1 = set()
		for j in l:
			s1.add(i ^ j)
		if s1 == l:
			ans = i
			break
	print(ans)


kl = int(input())
for l in range(kl):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n % 2 == 1:
		print(-1)
		continue
	if n % 4 != 0:
		(m, pr) = (0, 1)
		for i in range(n):
			m = m ^ a[i]
		for i in range(n):
			c = a[:]
			c.remove(a[i])
			if m ^ a[i] not in c:
				pr = 0
				break
		if pr:
			print(m)
		else:
			print(-1)
		continue
	for m in range(1024):
		pr = 1
		for i in range(n):
			c = a[:]
			c.remove(a[i])
			if m ^ a[i] not in c:
				pr = 0
				break
		if pr:
			break
	if pr:
		print(m)
	else:
		print(-1)


import sys
input = sys.stdin.readline
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
for _ in range(inp()):
	n = inp()
	x = ip()
	if n % 2:
		print(-1)
		continue
	dt = {i: 0 for i in x}
	for i in range(1, 1025):
		flag = 1
		for j in range(n):
			t = i ^ x[j]
			if dt.get(t, 1):
				flag = 0
				break
		if flag:
			break
	if flag:
		print(i)
	else:
		print(-1)


t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 1
	while i < 1024:
		flag = 1
		for j in range(0, len(a)):
			z = i ^ a[j]
			if z in a:
				flag = 0
			else:
				flag = 1
				break
		if flag == 0:
			break
		i += 1
	if flag == 0:
		print(i)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	dic = {}
	for j in a:
		dic[j] = 0
	i = 1
	boo = False
	while i <= 1025:
		if a[0] ^ i in dic:
			temp = []
			for ex in a:
				temp.append(ex ^ i)
			if sorted(temp) == sorted(a):
				boo = True
		if boo:
			break
		i += 1
	ans = i if boo else -1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	ls = [int(a) for a in input().split()]
	found = False
	for j in range(1, 1025):
		for k in range(n):
			if ls[k] ^ j in ls:
				pass
			else:
				break
			if k == n - 1:
				found = True
				ans = j + 1
		if found:
			break
	if found:
		print(j)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().rstrip().split()))
	orig = {}
	for i in s:
		orig[i] = True
	req = -1
	for i in range(1, 1025):
		new = {}
		for j in s:
			if j ^ i in orig:
				new[j ^ i] = True
			else:
				break
		if len(orig) == len(new):
			req = i
			break
	print(req)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	s = set(arr)
	for i in range(1, 1025):
		news = set()
		for el in arr:
			news.add(el ^ i)
		if news == s:
			print(i)
			return
	print(-1)
tt = int(input())
for test in range(tt):
	solve()


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	tmp1 = -1
	l.sort()
	l1 = []
	for i in range(1, 1024 + 1):
		for j in range(len(l)):
			l1.append(l[j] ^ i)
		l1.sort()
		if l1 == l:
			tmp1 = i
			break
		l1 = []
	print(tmp1)


a = int(input())
for _ in range(a):
	x = int(input())
	li = list(map(int, input().split()))
	p = -1
	if x == 1:
		print(p)
	elif x == 2:
		print(li[0] ^ li[1])
	else:
		for i in range(1, 1025):
			for j in li:
				if j ^ i not in li:
					break
			else:
				p = i
				break
		print(p)


T = int(input())
while T:
	n = int(input())
	S = list(map(int, input().split()))
	k = 1
	f = True
	while k < 1025 and f == True:
		Z = set()
		for i in S:
			Z.add(i ^ k)
		if set(S) == Z:
			f = False
			break
		k = k + 1
	if f == False:
		print(k)
	else:
		print(-1)
	T = T - 1


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = -1
	for k in range(1, 1025):
		st = set(s)
		for i in s:
			val = i ^ k
			if val not in st:
				break
			st.remove(val)
		if not st:
			ans = k
			break
	print(ans)


from sys import stdin as cin
from itertools import accumulate
lmap = lambda f, it: list(map(f, it))
laccumulate = lambda it, *f: list(accumulate(it, *f))

def min_xor(s):
	if len(s) % 2 == 1:
		return -1
	s.sort(reverse=True)
	for n in s[1:]:
		k = s[0] ^ n
		t = [k ^ x for x in s]
		if s == sorted(t, reverse=True):
			return k
	return -1

def main():
	t = int(next(cin).strip())
	for i in range(t):
		next(cin)
		s = lmap(int, next(cin).strip().split())
		print(min_xor(s))
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	visited = [0] * 1025
	ans = -1
	maxi = max(l)
	mini = min(l)
	for i in range(n):
		visited[l[i]] = True
	for i in range(1, 1024):
		flag = 0
		for j in l:
			p = j ^ i
			if p <= mini and p >= maxi:
				flag = 1
				break
			elif visited[p] == False:
				flag = 1
				break
		if flag == 0:
			ans = i
			break
	print(ans)


for x in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	k = set(m)
	r = 0
	for i in range(1, 1025):
		if set([i ^ k for k in m]) == k:
			print(i)
			r = 1
			break
	if r == 0:
		print(-1)


def main():
	a = int(input())
	for x in range(a):
		n = int(input())
		b = list(map(int, input().split()))
		temp = set()
		st = 0
		for y in range(1, 1024):
			for z in b:
				temp.add(z ^ y)
			if temp == set(b):
				print(y)
				st = 1
				break
			temp.clear()
		if st == 1:
			continue
		print(-1)
main()


n = int(input())
for i in range(0, n):
	o = int(input())
	p = input().rstrip().split(' ')
	C = list(p)
	G = 1
	C.sort(key=int)
	for j in range(1, 1150):
		L = []
		for k in range(0, len(p)):
			A = int(p[k]) ^ j
			L.append(str(A))
		L.sort(key=int)
		if L == C:
			print(j)
			G = 0
			break
	if G == 1:
		print(-1)


def solve(numbers):
	numbers = set(numbers)
	for i in range(1, 1024):
		if set(map(lambda x: x ^ i, numbers)) == numbers:
			return i
	return -1
for T in range(int(input())):
	int(input())
	print(solve(map(int, input().split())))


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	fl = 0
	for i in range(1, 1025):
		new_s = set([el ^ i for el in s])
		if new_s == s:
			print(i)
			fl = 1
			break
	if fl == 0:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	flag = 0
	for i in range(1, 1025):
		s = [x ^ i for x in l]
		s.sort()
		if s == l:
			flag = 1
			print(i)
			break
	if flag == 0:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_right
from collections import Counter
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ls = sorted(arr)
	f = 0
	for i in range(1, 1025):
		s = set()
		for j in range(n):
			var = arr[j] ^ i
			if var not in s:
				s.add(var)
			else:
				break
		if sorted(list(s)) == ls:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		flag = 0
		for j in a:
			if i ^ j not in a:
				flag = 1
				break
		if flag == 0:
			ans = i
			break
	print(ans)


from math import log
t = int(input())
for choices in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if max(arr) == 0:
		m = 2
	else:
		m = 2 ** (int(log(max(arr), 2)) + 1)
	s1 = set(arr)
	ans = -1
	for i in range(1, m):
		s2 = set()
		for j in range(n):
			s2.add(arr[j] ^ i)
		if s1 == s2:
			ans = i
			break
	print(ans)


from math import inf as inf
from math import *
from collections import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = set(a)
	flag = 0
	for i in range(1, 1025):
		f = set()
		for j in a:
			f.add(j ^ i)
		if f == d:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


from collections import Counter
from sys import stdin, stdout
for t in range(int(stdin.readline().strip())):
	n = int(stdin.readline().strip())
	a = list(map(int, stdin.readline().strip().split()))
	a.sort()
	b = a[0]
	x = []
	for i in a:
		x.append(i ^ b)
	x.sort()
	p = -1
	for i in x[1:]:
		temp = [i ^ k for k in a]
		if Counter(a) == Counter(temp):
			p = i
			break
	stdout.write(str(p) + '\n')


n = int(input())
for i in range(n):
	v = int(input())
	s = input().split(' ')
	s = [int(i) for i in s]
	set_s = set(s)
	rez = -1
	for i in range(1, 1025):
		s_copy = set(set_s)
		for j in s:
			xor = j ^ i
			if xor in s_copy:
				s_copy.remove(xor)
			else:
				break
		else:
			rez = i
			break
	print(rez)


t = int(input())
results = []
for i in range(t):
	n = int(input())
	vals = input().split()
	S = {int(val) for val in vals}
	digits = bin(max(S))[2:]
	loopMax = 1 << len(digits)
	for k in range(1, loopMax):
		S2 = set()
		hasDup = False
		for s in S:
			x = s ^ k
			if x in S2:
				hasDup = True
				break
			else:
				S2.add(x)
		if not hasDup and S == S2:
			results.append(k)
			break
	else:
		results.append(-1)
for result in results:
	print(result)


def check(k, exists, given):
	for num in given:
		if not exists[num ^ k]:
			return False
	return True

def solve(exists, given):
	for k in range(1, 1025):
		if check(k, exists, given):
			return k
	return -1
tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	exists = [0 for i in range(2048)]
	for i in arr:
		exists[i] = 1
	print(solve(exists, arr))


for i in range(int(input())):
	n = int(input())
	r = 0
	flag = 0
	s = list(map(int, input().split()))
	s.sort()
	for i in range(1, 1025):
		b = []
		for j in s:
			b.append(j ^ i)
		b.sort()
		if s == b:
			flag = 1
			r = i
			break
	if flag == 1:
		print(r)
	else:
		print(-1)


import math
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	for i in range(1, 1025):
		b = []
		for j in a:
			b.append(j ^ i)
		s2 = set(b)
		if s == s2:
			print(i)
			break
	else:
		print(-1)


def f(A):
	A.sort()
	for i in range(1, 1024):
		B = []
		for j in A:
			B.append(i ^ j)
		B.sort()
		if A == B:
			return i
	return -1
t = int(input())
for i in range(t):
	n = input()
	B = input().split(' ')
	A = []
	for i in B:
		A.append(int(i))
	print(f(A))


t = int(input())
for _ in range(t):
	n = int(input())
	S = set()
	for s in input().split():
		S.add(int(s))
	flag = False
	for k in range(1, 1025):
		new_S = set()
		for s in S:
			new_S.add(s ^ k)
		if S == new_S:
			print(k)
			flag = True
			break
	if not flag:
		print('-1')


for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	st = set(l)
	ans = -1
	for i in range(1, 1024 + 1):
		st2 = set()
		for j in range(n):
			st2.add(l[j] ^ i)
		if st2 == st:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	for k in range(1, 1025):
		l = []
		for i in a:
			l.append(k ^ i)
		a.sort()
		l.sort()
		if l == a:
			print(k)
			f = 1
			break
	if f == 0:
		print(-1)


from collections import Counter
from itertools import combinations
T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	s = set(map(int, input().strip().split()))
	maxval = max(s)
	a = Counter()
	for e in s:
		q = e
		mv = maxval
		for j in range(12):
			if mv > 0:
				a[j, q % 2] += 1
			else:
				break
			q //= 2
			mv //= 2
	good_pos = set()
	bad_pos = set()
	for p in range(12):
		if a[p, 0] == a[p, 1] and a[p, 0] > 0:
			good_pos.add(p)
		elif a[p, 0] % 2 + a[p, 1] % 2:
			bad_pos.add(p)
	if bad_pos:
		print(-1)
		continue
	good_numbers = set()
	for r in range(1, len(good_pos) + 1):
		for ss in combinations(good_pos, r):
			curtstnum = sum((2 ** j for j in ss))
			newset = {a ^ curtstnum for a in s}
			if s == newset:
				good_numbers.add(curtstnum)
	if good_numbers:
		print(min(good_numbers))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	mark = {}
	flag = 0
	for i in A:
		mark[i] = 1
	for i in range(1, 1025):
		p = 0
		for j in A:
			if mark.get(j ^ i, 0) != 0:
				p += 1
		if p == n:
			flag = 1
			break
	print(i) if flag == 1 else print(-1)


def solve():
	n = int(input())
	l = [int(x) for x in input().split()]
	d = {x: 0 for x in l}
	maxi = max(d)
	mini = min(d)
	exp = 10
	while maxi < pow(2, exp):
		exp -= 1
	exp += 1
	exp = pow(2, exp) - 1
	flag = False
	q = exp
	global count
	if len(l) == 1:
		print(-1)
	else:
		for exp in range(1, q + 1):
			count = 0
			for x in d:
				ele = x ^ exp
				if ele not in d:
					flag = False
					break
				else:
					flag = True
					count += 1
			if flag == True and count == len(l):
				print(exp)
				break
		if flag == False:
			print(-1)
t = int(input())
while t > 0:
	t -= 1
	solve()


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

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

def lm():
	return list(map(int, input().split()))
q = nn()
for _ in range(q):
	n = nn()
	s = lm()
	sset = set(s)
	xset = set(range(1, 2 * max(s)))
	for i in range(n):
		new_set = set()
		for j in range(n):
			new = s[i] ^ s[j]
			if new in xset:
				new_set.add(new)
		xset = new_set
	if len(xset) == 0:
		print(-1)
	else:
		print(min(xset))


def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	N = read_int()
	S = read_ints()
	if N % 2 == 1:
		return -1
	ks = []
	for i in range(1, N):
		ks.append(S[i] ^ S[0])
	ks.sort()
	S_set = set(S)
	for k in ks:
		if S_set == set((s ^ k for s in S)):
			return k
	return -1
T = read_int()
for _ in range(T):
	print(solve())


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n, s):
	t = s[0]
	possibilities = set()
	for (i, item) in enumerate(s):
		if i > 0:
			possibilities.add(t ^ item)
	s = set(s)
	for item in s:
		new_possibilities = set()
		for option in possibilities:
			if option ^ item in s:
				new_possibilities.add(option)
		possibilities = new_possibilities
	if len(possibilities) == 0:
		return -1
	best = float('inf')
	for option in possibilities:
		best = min(best, option)
	return best
for i in tests():
	n = int_in()
	s = arr_in()
	out(solve(n, s))


def N():
	return int(input())

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

def L():
	return list(NM())

def LN(n):
	return [N() for i in range(n)]

def LL(n):
	return [L() for i in range(n)]
t = N()

def f():
	n = N()
	l = set(L())
	if n % 2 == 1:
		print(-1)
		return
	x = l.pop()
	ans = 2000
	for i in l:
		k = x ^ i
		for j in l:
			if not (j == i or j ^ k in l):
				break
		else:
			ans = min(ans, k)
	if ans == 2000:
		print(-1)
		return
	else:
		print(ans)
for _ in range(t):
	f()


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputmatrix(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d
t = int(input())
for q in range(t):
	n = inputnums()
	s = inputlist()
	s.sort()
	done = False
	for i in range(1, 1024):
		a = []
		for j in range(len(s)):
			a.append(s[j] ^ i)
		a.sort()
		if a == s:
			print(i)
			done = True
			break
	if not done:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = sorted(list(map(int, input().split())))
	found = False
	for i in range(1, 1025):
		temp = sorted([i ^ elem for elem in lst])
		if temp == lst:
			print(i)
			found = True
			break
	if not found:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = {int(x) for x in input().split()}
	m = 2 ** (len(bin(max(s))) - 2) + 1
	for i in range(1, m):
		ns = {x ^ i for x in s}
		if s == ns:
			print(i)
			break
	else:
		print(-1)


def xor_k(n, a):
	outputs = []
	for j in a:
		i = j
		break
	for j in a:
		xor = i ^ j
		if xor != 0:
			outputs.append(xor)
	ans = []
	for j in outputs:
		l = []
		for k in a:
			l.append(k ^ j)
		if a == set(l):
			ans.append(j)
	if ans != []:
		return min(ans)
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	arr = {int(i) for i in input().split()}
	print(xor_k(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	f = 0
	for i in range(1, 1029):
		l1 = []
		for j in l:
			l1.append(i ^ j)
		l1.sort()
		if l == l1:
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	for i in range(1, 1025):
		t = set()
		for j in range(n):
			t.add(l[j] ^ i)
		if s == t:
			print(i)
			break
	else:
		print(-1)


import math
T = int(input())
for test in range(1, T + 1):
	input()
	arr = set(map(int, input().split()))
	if len(arr) % 2 == 1:
		print(-1)
	else:
		ok = False
		for i in range(1, 1025):
			n_arr = set([c ^ i for c in arr])
			if n_arr == arr:
				print(i)
				ok = True
				break
		if not ok:
			print(-1)


q = int(input())
for _ in range(q):
	n = int(input())
	s = [int(i) for i in input().split()]
	m = s[0]
	u = [False] * 1025
	for i in range(n):
		u[s[i]] = True
		m = max(m, s[i])
	o = False
	ans = 0
	for k in range(1, 1025):
		f = True
		for i in range(n):
			t = s[i] ^ k
			if t < 0 or t > m or (not u[t]):
				f = False
				break
		if f:
			ans = k
			o = True
			break
	if o:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = set()
	d = {}
	for i in l:
		d[i] = 1
	List = [l[0] ^ l[i] for i in range(1, n)]
	List.sort()
	boo1 = True
	for i in List:
		boo = True
		for j in range(n):
			if l[j] ^ i not in d:
				boo = False
				break
		if boo:
			print(i)
			boo1 = False
			break
	if boo1:
		print(-1)


for tc in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n % 2 == 1:
		print(-1)
	else:
		s = set(l)
		exors = []
		for i in range(1, n):
			exors.append(l[0] ^ l[i])

		def apply(k):
			ex = set()
			for j in l:
				ex.add(j ^ k)
			return ex
		ans = []
		for i in exors:
			if apply(i) == s:
				ans.append(i)
		if ans == []:
			print(-1)
		else:
			print(min(ans))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s1 = set(arr)
	flag = 1
	for i in range(1, 1025):
		s2 = set()
		for j in range(n):
			if i ^ arr[j] not in s2:
				s2.add(i ^ arr[j])
		if s1 == s2:
			print(i)
			flag = 0
			break
	if flag == 1:
		print(-1)
		continue


for iota in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	sett = set(lst)
	maximum = max(lst)
	two = 0
	ans = False
	for i in range(11):
		if maximum < 2 ** i:
			two = i
			break
	for i in range(1, 2 ** two):
		change = set([x ^ i for x in lst])
		if change == sett:
			ans = True
			print(i)
			break
	if ans == False:
		print(-1)


t = int(input())
for t_round in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	if n % 2 == 1:
		print(-1)
		continue
	k = 10 ** 5
	first = s[0]
	for i in range(1, n):
		set_s = set(s)
		k_attempt = first ^ s[i]
		set_s.remove(first)
		set_s.remove(s[i])
		for j in range(1, n):
			if not set_s:
				k = min(k, k_attempt)
				break
			if s[j] in set_s:
				if s[j] ^ k_attempt in set_s:
					set_s.remove(s[j])
					set_s.remove(s[j] ^ k_attempt)
				else:
					break
	if k == 10 ** 5:
		print(-1)
	else:
		print(k)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	sets = set(s)
	ans = -1
	k = 1
	ptr = 0
	for i in range(1024):
		ptr = 0
		for j in s:
			if k ^ j in sets:
				continue
			else:
				ptr = -1
				break
		if ptr == 0:
			ans = k
			break
		k += 1
	print(ans)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	Max = max(l)
	temp = set(l)
	for x in range(1, 1025):
		temp1 = set(map(lambda y: y ^ x, l))
		if temp == temp1:
			return x
	return -1
t = int(input())
for i in range(t):
	ans = solve()
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = sorted(l)
	flag = 0
	for i in range(1, 1024):
		s = l
		c = 0
		for j in s:
			x = j ^ i
			if x in s:
				c += 1
		if c == len(l):
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


t = int(input())
answer = []

def find(number):
	n = 1
	while n <= number:
		n *= 2
	return n
for test in range(t):
	n = int(input())
	liste = list(map(int, input().split(' ')))
	k = -1
	for i in range(1, find(max(liste))):
		temp_list = []
		for element in liste:
			temp_list.append(element ^ i)
		if set(temp_list) == set(liste):
			k = i
			break
	answer.append(str(k))
print('\n'.join(answer))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	if a[0] == 0:
		flag = 0
		for i in range(1, n):
			arr = []
			for j in range(0, n):
				arr.append(a[j] ^ a[i])
			arr.sort()
			if arr == a:
				flag = 1
				break
		if flag == 1:
			print(a[i])
		else:
			print(-1)
	else:
		ans = a[0]
		flag = 0
		for i in range(1, n):
			x = ans ^ a[i]
			arr = []
			for i in range(n):
				arr.append(a[i] ^ x)
			arr.sort()
			if arr == a:
				flag = 1
				break
		if flag == 1:
			print(x)
		else:
			print(-1)


import sys
input = sys.stdin.readline
mx = 10

def b(n):
	return '0' * (mx - len(bin(n)[2:])) + bin(n)[2:]
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		a = [j ^ i for j in s]
		if set(a) == s:
			ans = i
			break
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = set(map(int, input().split()))
	for k in range(1, 1025):
		s = set([i ^ k for i in a])
		if s == a:
			print(k)
			break
	else:
		print(-1)


for __ in range(int(input())):
	n = int(input())
	S = set([int(_) for _ in input().split()])
	ans = -1
	for k in range(1, 1025):
		T = set([s ^ k for s in S])
		if S == T:
			ans = k
			break
	print(ans)


t = int(input())
for i in range(t):
	printed = False
	num = int(input())
	num_set = input().split()
	num_set = [int(n) for n in num_set]
	for k in range(1, 1024):
		for j in range(num):
			if k ^ num_set[j] not in num_set:
				break
			if j == num - 1 and (not printed):
				print(k)
				printed = True
		if printed:
			break
		if k == 1023 and (not printed):
			print(-1)


t = int(input())
results = []
for i in range(t):
	n = int(input())
	vals = input().split()
	S = {int(val) for val in vals}
	for k in range(1, 1024):
		T = set()
		for s in S:
			x = s ^ k
			T.add(x)
		if T == S:
			results.append(k)
			break
	else:
		results.append(-1)
for result in results:
	print(result)


def number_of_operations(a, b):
	if a == b:
		return 0
	if a > b and a % b != 0 or (b > a and b % a != 0):
		return -1
	q = b // a if b > a else a // b
	shifts = 0
	while q // 8 > 0 and q % 8 == 0:
		shifts += 1
		q = q // 8
	while q // 4 > 0 and q % 4 == 0:
		shifts += 1
		q = q // 4
	while q // 2 > 0 and q % 2 == 0:
		shifts += 1
		q = q // 2
	if q != 1:
		return -1
	else:
		return shifts

def johnny_and_his_hobbies(s):
	for k in range(1, 1025):
		new_set = set((x ^ k for x in s))
		if new_set == set(s):
			return k
	return -1

def problem_a():
	t = int(input())
	for test_case in range(1, t + 1):
		(a, b) = map(int, input().split())
		print(number_of_operations(a, b))

def problem_b():
	t = int(input())
	for test_case in range(1, t + 1):
		n = int(input())
		s = list(map(int, input().split()))
		print(johnny_and_his_hobbies(s))
problem_b()


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	for x in range(1, 1024):
		counter = 0
		b = []
		for y in range(n):
			item = s[y] ^ x
			b.append(item)
		b.sort()
		for z in range(n):
			if b[z] == s[z]:
				counter += 1
		if counter == n:
			flag = x
			break
		else:
			flag = -1
	print(flag)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

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

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

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		r = -1
		s = set(a)
		for i in range(1, 1024):
			t = set([c ^ i for c in a])
			if s == t:
				r = i
				break
		rr.append(r)
	return JA(rr, '\n')
print(main())


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = sorted(array())
	flag = False
	xor = 1
	for i in range(1, 1025):
		xor = 1
		ans = set()
		for ele in a:
			ans.add(ele ^ i)
		if sorted(ans) == a:
			flag = True
			break
	if flag:
		print(i)
	else:
		print(-1)


def bintoint(x):
	n = len(x)
	f = 0
	for i in range(n):
		f += int(x[n - i - 1]) * 2 ** i
	return f
rw = int(input())
for ewqr in range(rw):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	t = False
	for i in range(1, 1025):
		ssub = set()
		for j in a:
			ssub.add(i ^ j)
		if ssub == s:
			t = True
			k = i
			break
	if t:
		print(k)
	else:
		print(-1)


def primeFactors(n):
	ans = 0
	while n % 2 == 0:
		n //= 2
		ans += 1
	return (ans, n)
import math
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	l = list(s)
	if n == 1:
		print(-1)
		continue
	ans = -1
	if max(l) != 0:
		maxV = 2 ** math.ceil(math.log(max(l), 2))
		for i in range(1, 1024):
			new = set([])
			for j in l:
				new.add(j ^ i)
			if new == s:
				ans = i
				break
	print(ans)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		n = int(input())
		l = [int(i) for i in input().split(' ')]
		flag = False
		for i in range(1, 1025):
			s = set(l)
			flag = False
			for k in l:
				if k ^ i in s:
					s.remove(k ^ i)
				else:
					flag = True
					break
			if not flag:
				print(i)
				break
		if flag:
			print(-1)
main()


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	f = 0
	for i in range(1, 1025):
		ans = set()
		for j in s:
			ans.add(i ^ j)
		if ans == s:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


t = int(input())
for test in range(t):
	n = int(input())
	nums = [int(x) for x in input().split()]
	dp = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			dp[i][j] = nums[i] ^ nums[j]
	x = set(nums)
	import sys
	t = []
	for i in range(n):
		t.append({})
		for j in range(n):
			t[i][dp[i][j]] = j
	ans = sys.maxsize
	tt = 0
	for i in range(1, n):
		s = set()
		s.add(nums[0] ^ dp[0][i])
		for j in range(1, n):
			if dp[0][i] not in t[j]:
				break
			if nums[j] ^ dp[j][t[j][dp[0][i]]] in s:
				break
			s.add(nums[j] ^ dp[j][t[j][dp[0][i]]])
		if s == x:
			ans = min(ans, dp[0][i])
			tt = 1
	if tt == 0:
		print(-1)
	else:
		print(ans)


import math
for t in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	aset = set(a)
	if n % 2 == 1:
		print(-1)
	elif n == 2 and a[1] != a[0]:
		print(a[1] ^ a[0])
	elif n == 2:
		print(-1)
	else:
		ds = set()
		rems = set()
		for i in range(n):
			ds.add(a[i] ^ a[0])
		ds.remove(0)
		if len(ds) == 0:
			print(-1)
			continue
		for i in sorted(list(ds)):
			naset = []
			done = False
			for j in a:
				naset.append(j ^ i)
			if sorted(naset) == a:
				done = i
				break
		if done:
			print(done)
		else:
			print(-1)


T = int(input())
for test_case in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	chk = [0] * 2049
	for i in A:
		chk[i] += 1
	R = -1
	for i in range(1, 1025):
		added = 0
		for j in range(n):
			xor_res = A[j] ^ i
			if chk[xor_res] == 0:
				added = 1
				break
		if added == 0:
			R = i
			break
	print(R)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	m = max(s)
	final = 0
	for i in range(1, 1025):
		se = s[:]
		for j in range(n):
			se[j] = se[j] ^ i
		if set(se) == set(s):
			final = i
			break
	if final == 0:
		print(-1)
	else:
		print(final)


import sys
input = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	l = set(map(int, input().split()))
	for i in range(1, 1025):
		s = set()
		for e in l:
			s.add(i ^ e)
		if s == l:
			print(i)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	req = set([int(i) for i in input().split()])
	ans = -1
	for val in range(1, 1025):
		temp = set([i ^ val for i in req])
		if temp == req:
			ans = val
			break
	print(ans)


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	for i in range(1, 1025):
		s = []
		for j in range(n):
			s.append(a[j] ^ i)
		s.sort()
		if s == a:
			return i
	return -1
t = int(input())
for fd in range(t):
	print(main())


from collections import Counter

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = list(map(int, input().split()))
		if N == 1:
			print(-1)
			continue
		d = Counter({})
		for i in range(N):
			xors = set()
			for j in range(N):
				if j != i:
					xors.add(A[i] ^ A[j])
			d.update(Counter(xors))
		ans = -1
		for key in d:
			if d[key] == N:
				if ans == -1:
					ans = key
				else:
					ans = min(ans, key)
		print(ans)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	c = 0
	for i in range(1, 1025):
		p = []
		for j in l:
			p.append(i ^ j)
		p.sort()
		if l == p:
			print(i)
			c = -1
			break
		else:
			continue
	if c == 0:
		print(-1)


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	st = set(arr)
	ok = False
	for i in range(1, 2050):
		cur = set()
		for x in arr:
			if x ^ i not in st:
				break
			cur.add(x ^ i)
		if cur == st:
			res = i
			ok = True
			break
	if ok:
		write(res)
	else:
		write(-1)

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(arr, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	y = 0
	for k in range(1, 1025):
		p = list(map(lambda x: k ^ x, l))
		p.sort()
		if l == p:
			y = k
			break
	if y > 0:
		print(y)
	else:
		print(-1)


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

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

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

def check(ans, ls):
	st1 = set(ls)
	st2 = set()
	for val in ls:
		st2.add(val ^ ans)
	if st1 != st2:
		return -1
	else:
		return 0

def solve():
	n = get_int()
	ls = get_list()
	ans = -1
	flag = 1
	for ans in range(1, 1024):
		x = check(ans, ls)
		if x == 0:
			flag = 0
			break
	if flag:
		print(-1)
	else:
		print(ans)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout
from collections import defaultdict

def solve(arr, n):
	pass

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		bits = []
		for i in range(0, 11):
			cnt = 0
			for num in arr:
				if num & 1 << i:
					cnt += 1
			if cnt == n - cnt:
				bits.append(i)
		ret = float('inf')
		for mask in range(1, 1 << len(bits)):
			num = 0
			for i in range(len(bits)):
				if mask & 1 << i:
					num |= 1 << bits[i]
			if set([a ^ num for a in arr]) == set(arr):
				ret = min(ret, num)
		if ret == float('inf'):
			print(-1)
		else:
			print(ret)
main()


t = int(input())
while t:
	t = t - 1
	f = 0
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = []
	for i in range(1, 1025):
		f = 0
		m = []
		for j in l:
			m.append(j ^ i)
		m.sort()
		if l == m:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	for i in arr:
		dic[i] = 1
	ans = -1
	for i in range(1, 1025):
		b = True
		for j in arr:
			x = i ^ j
			try:
				if dic[x]:
					continue
			except:
				b = False
				break
		if b:
			ans = i
			break
	print(ans)


def solve(s, n):
	ans = None
	for k in range(1, 1024):
		new_set = set()
		for val in s:
			new_set.add(k ^ val)
		if s == new_set:
			return k
	return -1

def read():
	t = int(input())
	for i in range(t):
		n = int(input())
		s = set(list(map(int, input().strip().split())))
		ans = solve(s, n)
		print(ans)
read()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	p = set(l)
	k = 1
	key = 0
	while k < 1025:
		s = []
		for i in l:
			if i ^ k in l:
				s.append(i ^ k)
			else:
				break
		s.sort()
		r = set(s)
		if r == p:
			key = 1
			break
		else:
			k += 1
	if key == 1:
		print(k)
	else:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	no = -1
	for i in range(1, 2049):
		fg = 0
		for j in range(0, n):
			b = s[j] ^ i
			if b not in s:
				fg = 1
				break
		if fg == 0:
			no = i
			break
	print(no)


import sys, os
from difflib import SequenceMatcher
from collections import Counter
from bisect import bisect_left as bl, bisect_right as br
from io import BytesIO, IOBase
from itertools import accumulate
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def memoize(f):

	class memodict(dict):

		def __getitem__(self, *key):
			return dict.__getitem__(self, key)

		def __missing__(self, key):
			ret = self[key] = f(*key)
			return ret
	return memodict().__getitem__
LCSubstr = lambda a, b: SequenceMatcher(None, a, b).find_longest_match(0, len(a), 0, len(b))

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

def is_prime(n):
	if n < 5 or n & 1 == 0 or n % 3 == 0:
		return 2 <= n <= 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			p = p * p % n
			if p == n - 1:
				break
		else:
			return False
	return True

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def extended_gcd(a, b):
	(s, old_s) = (0, 1)
	(r, old_r) = (b, a)
	while r:
		q = old_r // r
		(old_r, r) = (r, old_r - q * r)
		(old_s, s) = (s, old_s - q * s)
	return (old_r, old_s, (old_r - old_s * a) // b if b else 0)

def modinv(a, m):
	(g, x, _) = extended_gcd(a % m, m)
	return x % m if g == 1 else None

def make_nCr_mod(max_n=2 * 10 ** 5, mod=10 ** 9 + 7):
	max_n = min(max_n, mod - 1)
	(fact, inv_fact) = ([0] * (max_n + 1), [0] * (max_n + 1))
	fact[0] = 1
	for i in range(max_n):
		fact[i + 1] = fact[i] * (i + 1) % mod
	inv_fact[-1] = pow(fact[-1], mod - 2, mod)
	for i in reversed(range(max_n)):
		inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod

	def nCr_mod(n, r):
		res = 1
		while n or r:
			(a, b) = (n % mod, r % mod)
			if a < b:
				return 0
			res = res * fact[a] % mod * inv_fact[b] % mod * inv_fact[a - b] % mod
			n //= mod
			r //= mod
		return res
	return nCr_mod

def binarySearch(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
			r = mid - 1
	return -1

def PrefixSum(arr):
	return list(accumulate(arr))

class inputs:

	def single(self):
		return int(input())

	def mul(self):
		return map(int, input().split())

	def list(self):
		return list(map(int, input().split()))
inp = inputs()

def main():
	for t in range(inp.single()):
		n = inp.single()
		a = inp.list()
		ans = -1
		for i in range(1, 1025):
			temp = []
			for j in range(n):
				k = i ^ a[j]
				temp.append(k)
			if sorted(temp) == sorted(a):
				ans = i
				break
		print(ans)
main()


def ans(arr, n):
	k = 1
	arr.sort()
	z = arr[-1]
	while k <= 2 * z:
		q = []
		for i in arr:
			q.append(i ^ k)
		q.sort()
		if q == arr:
			return k
		k += 1
	return -1
m = int(input())
for i in range(m):
	n = int(input())
	a = input().split()
	arr = []
	for i in a:
		arr.append(int(i))
	print(ans(arr, n))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	s = set(a)
	rez = -1
	for i in range(1, 1024):
		c = 0
		for el in a:
			if i ^ el not in s:
				break
			else:
				c += 1
		if c == n:
			rez = i
			break
	print(rez)


import math, sys, bisect
from collections import defaultdict, Counter, deque

def input():
	return sys.stdin.readline().strip()
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])
for _ in range(int(input())):
	N = int(input())
	A = alele()
	mark = {i: 1 for i in A}
	f = 0
	i = -1
	for i in range(1, 1024 + 1):
		f = 0
		for j in A:
			if mark.get(j ^ i, -1) == -1:
				f = 1
				break
		if f == 0:
			break
	if f == 0:
		print(i)
	else:
		print(-1)


T = int(input())
for _ in range(T):
	x = int(input())
	a = list(map(int, input().split()))
	a.sort()
	for k in range(1, 1025):
		ar = []
		for i in a:
			ar.append(i ^ k)
		if a == sorted(ar):
			print(k)
			break
	else:
		print(-1)


import sys

def solve():
	return None

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = q[i].rstrip().split(' ')
		q[i] = [int(x) for x in q[i]]
	for i in range(1, len(q), 2):
		a = q[i + 1]
		m = max(a)
		a2 = []
		sol = -1
		m = len(bin(m)) - 2
		m = 2 ** m
		for i in range(1, m):
			a2 = []
			for j in a:
				temp = i ^ j
				if temp not in a:
					break
				elif temp in a2:
					break
				else:
					a2.append(temp)
				if j == a[len(a) - 1]:
					sol = i
			if sol != -1:
				break
		print(sol)
main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = set(arr)
	flag = 0
	ans = 0
	for i in range(1, 1025):
		b = set()
		if i != 0:
			for j in range(n):
				b.add(arr[j] ^ i)
			if a == b:
				flag = 1
				ans = i
				break
			if flag == 1:
				break
	if flag == 1:
		print(ans)
	else:
		print('-1')


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = -1
	for k in range(1, 1024):
		ok = True
		for i in range(n):
			b = s[i] ^ k
			if b not in s:
				ok = False
		if ok:
			ans = k
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	new = set()
	count = 0
	done = False
	curr = 0
	for i in range(1, max(s) * 2):
		for el in s:
			new.add(el ^ i)
		if len(s) == len(new):
			if s == new:
				print(i)
				done = True
				break
			else:
				new = set()
	if not done:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = set(a)
	p = 0
	for i in range(1, 1025):
		c = set()
		for j in a:
			c.add(i ^ j)
		if c == a:
			p += 1
			print(i)
			break
	if p == 0:
		print(-1)


from sys import stdin, stdout
q = int(input())
for _ in range(q):
	n = int(input())
	nums = set(map(int, stdin.readline().strip().split()))
	(iszero, maxnum) = (None, 0)
	for i in nums:
		if i == 0:
			iszero = True
		if i > maxnum:
			maxnum = i
	if iszero:
		asf = 10 ** 9
		for i in nums:
			if i != 0:
				possible = True
				for j in nums:
					if i ^ j not in nums:
						possible = False
						break
				if possible:
					if i < asf:
						asf = i
		print(asf if asf != 10 ** 9 else -1)
	else:
		asf = 10 ** 9
		for i in range(1, 2 * maxnum):
			possible = True
			for j in nums:
				if i ^ j not in nums:
					possible = False
					break
			if possible:
				if i < asf:
					asf = i
		print(asf if asf != 10 ** 9 else -1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		flag = True
		for j in l:
			if j ^ i not in l:
				flag = False
				break
		if flag:
			ans = i
			break
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	a.sort()
	for x in range(1, 1024):
		b = [0] * N
		for i in range(N):
			b[i] = a[i] ^ x
		b.sort()
		if a == b:
			print(x)
			break
	else:
		print(-1)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def main():

	def solve():
		n = int(inputi())
		ss = {int(a) for a in inputi().split()}
		res = set(range(1, 1024))
		for s in ss:
			for r in res.copy():
				if r ^ s not in ss:
					res.remove(r)
		if res:
			print(min(res))
		else:
			print(-1)
	q = int(inputi())
	for _ in range(q):
		solve()
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	ss = set(s)
	cond = [i for i in range(1, 2 ** 10)]
	flag = False
	for tar in cond:
		u = set()
		for i in range(n):
			u.add(tar ^ s[i])
		if ss == u:
			flag = True
			print(tar)
			break
	if not flag:
		print(-1)


t = int(input())
while t != 0:
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	s = set(arr)
	ans = 0
	temp = 0
	for i in range(1, 1025):
		flag = 0
		newSet = set()
		for item in s:
			newSet.add(item)
		for j in range(n):
			val = i ^ arr[j]
			if val not in newSet:
				flag = 1
				break
			else:
				newSet.remove(val)
		if flag == 0:
			temp = 1
			ans = i
			break
	if temp == 1:
		print(ans)
	else:
		print(-1)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	array = set(map(int, input().split()))
	flag = 0
	for i in range(1, 1025):
		array1 = set()
		for j in array:
			array1.add(i ^ j)
		if array == array1:
			flag = 1
			print(i)
			break
	if not flag:
		print(-1)


tests = int(input())
for i in range(tests):
	n = int(input())
	s = [int(x) for x in input().split(' ')]
	set_s = set(s)
	answer = -1
	for k in range(1, 1024):
		result = 1
		for x in range(n):
			if s[x] ^ k not in set_s:
				result = 0
				break
		if result == 1:
			answer = k
			break
	print(answer)


t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	ans = 0
	for i in range(1024):
		s = a.copy()
		ok = True
		while len(s) != 0:
			e = s.pop()
			if e ^ i in s:
				s.remove(e ^ i)
			else:
				ok = False
				break
		if len(s) == 0 and ok:
			ans = i
			break
	print(ans if ok else -1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	mx = max(l)
	d = set()
	for ele in l:
		d.add(ele)
	flag = False
	for i in range(1, 1025, 1):
		j = 0
		while j < n:
			xor = i ^ l[j]
			if xor in d:
				d.remove(xor)
			else:
				break
			j += 1
		if j == n:
			flag = True
			break
		for ele in l:
			d.add(ele)
	if flag == True:
		print(i)
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	an = int(input())
	l = set(map(int, input().split()))
	s = set()
	gg = 0
	raj = 1
	kr = raj
	s = 'RAJ'
	for j in range(1, 1026):
		s = set()
		for k in l:
			s.add(k ^ j)
		if s == l:
			print(j)
			gg = 1
			break
	if gg == 0:
		print(-1)


def ans(s):
	s = set(s)
	for i in range(1, 1024 + 1):
		new_s = set((x ^ i for x in s))
		if new_s == s:
			return i
	return -1
T = int(input())
for t in range(T):
	n = input()
	s = input().split(' ')
	s = list((int(x) for x in s))
	print(ans(s))


import sys
import math
import random

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

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

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

def main():

	def pro(x):
		w = []
		while x != 1:
			w.append(x % 2)
			x //= 2
		w.append(1)
		w.reverse()
		return w
	n = iinput()
	q = rlinput()
	s = set(q)
	for x in range(1, 1025):
		fl = 1
		for i in range(n):
			if q[i] ^ x not in s:
				fl = 0
				break
		if fl:
			print(x)
			return
	print(-1)
for inytd in range(iinput()):
	main()


for k in range(int(input())):
	a = int(input())
	t = list(map(int, input().split()))
	t.sort()
	h = 0
	for j in range(1, 1025):
		f = []
		for i in t:
			f.append(i ^ j)
		f.sort()
		if t == f:
			print(j)
			h += 1
			break
	if h == 0:
		print(-1)


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial, sqrt
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, accumulate
from bisect import *
from functools import reduce
from operator import mul
star = lambda x: print(' '.join(map(str, x)))
grid = lambda r: [lint() for i in range(r)]
INF = float('inf')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import sys
from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil

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

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

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

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

def check(a, n):
	res = []
	for i in range(len(a)):
		res.append(a[i] ^ n)
	a.sort()
	res.sort()
	return a == res

def solve():
	n = inp()
	a = inlt()
	res = -1
	for i in range(1, 1025):
		if check(a, i):
			res = i
			break
	print(res)
t = 1
t = inp()
for _ in range(t):
	solve()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	val = []
	p = arr[0]
	for i in arr[1:]:
		val.append(i ^ p)
	ans = float('inf')
	for i in val:
		temp = []
		for j in arr:
			temp.append(j ^ i)
		if set(temp) == set(arr) and i != 0:
			ans = min(ans, i)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	st = set(nums)
	f = True
	for x in range(1, 1025):
		for num in nums:
			if num ^ x in st:
				continue
			else:
				f = False
				break
		if f:
			print(x)
			f = 5
			break
		f = True
	if f != 5:
		print(-1)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	for i in range(1, 1025):
		flag = 1
		for el in a:
			if not el ^ i in a:
				flag = 0
				break
		if flag:
			print(i)
			break
	if not flag:
		print(-1)
	t -= 1


import math as m
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	e = 'a'
	for i in range(1, 1025):
		c = []
		for j in range(n):
			c.append(a[j] ^ i)
		d = set(c)
		if d == b:
			e = i
			break
	if e == 'a':
		print(-1)
	else:
		print(e)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = list(map(int, input().split()))
		values.sort()
		for k in range(1, 1024):
			new_values = []
			for j in values:
				new_values.append(k ^ j)
			new_values.sort()
			if new_values == values:
				print(k)
				break
		else:
			print(-1)
main()


import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline

def geta(fn=lambda s: s.decode()):
	return map(fn, readline().split())

def gete(fn=lambda s: s.decode()):
	return fn(readline().rstrip())

def main():
	for _ in range(gete(int)):
		n = gete(int)
		s = set(geta(int))
		ok = False
		for k in range(1, 1025):
			s1 = set()
			for si in s:
				s1.add(si ^ k)
			if s1 == s:
				print(k)
				ok = True
				break
		if not ok:
			print(-1)
main()


import sys

def rs():
	return sys.stdin.readline().rstrip()

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

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

def ws(s):
	sys.stdout.write(s + '\n')

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

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')

def solve(n, s):
	ss = set(s)
	for x in range(1, 1025):
		pp = set([si ^ x for si in s])
		if ss == pp:
			return x
	return -1

def main():
	for _ in range(ri()):
		n = ri()
		s = ria()
		print(solve(n, s))
main()


from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(-1)
	elif n == 2 and 0 in l:
		print(max(l))
	else:
		d = {}
		ans = -1
		for i in range(n):
			d[l[i]] = 1
		for i in range(1, 1024):
			d2 = {}
			count = 0
			for j in range(n):
				if d2.get(l[j] ^ i) == None and d.get(l[j] ^ i) != None:
					count += 1
			if count == n:
				ans = i
				break
		print(ans)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

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

def solve():
	n = ri()
	S = set(rl())
	for i in range(1, 1024):
		for s in S:
			if s ^ i not in S:
				break
		else:
			print(i)
			return
	print(-1)
	return
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = set()
	a = set(map(int, input().split(' ')))
	k = 0
	while k < 1024:
		z = False
		k += 1
		for i in a:
			e = i ^ k
			if e not in a:
				z = False
				break
			if e in a:
				z = True
		if k == 1024:
			print(-1)
		if z:
			print(k)
			k = 1024


import math
t = int(input())
for w in range(t):
	n = int(input())
	s = set([int(i) for i in input().split()])
	if max(s) == 0:
		print(-1)
	else:
		c = 0
		for i in range(1, 1025):
			s1 = set()
			for j in range(n):
				s1.add(list(s)[j] ^ i)
			if s1 == s:
				k1 = i
				c = 1
				break
		if c == 0:
			print(-1)
		else:
			print(k1)


cases = int(input())
for t in range(cases):
	n = int(input())
	d = {int(i): True for i in input().split()}
	f = 0
	for k in range(1, 1024):
		c = sum([d.get(j ^ k, False) for j in d])
		if n == c:
			print(k)
			f = 1
			break
	if f == 0:
		print(-1)


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	s = list(map(int, stdin.readline().split()))
	c = s.copy()
	c.sort()
	m = 0
	for k in range(1, 1024):
		a = []
		for i in range(n):
			a.append(s[i] ^ k)
		a.sort()
		if a == c:
			print(k)
			m = 1
			break
	if m == 0:
		print(-1)


s = [i for i in range(1, 1025)]
for _ in range(int(input())):
	n = int(input())
	a = set(list(map(int, input().split())))
	m = max(a)
	c = 0
	for i in range(1, 1025):
		b = set()
		for j in a:
			b.add(j ^ i)
		if b == a:
			c = i
			break
	if c != 0:
		print(c)
	else:
		print(-1)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		t = set()
		for e in s:
			t.add(e ^ i)
		if t == s:
			ans = i
			break
	print(ans)


def test(k, s):
	t = set(s)
	for a in s:
		if a ^ k not in s:
			return False
	return True

def main():
	n = int(input())
	s = list(map(int, input().split()))
	if n < 2 or n % 2:
		return -1
	v = set()
	for a in s:
		for b in s:
			if a != b:
				k = a ^ b
				v.add(k)
	for k in sorted(v):
		if test(k, s):
			return k
	return -1
t = int(input())
for ti in range(t):
	print(main())


t = int(input())
while t > 0:
	ans = -1
	n = int(input())
	s = list(map(int, input().split()))
	for i in range(1, 1024):
		flag = 0
		for j in s:
			if i ^ j in s:
				continue
			else:
				flag = 1
				break
		if flag == 0:
			ans = i
			break
	print(ans)
	t -= 1


T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	Dict = {}
	for i in arr:
		Dict[i] = 1
	flag = 0
	for i in range(1, 1025):
		Dict1 = {}
		for j in arr:
			A = j ^ i
			if A not in Dict:
				flag = 1
				break
			Dict1[A] = 1
		if len(Dict1) == len(Dict):
			print(i)
			flag = 0
			break
	if flag == 1:
		print(-1)


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr.sort()
	gflag = False
	for i in range(1, n):
		k = arr[0] ^ arr[i]
		flag = True
		for j in range(1, n):
			pop = bisect.bisect_left(arr, k ^ arr[j])
			if pop == n or arr[pop] != k ^ arr[j]:
				flag = False
				break
		if flag == True:
			print(k)
			gflag = True
			break
	if gflag == False:
		print(-1)


t = int(input())

def check(i):
	for p in lt:
		if p ^ i not in s:
			return False
	return True
while t != 0:
	t -= 1
	n = int(input())
	lt = [int(x) for x in input().split()]
	s = set(lt)
	ans = -1
	for i in range(1, 1025):
		if check(i) == True:
			ans = i
			break
	print(ans)


for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	hm = [0] * 2000
	for i in l:
		hm[i] += 1
	if n % 2 != 0:
		print(-1)
	else:
		xr = []
		for i in range(1, n):
			xr.append(l[0] ^ l[i])
		a = []
		for i in xr:
			f = True
			for j in range(1, n):
				if hm[i ^ l[j]] != 1:
					f = False
					break
			if f:
				a.append(i)
		if len(a) == 0:
			print(-1)
		else:
			print(min(a))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2
from math import *
for _ in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	li = dict()
	for i in range(n):
		for j in range(n):
			if i != j:
				li.setdefault(num[i] ^ num[j], 0)
				li[num[i] ^ num[j]] += 1
	(flag, val) = (0, 10000000000.0)
	for (k, v) in li.items():
		if v == n:
			flag = 1
			val = min(val, k)
	if flag == 1:
		print(val)
	else:
		print(-1)


tests = int(input())
for _ in range(tests):
	n = int(input())
	d = set(list(map(int, input().split())))
	flag = False
	for k in range(1, 1025):
		new = set()
		for i in d:
			new.add(i ^ k)
		if new == d:
			print(k)
			flag = True
			break
	if not flag:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	inp = [int(j) for j in input().split()]
	numb = set(inp)
	flag = 0
	for k in range(1, 1024):
		temp = []
		for num in inp:
			temp.append(num ^ k)
		if numb == set(temp):
			flag = 1
			print(k)
			break
	if flag == 0:
		print(-1)


t = int(input())
for ab in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	kx = set()
	for al in a:
		kx.add(al)
	ts = True
	for px in range(1, 1025):
		ux = set()
		pq = True
		for tl in a:
			if tl ^ px in kx and tl ^ px not in ux:
				ux.add(tl ^ px)
			else:
				pq = False
				break
		if ux == kx and pq:
			print(px)
			ts = False
			break
		ux.clear()
	if ts:
		print(-1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		l2 = []
		for j in l:
			l2.append(i ^ j)
		if set(l) == set(l2):
			ans = i
			break
	print(ans)


exec('\ndef solve(n,s):\n    for i in range(1,1025):\n        s1=set(int(i^x) for x in s)\n        if s1==s:\n            return i\n    return -1\nfor _ in range(int(input())):\n    n=int(input());s=set(int(x) for x in input().split());print(solve(n,s))\n')


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	vals = [0 for i in range(n)]
	temp = []
	ans = -1
	nums = sorted(nums)
	for i in range(1, 1025):
		for f in range(n):
			temp.append(nums[f] ^ i)
		if sorted(temp) == nums:
			ans = i
			break
		temp = []
	print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	s = [int(__) for __ in input().split()]
	chk = [0] * 1024
	for a in s:
		chk[a] += 1
	stop = False
	for i in range(1, 1024):
		yes = True
		for a in s:
			if chk[a ^ i] == 0:
				yes = False
		if yes:
			print(i)
			stop = True
			break
	if stop:
		continue
	print(-1)


from sys import stdin
input = stdin.readline

def r(a, b):
	res = ''
	for j in range(len(a)):
		res += str(int(a[j] != b[j]))
	return res

def towork(a):
	a = str(bin(a))[2:]
	return '0' * (10 - len(a)) + a
for _ in range(int(input())):
	n = int(input())
	mx = -1
	s = {}
	for i in map(int, input().split()):
		s[i] = 1
		mx = max(mx, i)
	rj = len(bin(mx)) - 2
	for num in range(1, 1024):
		num = towork(num)
		for i in s:
			if not s.get(int(r(num, towork(i)), 2)):
				break
		else:
			print(int(num, 2))
			break
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	s1 = set()
	lst = list(s)
	for i in range(n):
		for j in range(i + 1, n):
			s1.add(lst[i] ^ lst[j])
	s1 = sorted(list(s1))
	happy = 0
	for i in list(s1):
		for j in lst:
			if i ^ j in s:
				continue
			else:
				break
		else:
			happy = 1
			print(i)
			break
	if happy == 0:
		print(-1)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	check = [0] * 1025
	f1 = 0
	for i in arr:
		check[i] = 1
	for i in range(1, 1024):
		f = 1
		for j in arr:
			c = i ^ j
			if c <= 1024 and (not check[c]):
				f = 0
				break
		if f:
			print(i)
			f1 = 1
			break
	if not f1:
		print(-1)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(-1)
		return
	x = set(a)
	for i in range(1, 2 * max(a) + 1):
		b = [j ^ i for j in a]
		b = set(b)
		if len(b) == n and x == b:
			print(i)
			return
	print(-1)
t = int(input())
for i in range(t):
	solve()


from sys import stdin
from math import log2, ceil
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	aSet = set(a)
	if max(a) == 0:
		print(-1)
		continue
	maximum = ceil(log2(max(a)))
	flag = False
	s = set()
	x = a[0]
	if n > 1:
		for i in a[1:]:
			x ^= i
	if x == 0:
		x += 1
	for i in range(x, 2 ** (maximum + 1) + 1):
		for j in a:
			s.add(i ^ j)
		if s == aSet:
			flag = True
			print(i)
			break
		s = set()
	if not flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = -1
	for i in range(1, 1024):
		a = []
		for j in arr:
			a.append(i ^ j)
		if set(arr) == set(a):
			flag = i
			break
	print(flag)


from math import log2, ceil
for _ in range(int(input())):
	input()
	inp = set([int(i) for i in input().split()])
	lim = 2 ** ceil(log2(max(inp) + 1))
	flag = True
	for i in range(1, lim):
		test = inp.copy()
		new = set([])
		while len(test) > 0:
			x = test.pop()
			new.add(x ^ i)
		if len(inp) == len(new) and inp == new:
			print(i)
			flag = False
			break
	if flag:
		print(-1)


import math
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		a = True
		for j in range(n):
			if l[j] ^ i not in l:
				a = False
				break
		if a:
			ans = i
			break
	print(ans)


from sys import stdin
from math import log2, ceil
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	flag = False
	s = set()
	for i in range(1, 1024):
		for j in a:
			s.add(i ^ j)
		if s == set(a):
			flag = True
			print(i)
			break
		s = set()
	if not flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		lis = []
		for j in range(n):
			lis.append(i ^ s[j])
		if set(lis) == set(s):
			ans = i
			break
	print(ans)


for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	ans = -1
	if b[0] == 0:
		for i in range(1, n):
			temp = []
			for j in range(n):
				temp.append(a[j] ^ b[i])
			if set(temp) == set(a):
				ans = b[i]
				break
		print(ans)
	else:
		m = max(b)
		y = 0
		while 1:
			if 2 ** y > m:
				break
			y += 1
		for i in range(1, 2 ** y + 1):
			temp = []
			for j in range(n):
				temp.append(a[j] ^ i)
			if set(temp) == set(a):
				ans = i
				break
		print(ans)


for _ in range(int(input())):
	n = int(input())
	S = set(map(int, input().split()))
	if n % 2:
		print(-1)
		continue
	for k in range(1, max(S) << 1):
		for s in S:
			if s ^ k not in S:
				break
		else:
			print(k)
			break
	else:
		print(-1)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in range(base ** n):
		s = [tb // base ** bt % base for bt in range(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class Tree:

	def __init__(self, inp_size=None, ls=None, init=True, index=1):
		self.LCA_init_stat = False
		self.ETtable = []
		if init:
			if ls == None:
				self.stdin(inp_size, index=index)
			else:
				self.size = len(ls) + 1
				(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def stdin(self, inp_size=None, index=1):
		if inp_size == None:
			self.size = int(input())
		else:
			self.size = inp_size
		(self.edges, _) = GI(self.size, self.size - 1, index=index)
		return

	def listin(self, ls, index=0):
		self.size = len(ls) + 1
		(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def dfs(self, x, func=lambda pr, prv, nx, dist: prv + dist, root_v=0):
		q = deque([x])
		v = [None] * self.size
		v[x] = root_v
		while q:
			c = q.pop()
			for (nb, d) in self.edges[c]:
				if v[nb] == None:
					q.append(nb)
					v[nb] = func(c, v[c], nb, d)
		return v

	def bfs(self, x, func=lambda pr, prv, nx, dist: prv + dist, root_v=0):
		q = deque([x])
		v = [None] * self.size
		v[x] = root_v
		while q:
			c = q.popleft()
			for (nb, d) in self.edges[c]:
				if v[nb] == None:
					q.append(nb)
					v[nb] = func(c, v[c], nb, d)
		return v

	def parent(self, x):
		return self.dfs(0, func=lambda pr, prv, nx, dist: pr, root_v=-1)

	def topological_sort(self, x):
		tps = []
		q = deque([x])
		v = [None] * self.size
		v[x] = 0
		while q:
			c = q.popleft()
			tps.append(c)
			for (nb, d) in self.edges[c]:
				if v[nb] == None:
					q.append(nb)
					v[nb] = 0
		return tps

	def EulerTour(self, x):
		q = deque()
		q.append(x)
		self.depth = [None] * self.size
		self.depth[x] = 0
		self.ETtable = []
		self.ETdepth = []
		self.ETin = [-1] * self.size
		self.ETout = [-1] * self.size
		cnt = 0
		while q:
			c = q.pop()
			if c < 0:
				ce = ~c
			else:
				ce = c
				for (nb, d) in self.edges[ce]:
					if self.depth[nb] == None:
						q.append(~ce)
						q.append(nb)
						self.depth[nb] = self.depth[ce] + 1
			self.ETtable.append(ce)
			self.ETdepth.append(self.depth[ce])
			if self.ETin[ce] == -1:
				self.ETin[ce] = cnt
			else:
				self.ETout[ce] = cnt
			cnt += 1
		return

	def LCA_init(self, root):
		self.EulerTour(root)
		self.st = SparseTable(self.ETdepth, init_func=min, init_idl=inf)
		self.LCA_init_stat = True
		return

	def LCA(self, root, x, y):
		if self.LCA_init_stat == False:
			self.LCA_init(root)
		(xin, xout) = (self.ETin[x], self.ETout[x])
		(yin, yout) = (self.ETin[y], self.ETout[y])
		a = min(xin, yin)
		b = max(xout, yout, xin, yin)
		id_of_min_dep_in_et = self.st.query_id(a, b + 1)
		return self.ETtable[id_of_min_dep_in_et]

	def __str__(self):
		return str(self.edges)

	def show(self):
		if all([all([d == 1 for (nd, d) in edge]) for edge in self.edges]):
			print([[nd for (nd, d) in edge] for edge in self.edges])
		else:
			print(self)

class SparseTable:

	def __init__(self, ls, init_func=min, init_idl=float('inf')):
		self.func = init_func
		self.idl = init_idl
		self.size = len(ls)
		self.N0 = self.size.bit_length()
		self.table = [ls[:]]
		self.index = [list(range(self.size))]
		self.lg = [0] * (self.size + 1)
		for i in range(2, self.size + 1):
			self.lg[i] = self.lg[i >> 1] + 1
		for i in range(self.N0):
			tmp = [self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) for j in range(self.size)]
			tmp_id = [self.index[i][j] if self.table[i][j] == self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) else self.index[i][min(j + (1 << i), self.size - 1)] for j in range(self.size)]
			self.table += [tmp]
			self.index += [tmp_id]

	def query(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		return self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))])

	def query_id(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		(a, b) = (self.index[N][l], self.index[N][max(0, r - (1 << N))])
		if self.table[0][a] == self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))]):
			b = a
		return b

	def right_bound(self, pos, x):
		k = (self.size - pos).bit_length()
		for j in range(k)[::-1]:
			nx = pos + (1 << j)
			if nx < n and self.query(pos, nx + 1) <= x:
				pos += 1 << j
		return pos

	def left_bound(self, pos, x):
		k = pos.bit_length()
		for j in range(k)[::-1]:
			nx = pos - (1 << j)
			if 0 <= nx and self.query(nx, pos + 1) <= x:
				pos -= 1 << j
		return pos

	def __str__(self):
		return str(self.table[0])

	def print(self):
		for i in self.table:
			print(*i)
show_flg = False
show_flg = True
ans = 0
for _ in range(int(input())):
	ans = 0
	n = I()
	s = LI()
	f = True
	N = 1 << 10
	for k in range(1, N):
		t = [k ^ j for j in s]
		if set(t) == set(s):
			print(k)
			f = False
			break
	if f:
		print(-1)


def check(k):
	for i in range(len(arr)):
		if not is_av[arr[i] ^ k]:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	is_av = [False] * 1024
	for i in arr:
		is_av[i] = True
	flag = 0
	for i in range(1, 1024):
		if check(i):
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	ans = -1
	for k in range(1, 1025):
		ss = set([k ^ i for i in s])
		if s == ss:
			ans = k
			break
	print(ans)


for i in range(int(input())):
	am = int(input())
	arr = list(map(int, input().split()))
	if am & 1:
		print(-1)
		continue
	iter = 1
	arr.sort()
	m = arr[am - 1]
	while True:
		nArr = [0] * am
		for i in range(am):
			nArr[i] = arr[i] ^ iter
		if sorted(nArr) == arr:
			print(iter)
			break
		if iter < 1024:
			iter += 1
		else:
			print(-1)
			break


for t in range(int(input())):
	N = int(input())
	A = set([int(i) for i in input().split()])
	for k in range(1, 1024 + 1):
		s = set([i ^ k for i in A])
		if A == s:
			print(k)
			break
	else:
		print(-1)


T = int(input())
for t in range(T):
	N = int(input())
	s = set((int(i) for i in input().split()))
	res = -1
	for k in range(1, 1024):
		new_set = set((k ^ i for i in s))
		if new_set == s:
			res = k
			break
	print(res)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	mx = max(s)
	cou = 0
	while mx >= 1:
		cou += 1
		mx /= 2
	m = cou
	isp = 0
	for k in range(1, 2 ** m):
		ss = []
		for j in range(n):
			ss.append(s[j] ^ k)
		if set(s) == set(ss):
			print(k)
			isp = 1
			break
	if isp == 0:
		print(-1)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		d = dict(l)
		ans = -1
		for i in range(1, 1025):
			count = 0
			for x in l:
				temp = x ^ i
				if d.get(temp, -1) == -1:
					break
				else:
					count += 1
			if count == n:
				ans = i
				break
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for i in range(int(input())):
	a = int(input())
	a = 1
	b = set(map(int, input().split()))
	c = set()
	while b - c != set() and a <= 1024:
		c.clear()
		for _ in b:
			c.add(a ^ _)
		a += 1
	if a == 1025:
		print(-1)
	else:
		print(a - 1)


import sys

def read():
	return sys.stdin.readline().strip()

def write(a):
	sys.stdout.write(a)

def check(a, k):
	s = {i ^ k for i in a}
	return s == a

def solve(a, n):
	for k in range(1, 1024 + 1):
		if check(a, k):
			return k
	return -1

def bruh():
	t = int(read())
	ans = [0] * t
	for i in range(t):
		n = int(read())
		a = {int(i) for i in read().split()}
		ans[i] = solve(a, n)
	write('\n'.join(map(str, ans)))
bruh()


for _ in range(int(input())):
	n = int(input())
	a = set([int(i) for i in input().split()])
	flag = 0
	for i in range(1, 2 * max(a) + 1):
		st = set()
		for j in a:
			st.add(j ^ i)
		if st == a:
			flag = 1
			break
	if flag == 1:
		print(i)
	else:
		print(-1)


t = int(input())
for l in range(t):
	x = int(input())
	r = list(map(int, input().split()))
	r.sort()
	flag = 0
	for i in range(1, 1024):
		flag = 1
		for j in r:
			target = i ^ j
			flag2 = 1
			start = 0
			end = len(r) - 1
			while start <= end:
				mid = (start + end) // 2
				if r[mid] == target:
					flag2 = 1
					break
				elif r[mid] < target:
					start = mid + 1
					flag2 = 0
				else:
					end = mid - 1
					flag2 = 0
			if flag2 == 0:
				flag = 0
				break
			else:
				continue
		if flag == 1:
			print(i)
			break
	if flag == 0:
		print(-1)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		arry.sort()
		if n % 2 == 0:
			ansfound = False
			for i in range(1, n):
				diu = arry[0] ^ arry[i]
				cr = True
				for i in range(n):
					fuk = arry[i] ^ diu
					if fuk in arry:
						continue
					else:
						cr = False
						break
				if not cr:
					continue
				else:
					ansfound = True
				if ansfound:
					break
			if ansfound:
				yield diu
			else:
				yield (-1)
		else:
			yield (-1)
t = int(input())
ans = gift()
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n % 2 == 1:
		print(-1)
	else:
		b = [0] * 1025
		for i in a:
			for j in a:
				if i != j:
					b[i ^ j] += 1
		m = 0
		for i in range(1, 1025):
			if b[i] == n:
				m = i
				break
		if m == 0:
			print(-1)
		else:
			print(m)


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

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

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

def invr():
	return map(int, input().split())
n = inp()
for i in range(n):
	z = inp()
	l = inlt()
	check = [0] * 1024
	for x in l:
		check[x] = 1
	for k in range(1, 1024):
		f = 1
		for x in l:
			if check[x ^ k] == 0:
				f = 0
				break
		if f:
			print(k)
			break
	if k == 1023 and f == 0:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = 1
	flag = True
	while x < 1025:
		curr = []
		for i in range(n):
			curr.append(arr[i] ^ x)
		if set(arr) == set(curr):
			print(x)
			flag = False
			break
		x += 1
	if flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = map(int, input().split())
	s = set(s)
	ans = -1
	for i in range(1, 1025):
		tmp = set()
		for x in s:
			tmp.add(x ^ i)
		if tmp == s:
			ans = i
			break
	print(ans)
	print()


t = int(input())
while t > 0:
	n = int(input())
	mainLis = []
	a = list(map(int, input().split()))
	for i in range(n):
		lis = []
		for j in range(n):
			if i != j:
				lis.append(a[i] ^ a[j])
		mainLis.append(lis)
	result = set(mainLis[0])
	for currSet in mainLis[1:]:
		result.intersection_update(currSet)
	res = list(result)
	res = sorted(res)
	if len(res) == 0:
		print(-1)
	else:
		print(res[0])
	t -= 1


from functools import reduce
for _ in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	s.sort()
	for j in range(1, 1025):
		if j == 1024:
			print(-1)
			break
		t = [j ^ x for x in s]
		t.sort()
		if all([t[i] == s[i] for i in range(n)]):
			print(j)
			break


a = int(input())
for i in range(a):
	b = int(input())
	l = list(map(int, input().split()))
	l = sorted(l)
	s = l
	boo = True
	for j in range(1, max(l) * 2):
		c = l[:]
		for h in range(len(c)):
			c[h] = c[h] ^ j
		if sorted(c) == s:
			boo = False
			print(j)
			break
	if boo:
		print(-1)


import math
for t in range(int(input())):
	a = int(input())
	arr = list(map(int, input().split()))
	finarr = list(set(arr))
	finarr.sort()
	found = 0
	for i in range(1, 1024):
		arr2 = []
		for j in range(a):
			arr2 += [arr[j] ^ i]
		xxx = list(set(arr2))
		xxx.sort()
		if xxx == finarr:
			print(i)
			found = 1
			break
	if found == 0:
		print(-1)


def pap(n, x):
	a = {}
	for i in x:
		a[i] = 1
	for i in range(1, 1025):
		b = {}
		for j in x:
			t = i ^ j
			if t not in a:
				break
			else:
				if t in b:
					break
				b[t] = 1
		if len(a) == len(b):
			print(i)
			return
	print(-1)
t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split(' ')))
	pap(n, x)


def find(lst):
	a = set()
	for i in range(1, 1024):
		if set(lst) == set(map(lambda x: x ^ i, lst)):
			return i
	return -1
I = lambda : list(map(int, input().split(' ')))
for _ in range(I()[0]):
	n = I()[0]
	lst = I()
	print(find(lst))


t = int(input())
for s in range(t):
	digits = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	for i in range(1, 1026):
		check = []
		for j in lst:
			k = i ^ j
			check.append(k)
		check.sort()
		if lst == check:
			break
	if i == 1025:
		print(-1)
	else:
		print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	nums.sort()
	res = -1
	for x in range(1, 1025):
		bitwise = []
		for y in nums:
			bitwise.append(x ^ y)
		bitwise.sort()
		if bitwise == nums:
			res = x
			break
	print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
t = int(input())
for j in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	set0 = set(nums)
	maxpow = math.ceil(math.log2(max(nums) + 1))
	out = -1
	for s in range(1, 2 ** maxpow + 1):
		newset = set([k ^ s for k in nums])
		if newset == set0:
			val = True
			out = s
			break
	print(out)


import sys
import math
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin

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

def check(k, n):
	for i in range(n):
		if not is_N[A[i] ^ k]:
			return False
	return True
t = int(input())
N = 1025
for ti in range(1, t + 1):
	n = int(input())
	A = int_split_input()
	is_N = [False] * N
	for x in A:
		is_N[x] = True
	for k in range(1, 1024):
		if check(k, n):
			print(k)
			break
	else:
		print(-1)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
from io import BytesIO
import threading
import bisect
import heapq
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')
ara = [0] * 100005
listOdd = list()

def aratosfen():
	global ara
	for i in range(2, int(math.sqrt(len(ara)))):
		if ara[i] == 0:
			for j in range(i * 2, len(ara), i):
				ara[j] = 1
	global listOdd
	for i in range(2, len(ara)):
		if ara[i] == 0:
			listOdd.append(i)

def run(str1, met, omet, f, l, s):
	fl = False
	cOp = 0
	for i in range(f, l, s):
		if str1[i] == met:
			fl = True
		elif fl == True and str1[i] == omet:
			cOp += 1
	return cOp
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	vis = [0] * 1025
	for i in range(n):
		vis[arr[i]] = 1
	if n == 1:
		print(-1)
		continue
	if n == 2:
		print(arr[0] ^ arr[1])
		continue
	arr2 = []
	for i in range(1, n):
		arr2.append(arr[0] ^ arr[i])
	arr2.sort()
	isAnswer = False
	for i in range(len(arr2)):
		x = arr2[i]
		cn = 0
		gArr = [0] * 1025
		for j in range(n):
			if vis[arr[j] ^ x] == 1 and gArr[arr[j] ^ x] == 0:
				gArr[arr[j] ^ x] = 1
				cn += 1
			else:
				break
		if cn == n:
			print(x)
			isAnswer = True
			break
	if isAnswer == False:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	if n % 2:
		print(-1)
	else:
		xor = {}
		hashmap = [0] * 1025
		for i in range(n):
			for j in range(i + 1, n):
				x = ar[i] ^ ar[j]
				if xor.get(x) == None:
					xor[x] = [[i, j]]
				else:
					xor[x].append([i, j])
		for key in sorted(xor.keys()):
			value = xor[key]
			if len(value) >= n // 2:
				print(key)
				break
		else:
			print(-1)


from copy import deepcopy
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = {}
	for i in a:
		if i not in x:
			x[i] = 0
		x[i] += 1
	flag1 = 0
	val = -1
	for i in range(1, 1025):
		flag = 1
		x1 = deepcopy(x)
		for j in range(0, len(a)):
			temp = a[j] ^ i
			if temp not in x1 or x1[temp] == 0:
				flag = 0
				break
			x1[temp] -= 1
		if flag == 1:
			flag1 = 1
			val = i
			break
	if flag1 == 1:
		print(val)
	else:
		print(-1)


cases = int(input())
for _ in range(cases):
	n = int(input())
	c = sorted(list(map(int, input().split())))
	m = []
	for i in range(1, 1024):
		w = []
		for j in range(len(c)):
			w.append(c[j] ^ i)
		if sorted(w) == c:
			m.append(i)
	try:
		print(min(m))
	except:
		print(-1)


for i in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	l.sort()
	for j in range(1, 1025):
		m = []
		for k in range(0, len(l)):
			m.append(l[k] ^ j)
		m.sort()
		if m == l:
			f = 1
			b = j
			break
	if f == 1:
		print(b)
	else:
		print('-1')


t = 1

def solve(a):
	ans = float('inf')
	for i in range(1, 1024):
		l = []
		for j in a:
			l.append(i ^ j)
		if set(l) == set(a):
			ans = i
			break
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	solve(a)


__multitest__ = True

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	D = {}
	for i in range(n):
		D[a[i]] = i
	for k in range(1, 1025):
		flag = True
		vis = [0] * n
		for i in range(n):
			if D.get(a[i] ^ k) == None:
				flag = False
				break
			elif vis[D[a[i] ^ k]] == 1:
				flag = False
				break
			else:
				vis[D[a[i] ^ k]] = 1
		if flag:
			print(k)
			return
	print(-1)
	return
t = 1 if not __multitest__ else int(input())
for tt in range(t):
	solve()


import math

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

def lcm(a, b):
	return a * b / gcd(a, b)

def bs(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return arr[mid]
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def swap(list, pos1, pos2):
	(list[pos1], list[pos2]) = (list[pos2], list[pos1])
	return list
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	y = set(s)
	f = 0
	for k in range(1, 1025):
		z = set()
		for i in s:
			z.add(i ^ k)
		if z == y:
			f = 1
			print(k)
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	ar.sort()
	ans = -1
	for i in range(1, 1025):
		br = []
		for j in ar:
			br.append(i ^ j)
		ok = True
		br.sort()
		for j in range(n):
			if ar[j] != br[j]:
				ok = False
				break
		if ok:
			ans = i
			break
	print(ans)


from collections import Counter
import sys
inp = [*open(0)]
inpi = 0

def I():
	global inpi
	ret = inp[inpi]
	inpi += 1
	return ret
for tc in range(1, 1 + int(I())):
	n = int(I())
	s = list(map(int, I().split()))
	c = Counter(s)
	s = set(s)
	for i in range(1, 1025):
		if all((c[j] == c[j ^ i] for j in s)):
			print(i)
			break
	else:
		print(-1)


t = int(input())
for i in range(t):
	jk = int(input())
	arr = list(map(int, input().split()))
	d = [x for x in range(1, 1026)]
	f = int(0)
	res = 0
	for x in d:
		for ele in arr:
			if x ^ ele not in arr:
				f = 0
				break
			else:
				f += 1
				if f == len(arr):
					res = x
					break
				continue
		if f == len(arr):
			break
	if res == 0:
		print('-1')
	else:
		print(res)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = set(map(int, input().split()))
	ans = 0
	f = 0
	for i in range(1, 1024):
		flag = 0
		new_s = set()
		for x in s:
			new_s.add(x)
		for j in s:
			xor = i ^ j
			if xor in new_s:
				new_s.remove(xor)
			else:
				flag = 1
				break
		if flag == 0:
			f = 1
			ans = i
			break
	if f == 1:
		print(ans)
	else:
		print('-1')


try:
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		arr.sort()
		flg = 0
		ans = -1
		for i in range(1, 1024):
			tem = []
			for j in range(n):
				tem.append(arr[j] ^ i)
			tem.sort()
			for k in range(n):
				if arr[k] != tem[k]:
					break
			else:
				ans = i
				break
		print(ans)
except:
	pass


def solve(test):
	n = int(input())
	arr = set(map(int, input().split()))
	k_max = 1025
	ans = -1
	for k in range(1, k_max + 1):
		trial_set = set()
		for ele in arr:
			trial_set.add(ele ^ k)
		if trial_set == arr:
			ans = k
			break
	print(ans)

def main():
	test_case = int(input())
	for test in range(test_case):
		solve(test + 1)
main()


import copy
for _ in range(int(input())):
	input()
	S = set(map(int, input().split()))
	printed = False
	for i in range(1, 1024):
		S2 = set()
		for j in S:
			S2.add(j ^ i)
		if S2 == S:
			print(i)
			printed = True
			break
	if not printed:
		print(-1)


def xor():
	result = []
	n = int(input())
	lst = list(map(int, input().split()))
	if n == 1:
		return -1
	for i in range(n):
		temp = []
		for j in range(n):
			if i != j:
				temp.append(lst[i] ^ lst[j])
		result.append(set(temp))
	tmp = result[0]
	for i in range(1, n):
		tmp = tmp & result[i]
		if not tmp:
			return -1
	return min(tmp)

def main():
	t = int(input())
	for i in range(t):
		print(xor())
main()


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	s = list(s)
	k = []
	i = 1
	for i in range(1, 1025):
		g = []
		for j in s:
			o = j ^ i
			g.append(o)
		if set(g) == set(s):
			print(i)
			break
		elif i == 1024:
			print(-1)
			break


inp = input
for _ in range(int(inp())):
	n = int(inp())
	arr = list(map(int, inp().strip().split(' ')))
	pos = set()
	for i in range(n):
		for j in range(i + 1, n):
			num = arr[i] ^ arr[j]
			pos.add(num)
	pos = list(pos)
	pos.sort()
	flag = False
	ans = -1
	for p in pos:
		vis = {p: False for p in arr}
		for a in arr:
			temp = a ^ p
			vis[temp] = True
		if all(vis.values()):
			flag = True
			ans = p
			break
	if flag:
		print(ans)
	else:
		print(-1)


def solution(n, A):
	for k in range(1, 1025):
		S = set([a ^ k for a in A])
		if A == S:
			return k
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	A = set(map(int, input().split(' ')))
	print(solution(n, A))


def solve():
	n = input()
	nums = list(map(int, input().split()))
	numSet = set(nums)

	def check(nums, k):
		seen = set()
		for n in nums:
			if n ^ k not in numSet:
				return False
		return True
	for i in range(1, 1024):
		if check(nums, i):
			print(i)
			break
	else:
		print(-1)
for _ in range(int(input())):
	solve()


def solve():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	a.sort()
	for i in range(0, n):
		if i == 0:
			continue
		k = a[0] ^ a[i]
		thisset = []
		for j in a:
			thisset.append(j ^ k)
		if set(thisset) == set(a):
			print(k)
			return
	print(-1)
t = int(input())
while t > 0:
	solve()
	t -= 1


import sys
import math
input = sys.stdin.readline

def check_xor_set(s, k):
	for item in s:
		new_item = item ^ k
		if new_item not in s:
			return False
	return True
T = int(input())
for t in range(T):
	N = int(input())
	A = set(map(int, input().split(' ')))
	solved = False
	for k in range(1, 1024):
		if check_xor_set(A, k):
			print(k)
			solved = True
			break
	if not solved:
		print(-1)


from sys import stdin, stdout
for testcases in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	res = 0
	answer = -1
	arr.sort()
	for i in range(1, 1025):
		arr1 = []
		for j in arr:
			arr1.append(j ^ i)
		arr1.sort()
		if arr1 == arr:
			answer = i
			break
	print(answer)


x = int(input())
for xxx in range(x):
	input()
	ppp = sorted(list(map(int, input().split())))
	flag = 1
	for ans in range(1, 1024):
		pp = []
		for j in ppp:
			pp.append(j ^ ans)
		if ppp == sorted(pp):
			print(ans)
			flag = 0
			break
	if flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = 0
	for i in range(1, 1025):
		m = [j ^ i for j in l]
		l.sort()
		m.sort()
		if m == l:
			p = 1
			print(i)
			break
	if p == 0:
		print(-1)


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
from collections import Counter
t = read_int()
for case_num in range(t):
	n = read_int()
	s = list(read_ints())
	cs = Counter(s)
	found = False
	for i in range(1, 1024):
		a = [si ^ i for si in s]
		ca = Counter(a)
		if ca == cs:
			print(i)
			found = True
			break
	if not found:
		print(-1)


def findXOR(a):
	n = len(a)
	candidates = [0 for i in range(n - 1)]
	for i in range(n - 1):
		candidates[i] = a[i] ^ a[n - 1]
	S = set(a)
	valid_candidates = [1 for i in range(n - 1)]
	for i in range(n - 1):
		for j in range(n - 1):
			if valid_candidates[j] == 0:
				continue
			if a[i] ^ candidates[j] not in S:
				valid_candidates[j] = 0
	ans = []
	for i in range(n - 1):
		if valid_candidates[i] == 1:
			ans.append(candidates[i])
	if len(ans) == 0:
		return -1
	else:
		return min(ans)
n = int(input())
for i in range(n):
	nn = input()
	a = [int(x) for x in input().split()]
	print(findXOR(a))


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, A):
	from collections import Counter
	from itertools import combinations
	bc = Counter()
	for a in A:
		for (i, b) in enumerate(reversed(bin(a)[2:])):
			if b == '1':
				bc[i] += 1
	ks = []
	for (k, v) in bc.items():
		if v == N // 2:
			ks.append(k)
	r = set(A)
	ans = INF
	for n in range(1, len(ks) + 1):
		for i in combinations(ks, r=n):
			K = 0
			for j in i:
				K += 1 << j
			t = set()
			for a in A:
				t.add(a ^ K)
			if t == r:
				ans = min(ans, K)
	if ans == INF:
		return -1
	return ans

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = set(arr)
	ans = -1
	for i in range(1, 1024):
		b = set()
		for j in range(n):
			b.add(arr[j] ^ i)
		if b == a:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input().split()
	s = [int(i) for i in s]
	m = set(s)
	ibroke = 0
	for i in range(1, 1025):
		a = []
		for j in s:
			a.append(i ^ j)
		if set(a) == m:
			print(i)
			ibroke = 1
			break
	if ibroke == 0:
		print(-1)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = set(map(int, stdin.readline().split()))
	fl1 = 0
	for j in range(1, 1025):
		co = 0
		visi = set()
		fl = 1
		for x in arr:
			if x ^ j in arr and x ^ j not in visi:
				co += 1
				visi.add(x ^ j)
			else:
				fl = 0
				break
		if fl:
			fl1 = 1
			print(j)
			break
	if not fl1:
		print(-1)


t = int(input())
while t != 0:
	n = int(input())
	list1 = list(map(int, input().split()))
	list1.sort()
	f1 = 0
	index = -1
	for i in range(1, 1025):
		temp = list()
		for j in range(len(list1)):
			temp.append(list1[j] ^ i)
		temp.sort()
		f1 = 0
		for j in range(len(temp)):
			if temp[j] != list1[j]:
				f1 = 1
				break
		if f1 == 0:
			index = i
			break
	print(index)
	t -= 1


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

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

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

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

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

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
ans = []
for _ in range(ii()):
	n = ii()
	s = set(li())
	for i in range(1, 1025):
		b = set()
		for item in s:
			b.add(item ^ i)
		if b == s:
			ans.append(i)
			break
	else:
		ans.append(-1)
print(*ans, '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = 1
	d = {}
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	while x <= 1024:
		e = d.copy()
		for j in l:
			try:
				e[j ^ x] -= 1
				if e[j ^ x] == 0:
					del e[j ^ x]
			except:
				break
		if e:
			x += 1
		else:
			print(x)
			break
	if x == 1025:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	m = max(a)
	ans = 0
	for i in range(1, 1025):
		flag = 0
		for j in a:
			k = j ^ i
			if k in a:
				flag += 1
		if flag == n:
			ans = i
			break
	print(ans if ans else -1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	ans = -1
	for i in range(1, 1024):
		s2 = set()
		for num in s:
			s2.add(num ^ i)
		same = True
		for num in s2:
			if not num in s:
				same = False
		if same:
			ans = i
			break
	print(ans)


def check(a, n):
	b = set(a)
	for i in range(1, 1025):
		j = 0
		while j < n:
			x = a[j] ^ i
			if x not in b:
				break
			j += 1
		else:
			return i
	return -1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(check(a, n))


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	a = []
	c = []
	s.sort()
	for k in range(1, 1024):
		for i in s:
			b = i ^ k
			a.append(b)
		if s == sorted(a):
			c.append(k)
		a.clear()
	if len(c) == 0:
		c.append(-1)
	print(min(c))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	for j in range(n):
		s.add(a[j])
	a.sort()
	ans = -1
	for j in range(1, 1024):
		breakflag = 0
		for k in range(n):
			if not a[k] ^ j in s:
				breakflag = 1
				break
		if breakflag == 0:
			ans = j
			break
	print(ans)


from sys import stdin, stdout

def is_power_of_two(n):
	return n != 0 and n & n - 1 == 0
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = set(map(int, stdin.readline().split()))
	if n % 2 == 1:
		print(-1)
		continue
	ans = -1
	for k in range(1, 1024):
		if all((x ^ k in s for x in s)):
			ans = k
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(1, 2000):
		b = [0] * n
		for j in range(n):
			b[j] = a[j] ^ i
		if sorted(a) == sorted(b):
			ans = i
			break
	print(ans) if ans > 0 else print(-1)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	k = 1
	log = 0
	while k < 1025:
		m = []
		for x in l:
			m.append(x ^ k)
		m.sort()
		if l == m:
			log = 1
			break
		k += 1
	if log == 1:
		print(k)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	for k in range(1, 1025):
		if k == 1024:
			print(-1)
			break
		sc = set([e ^ k for e in s])
		if s == sc:
			print(k)
			break


t = int(input())

def fun(n, a):
	b = set(a)
	ans = -1
	s = set()
	for i in range(1, 1025):
		for j in a:
			s.add(j ^ i)
		if s == b:
			ans = i
			break
		s.clear()
	print(ans)
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	fun(n, a)


for _ in range(int(input())):
	input()
	l = list(map(int, input().split()))
	t = [i ^ l[0] for i in l][1:]
	t.sort()
	sol = -1
	for x in t:
		if set(l) == set([i ^ x for i in l]):
			sol = x
			break
	print(sol)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, 1024):
		k = []
		for j in a:
			if j ^ i not in a:
				break
			else:
				k.append(j ^ i)
		else:
			if set(k) == set(a):
				print(i)
				break
	else:
		print(-1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().strip().split()))
	flag = 0
	q = max(li)
	for i in range(1, 1025):
		res = set([k ^ i for k in set(li)])
		if res == set(li):
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


def solve():
	n = int(input())
	l = [int(x) for x in input().split()]
	flag = False
	if len(l) == 1:
		print(-1)
	else:
		for k in range(1, 1024):
			for x in l:
				if x ^ k not in l:
					flag = False
					break
				else:
					flag = True
			if flag == True:
				print(k)
				break
		if flag == False:
			print(-1)
t = int(input())
while t > 0:
	t -= 1
	solve()


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
t = iinput()
for _ in range(t):
	n = iinput()
	a = rlinput()
	if n % 2:
		print(-1)
		continue
	temp = [0] * 100000
	for i in range(n):
		for j in range(i + 1, n):
			temp[a[i] ^ a[j]] += 1
	ans = -1
	for i in range(1, 100000):
		if temp[i] == n // 2:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	x = set(x)
	f = 0
	for i in range(1, 1025):
		d = set()
		for j in x:
			d.add(j ^ i)
		if d == x:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	test = True
	l = list(map(int, input().split()))
	l.sort()
	mx = max(l)
	for j in range(1, 4097):
		ll = []
		for c in l:
			ll.append(c ^ j)
		ll.sort()
		if ll == l:
			print(j)
			test = False
			break
	if test:
		print(-1)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ele = set(arr)
	ans = -1
	for x in range(1, 1025):
		temp = [item ^ x for item in arr]
		if set(temp) == ele:
			ans = x
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = set(map(int, input().split()))
	k = 1
	m = max(a)
	t = 0
	while k < 1024:
		b = []
		for i in a:
			b.append(i ^ k)
		c = set(b)
		if c == a:
			print(k)
			t = 1
			break
		k = k + 1
	if t == 0:
		print(-1)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	has = [False] * 1030
	for i in range(n):
		has[a[i]] = True
	ans = -1
	for i in range(1, n):
		val = a[0] ^ a[i]
		ok = True
		for j in range(n):
			if not has[a[j] ^ val]:
				ok = False
				break
		if ok:
			ans = val if ans == -1 else min(ans, val)
	print(ans)
for _ in range(int(input())):
	solve()


for i in range(int(input())):
	k = 0
	n = int(input())
	a = [int(x) for x in input().split()]
	s = set(a)
	for i in range(1, 1024):
		d = set()
		for j in s:
			c = j ^ i
			d.add(c)
		if d - s == set():
			print(i)
			k = 1
			break
	if k == 0:
		print(-1)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	mini = sys.maxsize
	i = 0
	for j in range(1, n):
		temp = s[i] ^ s[j]
		check = 0
		if temp < mini:
			k = 1
			while k < n:
				if k != j:
					if temp ^ s[k] not in s:
						check = 1
						break
				k += 1
			if check != 1:
				mini = temp
	if mini != sys.maxsize:
		print(mini)
	else:
		print(-1)


t = int(input(''))
for _ in range(t):
	n = int(input(''))
	a = list(map(int, input().split()))
	a.sort()
	flag = 0
	for i in range(1, 1024):
		b = []
		for j in range(n):
			b.append(a[j] ^ i)
		b.sort()
		if a == b:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


from sys import stdin, stdout, stderr, maxsize
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
for _ in range(I()):
	n = I()
	ls = set(map(int, stdin.readline().split()))
	f = 1
	for i in range(1, 1025):
		cnt = 0
		s = set()
		for j in ls:
			s.add(i ^ j)
		if len(ls.intersection(s)) == n:
			print(i)
			f = 0
			break
	if f:
		print(-1)


a = int(input())
for _ in range(a):
	x = int(input())
	li = list(map(int, input().split()))
	b = max(li)
	p = -1
	for i in range(1, 1025):
		for j in li:
			if j ^ i not in li:
				break
		else:
			p = i
			break
	print(p)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	k = 1
	b = 0
	while k < 2500:
		l2 = [0] * n
		for i in range(n):
			l2[i] = l[i] ^ k
		l2.sort()
		if l == l2:
			b = 1
			print(k)
			break
		k += 1
	if b == 0:
		print(-1)


import sys
input = sys.stdin.readline
test = int(input())
while test != 0:
	test -= 1
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		d[i] = 0
	ans = -1
	for i in range(1, 1025):
		fl = True
		for j in range(0, n):
			x = l[j] ^ i
			if x in d and d[x] == 0:
				continue
			else:
				fl = False
				break
		if fl:
			ans = i
			break
	print(ans)


import sys
import string
input = sys.stdin.readline
import math
from operator import xor
t = int(input())
for i in range(2 * t):
	if i % 2 == 0:
		z = int(input())
	else:
		m = list(map(int, input().split()))
		dicto = {}
		for el in m:
			dicto[el] = 1
		ok = 0
		for x in range(1, 1024):
			ss = set()
			for j in range(len(m)):
				if m[j] ^ x not in dicto:
					break
				else:
					ss.add(m[j])
			if len(ss) == len(m):
				print(x)
				ok = 1
				break
		if ok == 0:
			print(-1)


from collections import Counter, defaultdict
BS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def to_base(s, b):
	res = ''
	while s:
		res += BS[s % b]
		s //= b
	return res[::-1] or '0'
alpha = 'abcdefghijklmnopqrstuvwxyz'
from math import floor, ceil
t = int(input())
for i in range(t):
	ans = False
	n = int(input())
	s = set(map(int, input().split()))
	for k in range(1, 1024):
		ns = set((i ^ k for i in s))
		if ns & s == s:
			print(k)
			ans = True
			break
	if not ans:
		print(-1)


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(a) for a in input().split()]
	s = set(a)
	b = bin(max(a))
	p = 0
	for i in range(1, 2 ** len(b)):
		l = set()
		for j in range(n):
			k = a[j] ^ i
			l.add(k)
		if l == s:
			c = i
			p = 1
			break
	if p == 0:
		print(-1)
	else:
		print(c)


for _ in range(int(input())):
	n = int(input())
	arr = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		a = arr.copy()
		for el in arr:
			if i ^ el in a:
				a.remove(i ^ el)
		if len(a) == 0:
			ans = i
			break
	print(ans)


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d1 = {}
	for i in l:
		try:
			d1[i] += 1
		except:
			d1[i] = 1
	maxi = max(l)
	ans = 0
	for i in range(1, 1025):
		temp = []
		for j in l:
			temp.append(j ^ i)
		d2 = {}
		for j in temp:
			try:
				d2[j] += 1
			except:
				d2[j] = 1
		if d1 == d2:
			ans = i
			break
	if ans == 0:
		print(-1)
	else:
		print(ans)


import math as mt
import collections as cc
import sys
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	a = set(l)
	f = 0
	for i in range(1, 1025):
		temp = set()
		for j in l:
			temp.add(j ^ i)
		if a == temp:
			print(i)
			f = 1
			break
	if not f:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = -1
	num = 0
	for i in a:
		num |= 1 << i
	for x in range(1, 1025):
		numt = 0
		for i in a:
			numt |= 1 << (i ^ x)
		if numt == num:
			ans = x
			break
	print(ans)


import math
t = int(input())
cnt = 0
for cnt in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	cnt = 0
	ans = 0
	for i in range(1, 1024):
		for j in s:
			x = i ^ j
			if x not in s:
				break
		else:
			cnt = 1
			ans = i
			break
	if cnt == 0:
		print('-1')
	else:
		print(ans)


import sys
from math import log2, floor, ceil, sqrt
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	s = set(a)
	xor = 0
	ans = -1
	for i in range(1, 1025):
		t = set()
		for j in s:
			t.add(j ^ i)
		if t == s:
			ans = i
			break
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = -1
	s = 0
	for i in range(1, 1025):
		p = True
		for j in range(n):
			if a[j] ^ i not in a:
				p = False
				break
		if p == True:
			res = i
			break
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	ok = -1
	sum = 0
	for i in range(1, 1024):
		new = set()
		for j in a:
			new.add(j ^ i)
		if new == a:
			ok = i
			break
	if ok == -1:
		print(-1)
	else:
		print(ok)


def check(i):
	global a
	a = set(a)
	b = set()
	for x in a:
		b.add(x ^ i)
	if b == a:
		return 1
	return 0
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, 1025):
		if check(i):
			print(i)
			break
	else:
		print('-1')


t = int(input())
for x in range(t):
	n = int(input())
	L = list(map(int, input().strip().split()))
	flag = 0
	for i in range(1, 1024):
		a = []
		for j in L:
			a.append(j ^ i)
		L.sort()
		a.sort()
		if a == L:
			flag = 1
			break
	if flag == 0:
		print(-1)
	else:
		print(i)


ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
for _ in range(ii()):
	n = ii()
	a = il()
	c = 0
	for i in range(1, 1025):
		b = []
		if c >= 1:
			break
		for j in range(0, n):
			b.append(i ^ a[j])
		if set(b) == set(a):
			print(i)
			c = c + 1
			break
	if c == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	k = 1
	while k < 1024:
		l = []
		y = [x ^ k for x in x]
		y.sort()
		x.sort()
		if y == x:
			print(k)
			break
		k += 1
	else:
		print('-1')


t = int(input())
for u in range(t):
	n = int(input())
	x = set([int(_) for _ in input().split()])
	ez = False
	for k in range(1, 1025):
		x1 = set([___ ^ k for ___ in x])
		if x1 == x:
			print(k)
			ez = True
			break
	if not ez:
		print(-1)


def main(num, lst):
	inter = set()
	ind = 0
	for i in range(num):
		intersect = set()
		for j in range(num):
			intersect.add(lst[i] ^ lst[j])
		if ind == 0:
			inter = intersect
			ind += 1
			continue
		inter = inter.intersection(intersect)
	inter.remove(0)
	if inter == set():
		return -1
	return min(inter)
for _ in range(int(input())):
	num = int(input())
	lst = list(map(int, input().split()))
	print(main(num, lst))


for _ in range(int(input())):
	n = int(input())
	set1 = set([int(i) for i in input().split()])
	a = -1
	for i in range(1, 1024):
		if set1 == set([i ^ j for j in set1]):
			a = i
			break
	print(a)


import queue
import math
t = int(input())
while t:
	n = int(input())
	a = input().split()
	a = set([int(x) for x in a])
	ans = -1
	for k in range(1, 1024):
		b = set()
		for x in a:
			b.add(k ^ x)
		if len(b) == len(a):
			flag = True
			for x in b:
				if x not in a:
					flag = False
					break
			if flag:
				ans = k
				break
	print(ans)
	t -= 1


import math
testCases = int(input())
inputs = []
results = []
for i in range(testCases):
	setNumber = int(input())
	line = input().split()
	line = [int(num) for num in line]
	line.sort()
	inputs.append(line)

def findResult(line):
	s = set(line)
	for i in range(1, 1025):
		res = True
		for t in s:
			if not t ^ i in s:
				res = False
				break
		if res:
			return i
	return -1
for line in inputs:
	print(findResult(line))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	f = False
	for k in range(1, 1025):
		m = [i ^ k for i in arr]
		m.sort()
		if m == arr:
			print(k)
			f = True
			break
	if f is False:
		print(-1)


t = int(input())
for q in range(t):
	flag = 0
	n = int(input())
	arr = list(map(int, input().split()))
	hash = [0] * 1025
	flag = 0
	for i in range(n):
		hash[arr[i]] = 1
	for k in range(1, 1025):
		for i in range(n):
			if arr[i] ^ k > 1024 or hash[arr[i] ^ k] == 0:
				flag = 1
				break
			else:
				flag = 2
		if flag == 2:
			break
	if flag == 2:
		print(k)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	size = int(input())
	li = list(map(int, input().split()))
	flag = False
	for i in range(1, max(li) * 2):
		s = []
		for j in li:
			s.append(j ^ i)
		if set(s) == set(li):
			flag = True
			print(i)
			break
	if not flag:
		print(-1)


def ans(nums):
	min_ = 1
	max_ = max(nums)
	max2 = 1
	while max2 <= max_:
		max2 *= 2
	for v in range(min_, max2):
		nums2 = set()
		for number in nums:
			xor_val = v ^ number
			if xor_val not in nums:
				break
			nums2.add(xor_val)
		if nums2 == nums:
			return v
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	nums = set([int(x) for x in input().split()])
	print(ans(nums))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	sa = set()
	for i in a:
		sa.add(i)
	ans = 9999999999999999999
	for i in range(1, n):
		next = a[0] ^ a[i]
		if next <= 0:
			continue
		pos = True
		for j in range(n):
			if a[j] ^ next not in sa:
				pos = False
				break
		if pos:
			ans = min(ans, next)
	print(ans if ans != 9999999999999999999 else -1)


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	d = dd(int)
	for i in l:
		d[i] = 1
	for i in range(1, 1025):
		lol = 0
		for j in l:
			if d[i ^ j] == 0:
				lol = 1
				break
		if lol == 0:
			print(i)
			break
	if lol:
		print(-1)
	t -= 1


for _ in range(int(input())):
	len = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	ans = -1
	for j in range(1, 1024):
		s1 = set()
		for k in range(len):
			s1.add(arr[k] ^ j)
		if s == s1:
			ans = j
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	m = max(arr)
	l = len(bin(m)[2:])
	num = int('1' * l, 2)
	ans = -1
	for i in range(1, num + 1):
		tem = set()
		for j in arr:
			tem.add(j ^ i)
		if tem == s:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s1 = set(l)
	s2 = set()
	f = 0
	for i in range(1, 2 * (max(l) + 1)):
		s2.clear()
		for j in l:
			s2.add(j ^ i)
		if s1 == s2:
			f = 1
			break
	if f == 1:
		print(i)
	else:
		print(-1)


def xor(a, b):
	a = ('0000000000' + bin(a)[2:])[-10:]
	b = ('0000000000' + bin(b)[2:])[-10:]
	res = ''
	for i in range(10):
		if not a[i] and (not b[i]):
			continue
		if a[i] == b[i]:
			res += '0'
		else:
			res += '1'
	return int(res, 2)
t = int(input())
while t:
	t -= 1
	n = input()
	arr = [int(x) for x in input().split()]
	arr.sort()
	for k in range(1, 1024):
		cool = True
		new_arr = []
		for num in arr:
			new_num = xor(num, k)
			if not new_num in arr:
				cool = False
				break
			else:
				new_arr.append(new_num)
		if cool:
			new_arr.sort()
			if new_arr == arr:
				print(k)
				break
	if not cool:
		print(-1)


from collections import defaultdict as dd

def lcm(a, b):
	return a * b // gcd(a, b)

def inin():
	return int(input())

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

def ar(element, size):
	return [element for i in range(size)]

def digitsum(num):
	su = 0
	while num:
		su += num % 10
		num //= 10
	return su

def yes():
	print('YES')

def no():
	print('NO')
Testcase = inin()
for _ in range(Testcase):
	n = inin()
	arr = inar()
	arr.sort()
	for k in range(1, 1025):
		arr2 = [i ^ k for i in arr]
		arr2.sort()
		if arr == arr2:
			print(k)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ss = set(a)
	flag = 0
	for i in range(1, 1024):
		s = set()
		for j in range(n):
			s.add(a[j] ^ i)
		if s == ss:
			flag = i
			break
	if flag != 0:
		print(flag)
	else:
		print(-1)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	s = set(map(int, input().split()))
	p = -1
	for k in range(1, 1025):
		q = set()
		i = 0
		for x in s:
			q.add(x ^ k)
		if s == q:
			p = k
			break
	print(p)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	n = int(fastinput())
	b = list(map(int, fastinput().split()))
	if n % 2 != 0:
		print(-1)
	else:
		s = set(b)
		ans = 0
		b.sort()
		x = b[0]
		for i in b[1:]:
			y = i ^ x
			temp = set([x, i])
			for j in b:
				if j == b[0] or i == j:
					continue
				c = j ^ y
				if not c in s:
					break
				else:
					temp.add(c)
			if temp == s:
				ans = y
				break
		if ans:
			print(ans)
		else:
			print(-1)


t = int(input())
while t:
	n = int(input())
	l = []
	l = list(map(int, input().split()))
	f = 0
	i = 1
	while i < 1024:
		f = 0
		for j in range(n):
			if l[j] ^ i in l:
				f = 0
			else:
				f = 1
				break
		if f == 0:
			break
		i += 1
	if f == 0:
		print(i)
	else:
		print(-1)
	t -= 1


t = int(input())

def equal(a, b):
	for i in range(0, len(a)):
		if a[i] != b[i]:
			return False
	return True
while t > 0:
	n = int(input())
	_list1 = list(map(int, input().split()))
	_list1.sort()
	ans = -1
	for i in range(1, 2000):
		_list2 = []
		for j in _list1:
			_list2.append(i ^ j)
		_list2.sort()
		if equal(_list1, _list2):
			ans = i
			break
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	idx = [-1] * 1025
	found = False
	for (i, a) in enumerate(arr):
		idx[a] = i
	for i in range(1, 1024 + 1):
		present = [False] * n
		for a in arr:
			if a ^ i <= 1024 and idx[a ^ i] != -1:
				present[idx[a ^ i]] = True
		if all(present):
			print(i)
			found = True
			break
	if not found:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	c = set()
	check = True
	for i in range(1, 1024):
		c = set()
		for j in s:
			c.add(j ^ i)
		if s == c:
			print(i)
			check = False
			break
	if check:
		print(-1)


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cc = 0
	for i in range(1, 1025):
		c = 0
		for j in a:
			x = j ^ i
			if x not in a:
				c += 1
				break
		if c == 0:
			cc += 1
			print(i)
			break
	if cc == 0:
		print(-1)


t = int(input())
n = []
s = []
for _ in range(t):
	n.append(int(input()))
	s.append(list(map(int, input().split())))
for i in range(t):
	N = n[i]
	S = set(s[i])
	ans = -1
	for j in range(1, 1025):
		s1 = set()
		for k in s[i]:
			s1.add(k ^ j)
		if s1 == S:
			ans = j
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	flag = 1
	arr = list(map(int, input().split()))
	arr.sort()
	for k in range(1, 1025):
		sets = set(arr)
		for i in range(n):
			if arr[i] in sets:
				if arr[i] ^ k in sets:
					sets.remove(arr[i])
					sets.remove(arr[i] ^ k)
		if len(sets) == 0:
			print(k)
			flag = 0
			break
	if flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	tmp = list(map(int, input().split()))
	lis = list(set(tmp))
	ans = -1
	flag = 0
	for i in range(1, 1026):
		tmp2 = []
		for j in lis:
			tmp2.append(i ^ j)
		if set(tmp2) == set(lis):
			ans = i
			flag = 1
			break
	if flag == 0:
		print(-1)
	else:
		print(ans)


def f1(x, y):
	newSet = set()
	for i in x:
		newSet.add(i ^ y)
	return newSet
for _ in range(int(input().strip())):
	n = int(input().strip())
	S = set(map(int, input().strip().split()))
	ans = -1
	for i in range(1, 1025):
		if S == f1(S, i):
			ans = i
			break
	print(str(ans))


t = int(input())
for ii in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	for i in range(1, 2048):
		s1 = set()
		for si in s:
			a = si ^ i
			if a in s1 or a not in s:
				break
			else:
				s1.add(a)
		else:
			print(i)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	a = sorted(a)
	flag = 0
	for i in range(1, 1025):
		c = []
		for j in range(len(a)):
			c.append(a[j] ^ i)
		c = sorted(c)
		if c == a:
			print(i)
			flag += 1
			break
	if flag == 0:
		print(-1)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dict = {}
	for i in range(n):
		dict[arr[i]] = 1
	cond = -1
	for k in range(1, 1024):
		ans = 1
		for i in range(n):
			x = arr[i] ^ k
			if x not in dict:
				ans = 0
				break
		if ans == 1:
			cond = k
			break
	print(cond)


import math
I = input
for _ in range(int(I())):
	n = int(I())
	a = list(map(int, I().split()))
	a1 = dict.fromkeys(a, 0)
	m = max(a)
	t = int(math.log(m, 2)) if m != 0 else 0
	d = 2 ** (t + 1)
	ans = -1
	for i in range(1, d):
		t = 0
		l = []
		for j in a:
			if j ^ i in a1:
				l.append(j ^ i)
			else:
				t = 1
				break
		if t == 0 and len(set(l)) == n:
			ans = i
			break
	print(ans)


from sys import *

def solve(a, m):
	for i in range(1, 1025):
		b = set()
		for j in a:
			b.add(j ^ i)
		if len(a - b) == 0:
			return i
	return -1
test = 1
test = int(input())
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	m = max(arr)
	arr = set(arr)
	ans = solve(arr, m)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	f = 0
	ee = min(a) + max(a)
	for j in range(1, ee + 1):
		ww = set()
		for p in a:
			ww.add(p ^ j)
		if b == ww:
			print(j)
			f = 1
			break
	if f == 0:
		print(-1)


import sys
input = sys.stdin.readline
from math import ceil, floor, factorial

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = sorted(int_array())
	for i in range(n):
		mset = set()
		for j in range(n):
			if a[i] == a[j]:
				continue
			mset.add(a[i] ^ a[j])
		if i == 0:
			ans = mset.copy()
		else:
			ans = ans.intersection(mset)
	try:
		print(min(ans))
	except:
		print(-1)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	a = L()
	d = Counter(a)
	for i in range(1, 1025):
		b = []
		for j in range(n):
			b.append(a[j] ^ i)
		if d == Counter(b):
			print(i)
			return
	print(-1)
for i in range(I()):
	solve()


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	flag = 0
	for i in range(1, 1025):
		if i == 1024:
			flag = 1
		d = set()
		for j in s:
			if j ^ i not in s:
				break
			else:
				d.add(j ^ i)
		if s == d:
			break
	print(-1 if flag else i)


import sys, collections, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = set(map(int, sys.stdin.readline().split()))
	for i in range(1, 1025):
		temp = set()
		for j in arr:
			temp.add(j ^ i)
		can = False
		if temp == arr:
			can = True
			print(i)
			break
	if not can:
		print(-1)


T = int(input())
for _ in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	s = set(s)
	k = -1
	for i in range(1, 1024):
		s1 = [f ^ i for f in s]
		s1 = set(s1)
		if s1 == s:
			k = i
		if k != -1:
			break
	print(k)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	for i in range(1, 1025):
		r = set()
		for si in s:
			r.add(si ^ i)
		if s == r:
			print(i)
			break
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ya = 0
	for i in range(1, 1025):
		b = []
		for j in range(n):
			b.append(a[j] ^ i)
		b.sort()
		if b == a:
			ya = i
			break
	if ya > 0:
		print(ya)
	else:
		print(-1)


def dfs(a, vis, good_p, ndx, cur):
	if ndx >= len(good_p):
		for x in a:
			if not vis[x ^ cur]:
				return -1
		return cur
	ans0 = dfs(a, vis, good_p, ndx + 1, cur)
	if ans0 > 0:
		return ans0
	ans1 = dfs(a, vis, good_p, ndx + 1, cur | good_p[ndx])
	if ans1 > 0:
		return ans1
	return -1

def solve(a):
	vis = [False for x in range(1025)]
	for x in a:
		vis[x] = True
	p2 = 1024
	good_p = []
	while p2 > 0:
		cnt0 = 0
		cnt1 = 0
		for x in a:
			if x & p2:
				cnt1 += 1
			else:
				cnt0 += 1
		if cnt0 == cnt1:
			good_p.append(p2)
		p2 //= 2
	if not good_p:
		return -1
	return dfs(a, vis, good_p, 0, 0)
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(solve(a))


t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	maxx = max(a)
	ok = False
	for i in range(1, maxx * 2 + 5):
		tmp = set()
		for j in a:
			tmp.add(i ^ j)
		if tmp == a:
			print(i)
			ok = True
			break
	if not ok:
		print(-1)


for ad in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = set(l)
	t = 0
	for i in range(1, 1026):
		s = []
		for j in l:
			s.append(i ^ j)
		s = set(s)
		if s == m:
			t = 1
			break
	if t == 1:
		print(i)
	else:
		print(-1)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		a.sort()
		x = -1
		for k in range(1, 1024):
			if a == sorted([i ^ k for i in a]):
				x = k
				break
		print(x)
main()


from sys import stdin
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
hg = lambda x, y: (y + x - 1) // x * x
for _ in range(I()):
	n = I()
	a = list(R())
	ans = -1
	for i in range(1, 1025):
		if i ^ a[0] in a:
			e = a.copy()
			while e and e[0] ^ i in e:
				e.remove(e[0] ^ i)
				e.remove(e[0])
			if not e:
				ans = i
				break
	print(ans)


def main():
	from io import BytesIO
	from os import read, fstat
	input = BytesIO(read(0, fstat(0).st_size)).readline
	from sys import stdout
	print = stdout.write
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = list(map(int, input().split()))
		good_set = set(s)
		flag = False
		good_number = 0
		for number in range(1, 1025):
			optionality_set = []
			for i in range(n):
				optionality_set.append(s[i] ^ number)
			if set(optionality_set) == good_set:
				flag = True
				good_number = number
				break
		if flag:
			print('{0}\n'.format(good_number))
		else:
			print('-1\n')
main()


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
from random import *
from itertools import permutations
input = stdin.readline
prin = stdout.write
from random import sample
from collections import Counter, deque
from fractions import *
from math import sqrt, ceil, log2, gcd, cos, pi, floor
from copy import deepcopy
mod = 10 ** 9 + 7
mod2 = 998244353

class DisjSet:

	def __init__(self, n):
		self.rank = [1] * n
		self.parent = [i for i in range(n)]

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1

def ps(n):
	cp = 0
	lk = 0
	arr = {}
	lk = 0
	ap = n
	cc = 0
	while n % 2 == 0:
		n = n // 2
		cc = 1
	if cc == 1:
		lk += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		cc = 0
		while n % ps == 0:
			n = n // ps
			cc = 1
		lk += 1 if cc == 1 else 0
	if n != 1:
		lk += 1
	if lk == 1:
		return False
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n):
		qlr = qlr * ip % mod
	for ij in range(1, r):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
from types import GeneratorType

def iterative(f, stack=[]):

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

def power(arr):
	listrep = arr
	subsets = []
	for i in range(2 ** len(listrep)):
		subset = []
		for k in range(len(listrep)):
			if i & 1 << k:
				subset.append(listrep[k])
		subsets.append(subset)
	return subsets

def pda(n):
	list = []
	su = 0
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				list.append(i)
				su += i
			else:
				list.append(n // i)
				list.append(i)
				su += i
				su += n // i
	return su

def dis(xa, ya, xb, yb):
	return sqrt((xa - xb) ** 2 + (ya - yb) ** 2)

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

def ilist():
	return [int(x) for x in input().strip().split()]

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

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

def google(test):
	return 'Case #' + str(test) + ': '

def overlap(x1, y1, x2, y2):
	if x2 > y1:
		return y1 - x2
	if y1 > y2:
		return y2 - x2
	return y1 - x2

def dist(x1, y1, x2, y2):
	return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

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

def prod(arr):
	n = len(arr)
	k = 1
	for j in range(n):
		k *= arr[j]
	return k

def SumOfDigits(s):
	su = 0
	while s:
		su += s % 10
		s = s // 10
	return su

def std():
	return stdout.flush()

def finds(s):
	att = 0
	i = 1
	while i < len(s):
		att = int(s[:i])
		j = i
		cc = 0
		while j < len(s):
			att += 1
			sk = att
			news = str(sk)
			rr = len(news)
			if news == s[j:j + rr]:
				j += rr
			else:
				cc = 1
				break
		if cc == 0:
			return True
		i += 1
	return False
arr = []
for i in range(1, 10001):
	s = str(i)
	j = i
	while len(s) <= 7:
		j += 1
		s += str(j)
		arr.append(int(s))
arr.sort()

def subs(arr):
	res = set()
	pre = {0}
	for x in arr:
		pre = {x | y for y in pre} | {x}
		res |= pre
	return len(res)
t = ii()
for pl in range(t):
	n = ii()
	arr = ilist()
	sk = set(arr)
	ans = -1
	for i in range(1, 1024):
		temp = set()
		for j in range(n):
			temp.add(arr[j] ^ i)
		cc = 0
		if temp == sk:
			cc = 1
			ans = i
			break
	print(ans)


def solution(a, n):
	temp = set()
	temp_a = list(a)
	for i in range(n):
		for j in range(i + 1, n):
			temp.add(temp_a[i] ^ temp_a[j])
	temp = sorted(list(temp))
	for i in list(temp):
		for j in temp_a:
			if i ^ j in a:
				continue
			else:
				break
		else:
			return i
	return -1

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = set(map(int, input().split()))
		print(solution(a, n))
main()


import sys
input = sys.stdin.readline

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

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

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

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	a = getList()
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 0
		d[a[i]] += 1
	res = -1
	for k in range(1, 1025):
		d2 = d.copy()
		yes = True
		for i in range(n):
			p = a[i] ^ k
			if p not in d2:
				yes = False
				break
			d2[p] -= 1
			if d2[p] < 0:
				yes = False
				break
		if yes:
			res = k
			break
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	found = False
	for i in range(1, 1024):
		ns = set([c ^ i for c in s])
		if s == ns:
			print(i)
			found = True
			break
	if not found:
		print(-1)


def Johnny_and_His_Hobbies(n, S):
	S.sort()
	k = -1
	for j in range(1, 1025):
		if sorted([j ^ i for i in S]) == S:
			k = j
			break
	return k
t = input()
for i in range(int(t)):
	n = input()
	s = list(map(int, input().split()))
	print(Johnny_and_His_Hobbies(int(n), s))


t = int(input())
import math
for _ in range(t):
	n = int(input())
	s = set([int(x) for x in input().split()])
	if n == 1:
		print(-1)
		continue
	m = 2 ** (int(math.log2(max(s))) + 1) - 1
	ans = 0
	k = 1
	while k <= m and ans == 0:
		s2 = set()
		for i in s:
			s2.add(i ^ k)
		if s == s2:
			ans = k
		k += 1
	if ans != 0:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	lst1 = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		a = 0
		for k in range(1, 1025):
			lst2 = []
			for i in range(n):
				lst2.append(lst1[i] ^ k)
			if sorted(lst2) == sorted(lst1):
				a = 1
				break
		if a == 1:
			print(k)
		else:
			print(-1)


t = int(input())
for q in range(t):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	ans = True
	m = -1
	for i in range(1, 1024):
		ans = True
		l = []
		for j in a:
			l.append(i ^ j)
		if sorted(l) != a:
			ans = False
		if ans:
			m = i
			break
	if ans:
		print(m)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = -1
	for j in range(1, 1025):
		l1 = []
		for k in range(len(l)):
			l1.append(l[k] ^ j)
		l1.sort()
		if l == l1:
			ans = j
			break
	print(ans)


from sys import stdin, stdout

def get_int():
	return int(stdin.readline().strip())

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

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

def get_string():
	return stdin.readline().strip()
for _ in range(int(stdin.readline())):
	n = get_int()
	s = get_array()
	k = 1
	m = max(s)
	f = 0
	while k < 1024:
		for i in s:
			j = i ^ k
			if j in s:
				continue
			else:
				break
		else:
			print(k)
			f = 1
			break
		k += 1
	if f == 0:
		print(-1)


import math
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	d = set(s)
	a = set()
	(b, c) = (0, 0)
	for k in range(1, 1025):
		for i in range(n):
			a.add(s[i] ^ k)
		if a == d:
			b = k
			c = 1
			break
		else:
			a = set()
	if c == 1:
		print(b)
	else:
		print(-1)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		for j in s:
			if i ^ j not in s:
				break
		else:
			ans = i
			break
	print(ans)


def func():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	arr.sort()
	for j in range(1, 1024):
		arr1 = []
		for i in range(n):
			arr1.append(arr[i] ^ j)
		if sorted(arr1) == arr:
			print(j)
			return
	print(-1)
for _ in range(int(input())):
	func()


import sys
import math
import operator as op
from functools import reduce

def read_line():
	return sys.stdin.readline()[:-1]

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

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer / denom
N = int(1000000.0) + 10
fenwick = [0] * N

def add(i, x, n):
	while i < n:
		fenwick[i] += x
		i = i | i + 1

def fsum(i, n):
	res = 0
	while i >= 0:
		res += fenwick[i]
		i = (i & i + 1) - 1
	return res

def fsum_range(i, j, n):
	return fsum(j, n) - fsum(i - 1, n)
mod = int(1000000000.0) + 7
t = read_int()
for i in range(t):
	n = read_int()
	a = read_int_line()
	d = {}
	for i in a:
		d[i] = 1
	ans = -1
	if max(a) == 0:
		print(ans)
		continue
	val = math.ceil(math.log(max(a), 2)) + 1
	val = 1 << val
	for i in range(1, val):
		f = True
		for j in range(n):
			if i ^ a[j] not in d:
				f = False
		if f:
			ans = i
			break
	print(ans)


from collections import *
for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	d = Counter((x ^ y for (i, x) in enumerate(a) for y in a[:i]))
	print(min([k for k in d if d[k] * 2 == len(a)] or [-1]))


import sys
import heapq
import random
import collections
import math

def solve(lst):
	console('----- solving ------')
	lst = sorted(lst)
	if len(lst) % 2 != 0:
		return -1
	sett = set(lst)
	for k in range(1, 1024):
		lss = [a ^ k for a in lst]
		if set(lss) == sett:
			return k
	return -1

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	_ = int(input())
	lst = list(map(int, input().split()))
	res = solve(lst)
	print(res)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(l()[0]):
	n = l()[0]
	A = l()
	A.sort()
	ans = -1
	for i in range(1, 1025):
		B = [A[j] ^ i for j in range(n)]
		B.sort()
		if A == B:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ar = set(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		kli = set({})
		for i in ar:
			for j in ar:
				if i != j:
					kli.add(i ^ j)
		ans = -1
		kli = list(kli)
		kli.sort()
		for i in kli:
			flag = True
			for j in ar:
				if not i ^ j in ar:
					flag = False
					break
			if flag:
				ans = i
				break
		print(ans)


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	s = set(a)
	flag = True
	for k in range(1, 1025):
		tmp = set()
		for i in s:
			tmp.add(i ^ k)
		if tmp == s:
			flag = False
			print(k)
			break
	if flag:
		print(-1)


tt = int(input())
for qwerty in range(tt):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = set(arr)
	brr = set()
	var = 0
	for i in range(1, 1024):
		brr.clear()
		for j in arr:
			z = j ^ i
			brr.add(z)
		val = brr - arr
		if not val:
			print(i)
			var = 1
			break
	if var == 0:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * (n - 1)
	a1 = l[0]
	index = 0
	for i in range(1, n):
		l1[index] = l[i] ^ a1
		index += 1
	l1.sort()
	st = set(l)
	l = []
	flag = 0
	for i in l1:
		flag = 1
		for k in st:
			if i ^ k not in st:
				flag = 0
				break
		if flag == 1:
			print(i)
			break
	if flag == 0:
		print(-1)


n = int(input())
for i in range(0, n):
	y = int(input())
	x = list(map(int, input().split()))
	z = [0] * y
	x.sort()
	j = 1
	while j < 1025:
		for k in range(0, y):
			z[k] = x[k] ^ j
		z.sort()
		if x == z:
			break
		j += 1
	if j == 1025:
		print('-1')
	else:
		print(j)


import os
import sys
import io
import math
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s1 = set(a)
	k = 1
	while k <= 1024:
		check = True
		for i in a:
			if i ^ k not in s1:
				check = False
				break
		if check == True:
			print(k)
			break
		k += 1
	if check == False:
		print(-1)


for _ in range(int(input())):
	input()
	s = set(list(map(int, input().split())))
	for i in range(1, 1025):
		for x in s:
			if x ^ i not in s:
				break
		else:
			print(i)
			break
	else:
		print(-1)


def solve():
	n = int(input())
	a = set(map(int, input().split()))
	ans = -1
	for j in range(1, 10 ** 4):
		s = set()
		for i in a:
			s.add(i ^ j)
		if s == a:
			ans = j
			break
	print(ans)
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	m = dict(map(lambda x: (x[1], x[0]), enumerate(s)))
	temp = n * [0]
	flag = True
	for i in range(1, n):
		k = s[0] ^ s[i]
		flag2 = True
		for e in s:
			if e ^ k in m:
				if temp[m[e ^ k]] != -1:
					temp[m[e ^ k]] = 1
				else:
					flag2 = False
					break
			else:
				flag2 = False
				break
		if flag2:
			print(k)
			flag = False
			break
	if flag:
		print(-1)


t = int(input())
import heapq as hq
while t:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	k = []
	hq.heapify(k)
	for i in range(1, n):
		hq.heappush(k, arr[0] ^ arr[i])
	res = -1
	while k:
		res = hq.heappop(k)
		for j in arr:
			if res ^ j not in arr:
				res = -1
				break
		if res != -1:
			break
	if res == 0:
		print(-1)
	else:
		print(res)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	z = []
	for k in range(1, 1025):
		z = []
		for j in range(n):
			z.append(a[j] ^ k)
		if set(a) == set(z):
			print(k)
			break
	if k == 1024:
		print('-1')


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	setmain = set(lst)
	flag = 0
	for s in range(1, 1025):
		set1 = []
		for k in lst:
			set1.append(s ^ k)
		if set(set1) == setmain:
			print(s)
			flag = 1
			break
	if flag == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s1 = set(l)
	f = 0
	for i in range(1, 1024):
		s2 = set()
		for j in range(n):
			s2.add(l[j] ^ i)
		if s1 == s2:
			f = 1
			t = i
			break
	if f == 1:
		print(t)
	else:
		print('-1')


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = a.copy()
	x.sort()
	answer = -1
	if x[0] == 0:
		for i in range(1, n):
			temporary = []
			for j in range(n):
				temporary.append(a[j] ^ x[i])
			if set(temporary) == set(a):
				answer = x[i]
				break
		print(answer)
	else:
		m = max(x)
		y = 0
		while 1:
			if 2 ** y > m:
				break
			y += 1
		for i in range(1, 2 ** y + 1):
			temporary = []
			for j in range(n):
				temporary.append(a[j] ^ i)
			if set(temporary) == set(a):
				answer = i
				break
		print(answer)


t = int(input())
for q in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	f = True
	for i in range(1, 1024):
		s2 = set()
		for j in s:
			s2.add(j ^ i)
		if s == s2:
			print(i)
			f = False
			break
	if f:
		print(-1)


T = int(input())
for t_itr in range(T):
	N = int(input())
	S = set(map(int, input().rstrip().split()))
	found = False
	for k in range(1, 1025):
		found = S == set(map(lambda x: x ^ k, S))
		if found:
			break
	if k == 1024:
		print(-1)
	else:
		print(k)


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	for k in range(1, 1024):
		tmp = {i ^ k for i in s}
		if s == tmp:
			print(k)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = list(input().strip())
	s = list(map(int, input().strip().split()))
	check = set(s)
	found = False
	for i in range(1, 1025):
		newset = set([e ^ i for e in s])
		if check == newset:
			print(i)
			found = True
			break
	if not found:
		print(-1)


def solve(a):
	m = max(a)
	b = sorted([i ^ m for i in a if i != m])
	for i in b:
		found = True
		for j in a:
			if j ^ i not in a:
				found = False
				break
		if found:
			return i
	return -1
n = int(input())
for i in range(n):
	k = input()
	a = list(map(int, input().split()))
	print(solve(a))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def solve(n, S):
	if n == 1:
		return -1
	a = S[0]
	for i in range(1, n):
		T = []
		t = a ^ S[i]
		for s in S:
			T.append(s ^ t)
		T.sort()
		for (u, v) in zip(S, T):
			if u != v:
				break
		else:
			return t
	return -1

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		S = list(map(int, input().split()))
		S.sort()
		ans = solve(n, S)
		sys.stdout.write(str(ans) + '\n')
main()


t = int(input())
while t:
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	p = -1
	for i in range(1, 1024):
		check = []
		for j in range(0, n):
			check.append(i ^ s[j])
		check.sort()
		for k in range(0, n):
			if s[k] != check[k]:
				break
		else:
			p = i
			break
	print(p)
	t = t - 1


t = int(input())
for _ in range(t):
	n = int(input())
	s = set((int(i) for i in input().split()))
	for i in range(1, 1025):
		found = True
		for s_i in s:
			if not s_i ^ i in s:
				found = False
				break
		if found:
			break
	if found:
		print(i)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	num = 0
	for i in range(1, 1025):
		ans = []
		for j in arr:
			ans.append(j ^ i)
		num = i
		flag = True
		for i in ans:
			if i not in arr:
				flag = False
		if flag:
			break
	if num == 1024:
		print(-1)
	else:
		print(num)


t = int(input())
while t:
	n = int(input())
	z = input('').split(' ')
	z = list(map(int, z))
	z.sort()
	ans = -1
	for i in range(1, 1025):
		l = list(map(lambda x: i ^ x, z))
		l.sort()
		if l == z:
			ans = i
			break
	print(ans)
	t -= 1


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = inar()
		arr.sort()
		ans = -1
		for i in range(1, 1024 + 1):
			lis = []
			for j in range(n):
				lis.append(arr[j] ^ i)
			lis.sort()
			if lis == arr:
				ans = i
				break
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	flag = 0
	s.sort()
	for k in range(1, 1024, 1):
		new = []
		for i in range(n):
			t = k ^ s[i]
			if t not in s:
				break
			else:
				new.append(t)
		if sorted(new) == s:
			print(k)
			flag = 1
			break
	if flag == 0:
		print(-1)


import sys
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = list(set(l))
	if n == 1:
		print(-1)
	else:
		t = sys.maxsize
		x = l[0]
		f = 0
		for i in range(1, n):
			y = x ^ l[i]
			p = []
			for j in l:
				p.append(y ^ j)
			if set(p) == set(s):
				f = 1
				t = min(t, y)
		if f == 0:
			print(-1)
		else:
			print(t)


import sys

def input():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(int(input())):
		n = int(input())
		s = set(map(int, input().split()))
		f = 0
		for k in range(1, 1025):
			cnt = 0
			for e in s:
				if k ^ e in s:
					cnt += 1
				else:
					break
			if cnt == len(s):
				print(k)
				f = 1
				break
		if f == 0:
			print(-1)
main()


I = input
N = 1025
for _ in range(int(I())):
	n = int(I())
	a = [int(i) for i in I().split()]
	Is = [False] * N
	for i in a:
		Is[i] = True

	def chk(k):
		for i in range(n):
			if not Is[a[i] ^ k]:
				return False
		return True
	ans = -1
	for k in range(1, 1024):
		if chk(k):
			ans = k
			break
	print(ans)


def solve(n, s):
	for i in range(1, 1025):
		s1 = set((int(i ^ x) for x in s))
		if s1 == s:
			return i
	return -1
for _ in range(int(input())):
	n = int(input())
	s = set((int(x) for x in input().split()))
	print(solve(n, s))


import math
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	Set = set(arr)
	found = False
	for k in range(1, 1025):
		s = set([a ^ k for a in Set])
		if s == Set:
			print(k)
			found = True
			break
	if not found:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split(' ')))
	s_c = s.copy()
	ans_flag = False
	for i in range(1, 1025):
		for j in range(len(s)):
			xor_result = s[j] ^ i
			if xor_result in s_c:
				s_c.remove(xor_result)
			else:
				s_c = s.copy()
				break
		if len(s_c) == 0:
			ans_flag = True
			print(i)
			break
	if not ans_flag:
		print(-1)


t = int(input())
for q in range(t):
	n = int(input())
	mas = list(map(int, input().split()))
	s1 = set(mas)
	flag = False
	for k in range(1, 1024):
		s2 = set()
		for i in mas:
			s2.add(i ^ k)
		if s1 == s2:
			print(k)
			flag = True
			break
	if not flag:
		print(-1)


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	n = int(input())
	s = set([int(x) for x in input().split()])
	for i in range(1, 1025):
		flag = 1
		new_s = set()
		for k in s:
			new_s.add(k ^ i)
		if new_s == s:
			print(i)
			flag = 0
			break
	if flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = sorted([*map(int, input().split())])
	f = False
	for i in range(1, a[-1] * 2):
		tmp = []
		for j in a:
			if j not in tmp:
				tmp.append(j)
				j ^= i
				tmp.append(j)
		if set(tmp) == set(a):
			f = True
			break
	if f:
		print(i)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	i = 1
	c = 0
	v = [False for _ in range(n)]
	while i < n:
		d = a[i] ^ a[0]
		for j in range(n):
			if a[j] ^ d in a:
				c += 1
			else:
				break
		if c == n:
			print(d)
			break
		else:
			c = 0
			i += 1
	if c != n:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	temp = set(a)
	ans = -1
	for i in range(1, 1024):
		b = temp.copy()
		for j in a:
			b.discard(i ^ j)
		if len(b) == 0:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ss = list(map(int, input().split()))
	sss = set(ss)
	ss0 = ss[0]
	mink = 1024
	for s in ss[1:]:
		k = ss0 ^ s
		if sss == {x ^ k for x in ss}:
			mink = min(mink, k)
	print(-1 if mink == 1024 else mink)


import math
for test in range(int(input())):
	het = int(input())
	lis = list(map(int, input().split()))
	set0 = set(lis)
	for i in range(1, 1025):
		pat = []
		for j in lis:
			pat.append(i ^ j)
		set1 = set(pat)
		if set1 == set0:
			print(i)
			break
	else:
		print(-1)


def mostsignificant(a):
	pos = 0
	while a != 0:
		pos += 1
		a = a >> 1
	return pos
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	l.sort()
	pos = mostsignificant(max(l))
	flag = 0
	for i in range(1, 2 ** pos + 1):
		p = [j ^ i for j in l]
		p.sort()
		if p == l:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


def solve():
	n = int(input())
	li = [int(x) for x in input().split()]
	li = list(set(li))
	d = {}
	ll = 0
	for ele in li:
		d[ele] = True
		ll += 1
	for i in range(1, 1025):
		ele = i ^ li[0]
		if d.get(ele, -1) == -1:
			continue
		for j in range(1, ll):
			x = li[j] ^ i
			if d.get(x, -1) == -1:
				break
		else:
			print(i)
			return
	else:
		print(-1)
t = int(input())
for i in range(t):
	solve()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		b = []
		for j in range(n):
			b.append(i ^ a[j])
		if set(a) == set(b):
			ans = i
			break
	print(ans)


def main():
	NIN = lambda : int(input())
	SIN = lambda : map(int, input().split())
	LIN = lambda : list(map(int, input().split()))
	from collections import defaultdict as dd
	t = NIN()
	for _ in range(t):
		n = NIN()
		a = LIN()
		d = dd(lambda : 0)
		if n % 2:
			print(-1)
			continue
		for i in a:
			for j in a:
				d[i ^ j] += 1
		ok = 0
		for i in sorted(d):
			if d[i] == n and i > 0:
				print(i)
				ok = 1
				break
		if not ok:
			print(-1)
	return
main()


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s = set(s)
	s1 = set()
	ans = -1
	for i in range(1, 2048):
		s1 = set()
		for j in s:
			s1.add(j ^ i)
		if s == s1:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	j = 1
	cnt = []
	flag = 0
	for j in range(1, 1026):
		for i in a:
			cnt.append(j ^ i)
		if set(cnt) == set(a):
			print(j)
			flag = 1
			break
		cnt = []
	if flag == 0:
		print(-1)


import collections
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = collections.defaultdict(int)
	ans = 1000000.0
	for i in range(n):
		for j in range(i + 1, n):
			t = a[i] ^ a[j]
			d[t] += 1
			if d[t] >= (n + 1) // 2 and t >= 1:
				if ans > t:
					ans = t
	if ans == 1000000.0:
		print(-1)
	else:
		print(ans)


int_inp = lambda : int(input())
strng = lambda : input().strip()
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
import math
from collections import Counter, defaultdict
for _ in range(int_inp()):
	k = int_inp()
	m = seq()
	s1 = set(m)
	s2 = set()
	f = -1
	binary = bin(max(s1))[2:]
	a = len(binary)
	a = int('1' * a, 2)
	for j in range(1, a + 1):
		for i in m:
			s2.add(i ^ j)
		if s1 == s2:
			f = j
			break
		else:
			s2.clear()
	print(f)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s = set(s)
	b = 0
	for k in range(1, 1025):
		s1 = set((i ^ k for i in s))
		if s == s1:
			print(k)
			b = 1
			break
	if b == 0:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	arr.sort()
	k = 1
	flag = 0
	while k <= 1024 and flag == 0:
		brr = []
		for j in range(n):
			brr.append(k ^ arr[j])
		brr.sort()
		if arr == brr:
			flag = 1
			break
		else:
			k = k + 1
	if flag == 0:
		print(-1)
	elif flag == 1:
		print(k)


import sys
lines = []
for line in sys.stdin:
	if 'Exit' == line.rstrip():
		break
	lines.append(line)
t = int(lines[0])
for i in range(1, 2 * t + 1, 2):
	n = int(lines[i])
	if n == 0:
		print(-1)
		continue
	s = set([int(x) for x in lines[i + 1].split(' ')])
	max = -1
	for num in s:
		if num > max:
			max = num
	k = 1
	while True:
		new_s = set()
		ln = 0
		for num in s:
			new_s.add(num ^ k)
			ln += 1
		if new_s == s and ln == len(s):
			print(k)
			break
		if k > max << 1:
			print(-1)
			break
		k += 1


n = int(input())
answers = [] * 0
for problems in range(n):
	inputs = [] * 0
	zzz = input()
	xxx = input().split(' ')
	for qwe in xxx:
		inputs.append(int(qwe))
	ans = 1024
	for i in range(1, len(inputs)):
		test = int(inputs[0] ^ inputs[i])
		inputs2 = [] * 0
		for j in range(len(inputs)):
			inputs2.append(int(inputs[j]) ^ test)
		if sorted(inputs) == sorted(inputs2):
			ans = min(ans, test)
	if ans == 1024:
		answers.append(-1)
	else:
		answers.append(ans)
for answer in answers:
	print(answer)


from collections import defaultdict as dfd
import math
import sys
import heapq

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

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

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

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	for _ in range(int(input())):
		N = II()
		A = LI()
		A = set(A)
		A = list(A)
		A.sort()
		ren = []
		io = 0
		for i in range(1, 1025):
			Z = []
			for j in range(len(A)):
				Z.append(i ^ A[j])
			Z.sort()
			if Z == A:
				io = 1
				print(i)
				break
		if io == 0:
			print(-1)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	sl = list(map(int, input().split()))
	maxx = max(sl)
	i = 1
	while i < 1024:
		if i in sl and 0 not in sl:
			i += 1
			continue
		ss = set()
		tsl = sl
		cnt = 0
		for j in tsl:
			if j ^ i in tsl:
				ss.add(j ^ i)
			else:
				break
		if len(ss) == len(sl):
			break
		else:
			i += 1
	if len(ss) != len(sl):
		print(-1)
	else:
		print(i)


from collections import defaultdict

def Hash(a):
	ar = defaultdict(int)
	for x in a:
		ar[x] += 1
	return ar

def xor(a, k):
	f = []
	for x in a:
		f.append(x ^ k)
	return f
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	aa = Hash(a)
	k = max(a)
	ans = 0
	for i in range(1, 1023 + 1):
		g = Hash(xor(a, i))
		if g == aa:
			ans = i
			break
	if ans != 0:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 2000
	for x in a:
		b[x] = 1
	ans = 1000010000
	for i in range(1, n):
		x = a[0] ^ a[i]
		ok = 1
		for y in a:
			if not b[x ^ y]:
				ok = 0
		if ok:
			ans = min(ans, x)
	print(ans if ans < 1000010000 else -1)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
for _ in range(int(input())):
	n = int(input())
	l = set(map(int, input().split()))
	f = 0
	for i in range(1, 1025):
		s = set()
		for j in l:
			if j ^ i in l:
				s.add(j ^ i)
		if len(s) == n:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


def solve(numbers):
	numbers = set(numbers)
	(head, *rest) = numbers
	options = set((head ^ x for x in rest))
	for x in sorted(options):
		if set((y ^ x for y in numbers)) == numbers:
			return x
	return -1
for T in range(int(input())):
	int(input())
	print(solve(map(int, input().split())))


for t in range(int(input())):
	ans = -1
	n = int(input())
	s = {int(i) for i in input().split()}
	i = 1
	while i < 1024:
		a = {x ^ i for x in s}
		if a == s:
			ans = i
			break
		i += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	ans = -1
	for k in range(1, 1024):
		s1 = set()
		for j in range(n):
			s1.add(l[j] ^ k)
		if s == s1:
			ans = k
			break
	print(ans)


for i in range(int(input())):
	num = int(input())
	a = [int(i) for i in input().split()]
	found = False
	l = 0
	i = 0
	while i != 1024:
		b = []
		i += 1
		for k in a:
			b.append(k ^ i)
		if sorted(a) == sorted(b):
			l = i
			found = True
			break
	if found:
		print(l)
	else:
		print(-1)


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P
from bisect import bisect_left as BL, bisect_right as BR
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY, log as LOG

def player1():
	print('')

def player2():
	print('')

def isPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def main():
	for _ in range(int(input())):
		n = int(input())
		s = set(map(int, input().split()))
		m = max(s)
		ans = -1
		for k in range(1, 2000):
			a = set()
			for v in s:
				a.add(v ^ k)
			if a == s:
				ans = k
				break
		print(ans)
main()


import sys
import math

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

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

def ILS():
	return list(map(str, sys.stdin.readline().strip().split()))
t = I()
for iii in range(t):
	n = I()
	l = IL()
	d = set(l)
	ans = -1
	for i in range(1, 1025):
		ss = set()
		for ll in l:
			ss.add(ll ^ i)
		if ss == d:
			ans = i
			break
	print(ans)


def fun(lis, k):
	for x in range(len(lis)):
		lis[x] = lis[x] ^ k
	lis.sort()
	return lis
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	for i in range(1, 1025):
		if a == fun(a[:], i):
			print(i)
			break
	else:
		print(-1)


testing_mode = True
import sys, traceback
test_data = ''
final_output = ''

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for x in range(multi)]
	else:
		next_line = next(test_data, None).strip() if test_data else sys.stdin.readline()
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = next_line.split()
		elif input_format == 4:
			formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
		elif input_format == 5:
			formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
		else:
			formatted_input = [next_line]
		return formatted_input

def log(*args):
	if testing_mode:
		print(*args)

def out(output_line, output_format=0, newline=True):
	global final_output
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 1:
		formatted_output = '\n'.join(map(str, output_line))
	if testing_mode:
		final_output += formatted_output + '\n' * newline
	else:
		print(formatted_output, end='\n' if newline else '')
test_inputs = ['\n6\n4\n1 0 2 3\n6\n10 7 14 8 3 12\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 1023\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']
test_outputs = ['\n1\n4\n2\n-1\n-1\n1023\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']

def solve():
	for z in range(input(1)[0]):
		n = input(1)[0]
		s = set(input(1))
		for i in range(1, 1024):
			c = set()
			for j in s:
				c.add(j ^ i)
			if c == s:
				out(i)
				break
		else:
			out(-1)
	return

def run_all(overwrite_testing=False):
	global testing_mode, final_output, test_data
	testing_mode = overwrite_testing and testing_mode
	if testing_mode:
		tests_failed = 0
		for (test_number, (test_in, test_out)) in enumerate(zip(test_inputs, test_outputs)):
			final_output = ''
			if test_in.lstrip()[:3] == '$$$':
				continue
			test_data = iter([i for i in test_in.split('\n') if i])
			print(f'Test Case #{test_number + 1}:')
			print('<' * 20)
			fail = False
			verdict = '\x1b[92m\x1b[1mPASS\x1b[0m'
			try:
				solve()
				final = final_output.rstrip('\n')
				raw_out = iter(map(str, final.split('\n'))) if final else iter(())
			except Exception:
				raw_out = ''
				fail = True
				print('\x1b[95m')
				traceback.print_exc()
				print('\x1b[0m')
			if raw_out:
				if test_out.lstrip()[:3] == '$$$':
					verdict = '\x1b[94m\x1b[1mSKIP\x1b[0m'
					print('\x1b[94m' + '\n'.join(list(raw_out)) + '\x1b[0m')
				else:
					for line_out in [i for i in test_out.split('\n') if i]:
						line_raw = next(raw_out, '% None %')
						if line_raw == line_out.strip():
							print(f'\x1b[92m{line_raw}\x1b[0m')
						else:
							print(f'\x1b[91m{line_raw}\x1b[0m')
							fail = True
					line_raw = next(raw_out, None)
					while line_raw:
						print(f'\x1b[91m{line_raw}\x1b[0m')
						line_raw = next(raw_out, None)
						fail = True
			print('>' * 20)
			if fail:
				verdict = '\x1b[91m\x1b[1mFAIL\x1b[0m'
				tests_failed += 1
			print(verdict + '\n')
		if tests_failed:
			print(f"\x1b[91m\x1b[1m{tests_failed} TEST{('S' if tests_failed > 1 else '')} FAILED!\x1b[0m")
		else:
			print(f'\x1b[92m\x1b[1mALL TESTS PASSED!\x1b[0m')
	else:
		test_data = False
		solve()
run_all(False)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	s = set(lst)
	ans = -1
	for i in range(1, 1024):
		new_st = set()
		for j in range(n):
			new_st.add(lst[j] ^ i)
		if new_st == s:
			ans = i
			break
	print(ans)


def alreadyFormed(a_dict, elem):
	try:
		return a_dict[elem]
	except:
		return True

def solve():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	a_dict = {i: False for i in a}
	for i in range(1, 1024 + 1):
		found_ans = True
		temp_dict = dict(a_dict)
		for ai in a:
			xor = ai ^ i
			if alreadyFormed(temp_dict, xor):
				found_ans = False
				break
			else:
				temp_dict[xor] = True
		if found_ans:
			print(i)
			return
	print(-1)
t = int(input().strip())
for _ in range(t):
	solve()


def ceillog(num, base):
	n = num
	res = 0
	while n:
		n //= base
		res += 1
	return res
for _ in range(int(input())):
	n = int(input())
	a = set([int(i) for i in input().split()])
	flag = 0
	for i in range(1, 2 ** ceillog(max(a), 2) + 1):
		st = set()
		for j in a:
			st.add(j ^ i)
		if st == a:
			flag = 1
			break
	if flag == 1:
		print(i)
	else:
		print(-1)


def check(arr):
	brr = set(arr)
	n = 1024
	for i in range(1, n + 1):
		f = 1
		for j in arr:
			if j ^ i not in brr:
				f = 0
				break
		if f == 1:
			return i
	return -1
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	print(check(arr))
	t -= 1


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		n = int(input())
		ara = [int(num) for num in input().split()]
		ara.sort()
		ans = -1
		for position in range(1, n):
			var = ara[0] ^ ara[position]
			current = []
			for index in range(n):
				current.append(var ^ ara[index])
			current.sort()
			for index in range(n):
				if ara[index] != current[index]:
					break
			else:
				ans = var
				break
		print(ans)
main()


from collections import deque, Counter, defaultdict
import math
from sys import stdin
import bisect
input = stdin.readline

def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1

def solve():
	n = int(input())
	l1 = list(map(int, input().split()))
	l1.sort()
	for i in range(1, 1025):
		l2 = []
		for j in range(n):
			v = l1[j] ^ i
			l2.append(v)
		l2.sort()
		if l1 == l2:
			print(i)
			return
	print(-1)
for _ in range(int(input())):
	solve()


def dictionize(l):
	d = {}
	for x in l:
		if x in d.keys():
			d[x] += 1
		else:
			d[x] = 1
	return d
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = [int(x) for x in input().split()]
	if n % 2 == 1:
		print(-1)
		continue
	sol = dictionize(l)
	can = {}
	for x in l[1:]:
		if l[0] != x:
			can[l[0] ^ x] = [l[0], x]
	for i in range(1, len(l) - 1):
		for j in range(i + 1, len(l)):
			xr = l[i] ^ l[j]
			if xr in can.keys():
				can[xr] += [l[i], l[j]]
	ks = list(can.keys())
	ks.sort()
	valid = False
	for elem in ks:
		if dictionize(can[elem]) == sol:
			valid = True
			print(elem)
			break
		else:
			continue
	if not valid:
		print(-1)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = sorted(map(int, input().split()))
	for x in range(1, 1025):
		if sorted((i ^ x for i in a)) == a:
			print(x)
			break
	else:
		print(-1)


from math import log2

def main():
	for t in range(int(input())):
		n = int(input())
		l = [int(j) for j in input().split()]
		s = set(l)
		for i in range(1, 1025):
			m = set()
			for j in range(n):
				m.add(i ^ l[j])
			if m == s:
				print(i)
				break
		else:
			print(-1)
main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	flag = 0
	for i in range(1, 1024):
		new = [x ^ i for x in s]
		if set(new) == set(s):
			flag = 1
			print(i)
			break
	if flag == 0:
		print(-1)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, Counter
import math

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

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
t = intIn()
while t > 0:
	t -= 1
	n = intIn()
	a = listIn()
	if n % 2 != 0:
		print(-1)
	else:
		c = Counter(a)
		li = []
		for i in range(1, n):
			li.append(a[0] ^ a[i])
		li = sorted(li)
		s1 = set(a)
		ans = -1
		for e1 in li:
			s2 = set()
			for e2 in s1:
				x = e1 ^ e2
				if c[x] == 0:
					break
				s2.add(x)
			if s1 == s2:
				ans = e1
				break
		print(ans)


T = int(input())
D = []
for i in range(0, T):
	n = int(input())
	E = []
	B = []
	A = [int(x) for x in input().split(' ')]
	A.sort()
	if n % 2 == 1:
		D.append(-1)
	else:
		for j in range(0, n):
			x = A[0] ^ A[j]
			B.append(x)
		for x in B:
			C = [y ^ x for y in A]
			C.sort()
			if C == A and x != 0:
				E.append(x)
		if len(E) > 0:
			D.append(min(E))
		else:
			D.append(-1)
for i in D:
	print(i)


t = int(input())
for i in range(0, t):
	n = int(input())
	q = input()
	q = q.split()
	for j in range(0, n):
		q[j] = int(q[j])
	s1 = set(q)
	ans = 1
	verdict = 0
	while verdict == 0:
		out = []
		for i in range(0, n):
			out.append(q[i] ^ ans)
		s2 = set(out)
		if s1 - s2 == set():
			verdict = 1
		elif ans == 1025:
			ans = -1
			verdict = 1
		else:
			ans += 1
	print(ans)


from sys import stdin, stdout
from collections import Counter

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

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

def ip():
	return int(stdin.readline().strip())

def op(ans):
	return stdout.write(str(ans) + '\n')
for i in range(ip()):
	n = ip()
	li = ai()
	s = set(li)
	mila = 0
	for i in range(1, 1025):
		c = 0
		for j in li:
			if i ^ j in s:
				c += 1
		if c == n:
			print(i)
			mila = 1
			break
	if mila == 0:
		print(-1)


import sys

class BJohnnyAndHisHobbies:

	def solve(self, tc=0):
		for _ in range(int(input())):
			n = int(input())
			a = set((int(_) for _ in input().split()))
			for x in range(1, 1 << 10):
				if a == set((e ^ x for e in a)):
					print(x)
					break
			else:
				print(-1)
solver = BJohnnyAndHisHobbies()
input = sys.stdin.readline
solver.solve()


import sys

def read():
	return set(map(int, sys.stdin.readline().split()))
for t in range(int(input())):
	n = int(input())
	arr = read()
	done = 0
	for i in range(1, 1025):
		lst = set()
		for j in arr:
			lst.add(j ^ i)
		if arr == lst:
			print(i)
			done = 1
			break
	if not done:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = set(arr)
	c = -1
	for i in range(1, 1025):
		check = False
		for j in range(n):
			if i ^ arr[j] in d:
				continue
			else:
				check = True
				break
		if check == False:
			c = i
			break
	print(c)


t = int(input())

def solve(n, arr):
	possible = set()
	for i in range(len(arr)):
		for j in range(i, len(arr)):
			k = arr[i] ^ arr[j]
			if k > 0:
				possible.add(k)
	arrs = set(arr)
	best = float('inf')
	for k in possible:
		created = set()
		for v in arr:
			created.add(v ^ k)
		if created == arrs:
			best = min(best, k)
	if best != float('inf'):
		return best
	return -1
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


T = int(input())
for t in range(T):
	n = int(input())
	S = [int(_) for _ in input().split()]
	setS = set(S)
	for k in range(1, 1025):
		for el in setS:
			if el ^ k not in setS:
				break
		else:
			print(k)
			break
	else:
		print(-1)


import collections

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

def readInt():
	return int(input().strip())

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

def solve(n, arr):
	arrSet = set(arr)
	MAX = 1024
	for k in range(1, MAX + 1):
		isPossible = True
		for val in arr:
			if k ^ val not in arrSet:
				isPossible = False
				break
		if isPossible:
			return k
	return -1
t = readInt()
for i in range(t):
	n = readInt()
	arr = readList()
	print(solve(n, arr))


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	anslist = []
	temp = 0
	for i in range(1, 1024):
		C = []
		for j in range(N):
			t = i ^ A[j]
			C.append(t)
		if sorted(C) == sorted(A):
			anslist.append(i)
	if len(anslist) == 0:
		print(-1)
	else:
		print(min(anslist))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	original = set(a)
	maximum = max(a)
	k = 0
	for i in range(1, 2057):
		if k == 1:
			break
		x = set()
		for j in a:
			x.add(j ^ i)
		if x == original:
			print(i)
			k = 1
			break
	if k == 0:
		print(-1)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	map1 = {}
	for j in arr:
		if j not in map1:
			map1[j] = 0
		map1[j] += 1
	count = True
	for j in range(1, 1025):
		map2 = {}
		for k in arr:
			num = k ^ j
			if num not in map2:
				map2[num] = 0
			map2[num] += 1
		count = True
		for k in map2:
			if k not in map1:
				count = False
				break
			elif map2[k] != map1[k]:
				count = False
				break
		if count == True:
			print(j)
			break
	if count == False:
		print('-1')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	f = 0
	for i in range(1, 1025):
		x = set()
		for y in l:
			x.add(i ^ y)
		if s == x:
			print(i)
			f = 1
			break
	if not f:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	arrxor = []
	maxarr = max(arr)
	start = 2
	cont = 1
	while maxarr >= start:
		start = start * 2
		cont = cont + 1
	for j in range(1, 2 ** cont + 1):
		for element in arr:
			bitwise = element ^ j
			arrxor.append(bitwise)
		arreglado = sorted(arrxor)
		if arreglado == arr:
			print(j)
			break
		if j == 2 ** cont:
			print(-1)
			break
		arrxor.clear()
		arreglado.clear()


import math
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	k = 1
	ans = -1
	while k <= 1024:
		B = [A[i] ^ k for i in range(n)]
		B.sort()
		if B == A:
			ans = k
			break
		k += 1
	print(ans)


for _ in range(int(input())):
	(n, res, r) = (int(input()), {}, -1)
	a = list(map(int, input().split()))
	for i in range(len(a)):
		for j in range(len(a)):
			if i != j:
				t = res.get(a[i] ^ a[j], 0)
				res[a[i] ^ a[j]] = t + 1
	for i in res.keys():
		if res[i] == len(a):
			if r == -1:
				r = i
			elif r > i:
				r = i
	print(r)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = set(map(int, stdin.readline().rstrip().split(' ')))
	for k in range(1, 1024):
		for ele in arr:
			if ele ^ k not in arr:
				break
		else:
			print(k)
			break
	else:
		print(-1)


t = int(input())
for i in range(t):
	items = int(input())
	num = set([int(x) for x in input().split()])
	new_set = set()
	if items == 1:
		print(-1)
	else:
		for i in range(1, 1024):
			new_set = set([x ^ i for x in num])
			out = num.intersection(new_set)
			if len(out) == len(num):
				print(i)
				break
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	list1 = [int(x) for x in input().split()]
	list1.sort()
	val = 0
	for i in range(1, 1024):
		list2 = [x ^ i for x in list1]
		list2.sort()
		if list1 == list2:
			val = i
			break
	if val == 0:
		print(-1)
	else:
		print(val)


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

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

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	flag = False
	for i in range(1, 2049):
		flag = True
		s = set()
		for j in range(n):
			s.add(a[j] ^ i)
		if set(a) != set(s):
			flag = False
		if flag:
			print(i)
			break
	if not flag:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	st = set(arr)
	upfg = 0
	for i in range(1, 1025):
		flag = 0
		for elem in arr:
			if elem ^ i not in st:
				flag = 1
				break
		if not flag:
			upfg = 1
			print(i)
			break
	if not upfg:
		print(-1)


def main():
	import sys
	sys.setrecursionlimit(10 ** 6)
	input = sys.stdin.readline
	T = int(input())
	for t in range(T):
		n = int(input())
		S = set([int(x) for x in input().strip().split()])
		M = [0] * 10
		for k in range(1, 1 << 10):
			p = []
			for s in S:
				p.append(s ^ k)
			if set(p) == S:
				print(k)
				break
		else:
			print(-1)
main()


for _ in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	m = [[-1 for a in range(0, n)] for b in range(0, n)]
	for i in range(0, n):
		for j in range(0, n):
			m[i][j] = l[i] ^ l[j]
	s = set(m[0])
	for i in range(1, len(m)):
		s = s.intersection(set(m[i]))
	s = list(s)
	s.sort()
	if len(s) > 1:
		print(s[1])
	else:
		print(-1)


t = int(input())

def app(s, pot):
	ns = set()
	for si in s:
		ns.add(si ^ pot)
	return ns
for _ in range(t):
	n = int(input())
	s = {int(x) for x in input().split()}
	for pot in range(1, 1024):
		ns = app(s, pot)
		if ns == s:
			print(pot)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	i = max(s)
	c = 0
	while i > 0:
		c += 1
		i = i >> 1
	k = -1
	for i in range(1, 2 ** c + 1):
		d = set()
		for j in s:
			d.add(j ^ i)
		if d == s:
			k = i
			break
	print(k)


def read_int():
	n = int(input())
	return n

def read_ints():
	ints = [int(x) for x in input().split(' ')]
	return ints

def solve(n, S):
	res = 0
	s = set(S)
	for i in range(1, 1024):
		sn = set()
		for si in s:
			sni = si ^ i
			if sni in s:
				sn.add(sni)
			else:
				break
		if sn == s:
			return i
	return -1
t = read_int()
for case in range(t):
	n = read_int()
	S = read_ints()
	sol = solve(n, S)
	print(sol)


for t in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	occ = [0 for i in range(1024)]
	for i in range(n):
		occ[s[i]] = 1
	which = -1
	for k in range(1, 1024):
		found = True
		for i in range(n):
			if occ[s[i] ^ k] == 0:
				found = False
				break
		if found:
			which = k
			break
	print(which)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
for _ in range(I()[0]):
	(n,) = I()
	a = I()
	st = set(a)
	done = 0
	for i in range(1, 1025):
		xy = set()
		for j in a:
			xy.add(j ^ i)
		if xy == st:
			print(i)
			done = 1
			break
	if done == 0:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	x = input().split()
	seq = []
	d = []
	for i in range(n):
		seq.append(int(x[i]))
		d.append(int(x[i]))
	f = 1
	ans = -1
	for i in range(1, 10000):
		for j in range(n):
			if d[j] ^ i not in seq:
				f = 0
				break
		if f == 1:
			ans = i
			break
		else:
			f = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l.copy()
	l1.sort()
	l2 = []
	f = 0
	for i in range(1, 1025):
		l2 = []
		for j in range(n):
			l2.append(i ^ l[j])
		l2.sort()
		if l1 == l2:
			f = 1
			ans = i
			break
	if f == 1:
		print(i)
	else:
		print(-1)


I = lambda : list(map(int, input().split()))
t = I()[0]
while t:
	t -= 1
	n = I()[0]
	arr = I()
	s = set(arr)
	ans = -1
	for i in range(1, 1025):
		r = set()
		for j in s:
			r.add(i ^ j)
		if s & r == s:
			ans = i
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 1
	for i in range(1, 1024):
		flag = 1
		for j in range(n):
			if arr[j] ^ i not in arr:
				flag = 0
				break
		if flag == 1:
			print(i)
			break
	if flag == 0:
		print(-1)


for i in range(int(input())):
	n = int(input())
	s1 = set([int(i) for i in input().split()])
	m = 2 ** (len(str(bin(max(s1)))) - 2)
	s2 = set()
	k = 1
	while s1 != s2 and k <= m:
		s2 = set()
		for i in s1:
			s2.add(i ^ k)
		k += 1
	if s1 == s2:
		print(k - 1)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def prog():
	for _ in range(int(input().strip())):
		n = int(input().strip())
		nums = [False for i in range(1024)]
		x = list(map(int, input().split()))
		set_x = set(x)
		for i in x:
			nums[i] = True
		val_k = -1
		for k in range(1, 1024):
			new_set = set()
			worked = True
			for a in x:
				new = k ^ a
				if nums[new]:
					new_set.add(new)
				else:
					worked = False
					break
			if worked and new_set == set_x:
				val_k = k
				break
		print(val_k)
prog()


t = int(input())
for loop in range(t):
	n = int(input())
	d = {}
	arr = list(map(int, input().split()))
	arr.sort()
	for i in arr:
		d[i] = 1
	if arr[0] == 0:
		ans = 0
		for i in range(1, n):
			flag = 0
			val = arr[i]
			for j in arr:
				if val ^ j not in d:
					flag = 1
					break
			if flag == 0:
				print(val)
				ans = 1
				break
		if ans == 0:
			print(-1)
	else:
		ans = 0
		for i in range(1, 1025):
			flag = 0
			if i not in d:
				for j in arr:
					if i ^ j not in d:
						flag = 1
						break
			else:
				continue
			if flag == 0:
				print(i)
				ans = 1
				break
		if ans == 0:
			print(-1)


for t in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	if n % 2 != 0:
		print(-1)
	else:
		xorp = []
		for i in range(1, n):
			xorp.append(s[i] ^ s[0])
		ans = []
		for i in range(0, n - 1):
			tmp = []
			for j in range(0, n):
				tmp.append(xorp[i] ^ s[j])
			if set(tmp) == set(s):
				ans.append(xorp[i])
		if len(ans) == 0:
			print(-1)
		else:
			print(min(ans))


import sys
input = sys.stdin.buffer.readline

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

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	n = int(input())
	arr = I()
	s1 = set(arr)
	ans = -1
	for j in range(1, 1025):
		s = set()
		for i in range(n):
			s.add(arr[i] ^ j)
		if s == s1:
			ans = j
			break
	print(ans)


import sys
input = sys.stdin.readline

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

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

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

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

def xor(s, x):
	s2 = list(map(lambda b: b ^ x, s))
	s2.sort()
	if s2 == s:
		return 1
	else:
		return 0
for i in range(t):
	n = inp()
	s = inlist()
	s.sort()
	ans = -1
	for j in range(1, 1024):
		if xor(s, j):
			ans = j
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = True
	i = 1
	while i < 1024 and c:
		j = 0
		while j < len(l) and l[j] ^ i in l:
			j += 1
		if j == len(l):
			c = False
			break
		i += 1
	if not c:
		print(i)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def SOLVE():
	N = int(input())
	L = list(map(int, input().split()))
	L.sort()
	RES = False
	for i in range(1, 1025):
		x = L[:]
		for j in range(N):
			x[j] = x[j] ^ i
		x.sort()
		x.sort()
		if x == L:
			ans = i
			RES = True
			break
	if RES:
		print(ans)
	else:
		print(-1)
TEST = int(input())
for _ in range(TEST):
	SOLVE()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = set(a)
	f = 0
	for i in range(1, 10000):
		s = set()
		for j in a:
			s.add(j ^ i)
		if a == s:
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


def setThing(S):
	greatest = max(S)
	greatest <<= 1
	for i in range(1, greatest):
		if {x ^ i for x in S} == S:
			return i
	return -1
test_num = int(input())
for i in range(test_num):
	trash = input()
	my_set = input().split()
	my_set = {int(x) for x in my_set}
	print(setThing(my_set))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	for i in range(1, 1025):
		res = []
		for x in arr:
			res.append(x ^ i)
		res.sort()
		f = 0
		if res == arr:
			f = 1
			ans = i
			break
	if f == 1:
		print(ans)
	else:
		print(-1)


from collections import deque
from sys import stdin, stdout
import math
input = stdin.readline
vals = [1, 2, 4, 8, 16, 32, 64, 64, 128, 256, 512, 1024, 2048]
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	comp = set(arr)
	for i in range(1, 1025):
		if comp == set([g ^ i for g in arr]):
			print(i)
			break
	else:
		print(-1)


ts = int(input())
results = []
for t in range(ts):
	input()
	bruh = {int(x) for x in input().split()}
	count = 1
	while count <= max(bruh) * 2:
		moment = set()
		for weirdchamp in bruh:
			moment.add(weirdchamp ^ count)
		if bruh == moment:
			break
		else:
			count += 1
	else:
		results.append('-1')
		continue
	results.append(str(count))
	continue
for i in results:
	print(i)


import sys, math, cmath, time
start_time = time.time()

def solve():
	ONLINE_JUDGE_SOLUTION = CodeforcesSolutions.AutoSolve('1362', 'B', 'GET', 'POST')

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

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

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

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

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

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

class CodeforcesSolutions:

	def AutoSolve(contest_id, question_id, get, post):
		n = inp()
		a = inlt()
		q = max(a)
		b = list(sorted(a))
		qw = -1
		for i in range(1, 2 * q + 1):
			for j in range(n):
				a[j] = a[j] ^ i
			if list(sorted(a)) == b:
				qw = i
				break
			a = list(b)
		print(qw)
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def main():
	t = int(input())
	for __ in range(t):
		n = int(input())
		poss_k = []
		a = list(map(int, input().split()))
		for i in range(n):
			temp = set()
			for j in range(n):
				if i != j:
					temp.add(a[i] ^ a[j])
			poss_k.append(temp)
		k = poss_k[0]
		for i in range(1, n):
			k = k & poss_k[i]
		k = list(k)
		if len(k) > 0:
			print(min(k))
		else:
			print(-1)
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, sqrt, trunc, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = set(l())
	r = False
	answer = -1
	for i in range(1, 1025):
		s = set()
		for j in arr:
			s.add(j ^ i)
		if s == arr:
			r = True
			answer = i
			break
		if r:
			break
	out(answer)


for i1 in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	lim = max(l)
	flag = 0
	for i in range(1, 1025):
		k = []
		for j in l:
			ans = j ^ i
			k.append(ans)
		k.sort()
		if k == l:
			flag = 1
			ans = i
			break
	if flag == 1:
		print(ans)
	else:
		print(-1)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]
for _ in range(val()):
	n = val()
	l = sorted(li())
	l = set(l)
	ans = -1
	for i in range(1, 1030):
		s = set(l)
		for j in l:
			s.add(j ^ i)
		if s == l:
			ans = i
			break
	print(ans)


bstr = lambda number: bin(number)[2:]

def solve(s):
	limit = 2 ** len(bstr(max(s))) - 1
	for i in range(1, limit + 1):
		if sorted([i ^ e for e in s]) == s:
			return i
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	s = (*map(int, input().split()),)
	print(solve(sorted(s)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n % 2 != 0:
		print(-1)
	else:
		l.sort()
		l1 = []
		f = 0
		for i in range(1, 1025):
			for j in l:
				l1.append(i ^ j)
			l1.sort()
			if l == l1:
				f = 1
				print(i)
				break
			else:
				l1.clear()
		if f == 0:
			print(-1)


tc = int(input())
for q in range(tc):
	len = int(input())
	nums = [int(x) for x in input().split()]
	nums.sort()
	found = False
	for i in range(1, 1024):
		xor = [x ^ i for x in nums]
		xor.sort()
		if xor == nums:
			print(i)
			found = True
			break
	if not found:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n & 1:
		print(-1)
		continue
	a.sort()

	def f(k):
		return sorted([x ^ k for x in a]) == a
	for k in range(1, 2048):
		if f(k):
			print(k)
			break
	else:
		print(-1)


t = int(input())
for k in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	for x in range(1, 1025):
		if set((x ^ q for q in a)) == a:
			print(x)
			break
	else:
		print(-1)


from math import log, ceil, pow
for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	if n % 2 == 1:
		print(-1)
		continue
	for i in range(1, 1024):
		if sorted([i ^ j for j in a]) == a:
			print(i)
			break
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	main = set(lst)
	new = []
	a = lst[0]
	for i in lst:
		new.append(a ^ i)
	done = False
	for i in new:
		if i != 0:
			temp = set([])
			for j in lst:
				k = j ^ i
				temp.add(k)
			if temp == main:
				done = True
				print(i)
				break
	if not done:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	li = sorted(list(map(int, input().split())))
	f = 0
	for i in range(1, 1024):
		li2 = []
		for j in li:
			li2.append(j ^ i)
		if li == sorted(li2):
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	L.sort()
	pos = L[0]
	j = 1
	M = []
	for j in range(1, n):
		val = L[j] ^ pos
		s = set()
		for i in range(n):
			if L[i] ^ val in L:
				s.add(L[i])
			else:
				break
		if len(s) == len(L):
			M.append(val)
	if M == []:
		print(-1)
	else:
		print(min(M))


import sys
import math

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def printspx(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')

def printchk(*args):
	return print(*args, end=' \\ ')
MODPRIME = int(1000000000.0 + 7)
BABYMODPR = 998244353
for _testcases_ in range(int(input())):
	n = int(input())
	li = get_list()
	flag = False
	se = set(li)
	for i in range(1, 1024):
		se2 = set()
		for j in li:
			se2.add(j ^ i)
		if se2 == se:
			print(i)
			flag = True
			break
	if not flag:
		print(-1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	check = 1
	maxia = max(a)
	visited = [0] * (maxia + 1)
	for i in a:
		visited[i] += 1
	ans = 0
	while 1:
		counted = visited[:]
		temp = 0
		for i in a:
			if i ^ check <= maxia and counted[i ^ check] == 1:
				counted[i ^ check] = 0
			else:
				temp = 1
				break
		if temp == 0:
			ans = 1
			break
		if check > 1035:
			break
		check += 1
	if ans == 1:
		print(check)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	for k in range(1, 1024):
		cur = set()
		S = set(s)
		for x in s:
			if x ^ k in S:
				cur.add(x ^ k)
			else:
				break
		if cur == S:
			print(k)
			break
	else:
		print(-1)


for i in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = []
	p = -1
	for j in range(1, 1025):
		m = set()
		for i in l:
			m.add(i ^ j)
		if m == set(l):
			p = j
			break
	print(p)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a.copy()
	b.sort()
	ans = -1
	for i in range(1, 1025):
		x = []
		for j in range(n):
			x.append(i ^ a[j])
		x.sort()
		if x == b:
			ans = i
			break
	print(ans)


import sys
import math
from collections import defaultdict, deque
import heapq
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = set(list(map(int, sys.stdin.readline().split())))
	ans = -1
	for i in range(1, 1025):
		b = set()
		for j in arr:
			b.add(i ^ j)
		if arr.intersection(b) == arr:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = [0] * n
	e = 0
	for i in range(1, 1024, 1):
		for x in range(n):
			k[x] = l[x] ^ i
		if sorted(k) == sorted(l):
			e = 1
			print(i)
			break
	if e == 0:
		print(-1)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		s = set(a)
		ans = 10 ** 9
		for x in a:
			k = a[0] ^ x
			if k == 0:
				continue
			new_s = set()
			for y in a:
				new_s.add(y ^ k)
			if s == new_s:
				ans = min(ans, k)
		if ans == 10 ** 9:
			ans = -1
		print(ans)
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	k = 1
	hashA = {i: None for i in arr}
	tmp = {}
	while True:
		tmp = {}
		if k == 1024:
			break
		for (i, j) in hashA.items():
			val = i ^ k
			if val in tmp:
				continue
			else:
				tmp[val] = None
		if hashA == tmp:
			break
		k += 1
	ans = k if k != 1024 else -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = 1
	ans = -1
	dic = {}
	for i in range(1, 1024):
		f = 1
		dic.clear()
		for item in a:
			dic[item ^ i] = 1
		for item in a:
			if dic.get(item, 0) != 1:
				f = 0
				break
		if f == 1:
			ans = i
			break
	if f:
		print(ans)
	else:
		print(-1)


from collections import defaultdict
T = int(input())

def solve(S):
	N = len(S)
	cnt = defaultdict(int)
	for i in range(N - 1):
		for j in range(i + 1, N):
			cnt[S[i] ^ S[j]] += 1
	cands = []
	for x in cnt:
		if cnt[x] >= N // 2:
			cands.append(x)
	cands.sort()
	S_set = set(S)
	for cand in cands:
		for x in S:
			if x ^ cand not in S_set:
				break
		else:
			print(cand)
			break
	else:
		print(-1)
for _ in range(T):
	N = int(input())
	S = [int(x) for x in input().split()]
	solve(S)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	S = set(map(int, input().split()))
	ok = False
	for i in range(1, 1024):
		tmp = {i ^ val for val in S}
		if tmp == S:
			print(i)
			ok = True
			break
	if not ok:
		print(-1)


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	poss = set()
	for j in range(1, n):
		k = arr[0] ^ arr[j]
		poss.add(k)
	for i in range(1, n):
		temp = set()
		for j in range(n):
			k = arr[i] ^ arr[j]
			if k in poss:
				temp.add(k)
		poss = temp.copy()
	if len(poss) == 0:
		res = -1
	else:
		res = 1025
		for x in poss:
			res = min(res, x)
	print(res)
	T -= 1


def checker(n, lst):
	if n % 2 != 0:
		return -1
	else:
		result = [lst[i] ^ lst[0] for i in range(1, n)]
		answer = list()
		for i in range(n - 1):
			tmp = [result[i] ^ lst[j] for j in range(n)]
			if set(tmp) == set(lst):
				answer.append(result[i])
		return -1 if not answer else min(answer)
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(checker(m, a))


def int_array():
	return list(map(int, input().strip().split()))
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	aset = set(a)
	f = 0
	for k in range(1, 1024 + 1):
		mset = set()
		for i in a:
			mset.add(i ^ k)
		if len(mset) == n and len(aset.difference(mset)) == 0:
			f = 1
			print(k)
			break
	if f:
		continue
	else:
		print(-1)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	fg = 0
	a = sorted(a)
	if n == 1:
		print('-1')
	else:
		for i in range(1, a[-1] * 2 + 1):
			l = []
			for j in range(n):
				l.append(a[j] ^ i)
			l = sorted(l)
			if l == a:
				print(i)
				fg = 1
				break
		if fg == 0:
			print('-1')


import math
t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = list(map(int, input().split()))
	d1 = {}
	for w in l:
		if w not in d1:
			d1[w] = 1
		else:
			d1[w] += 1
	m = max(l)
	m = len(bin(m).replace('0b', ''))
	f = 0
	for k in range(1, 2 ** m + 1):
		d2 = {}
		flag = 0
		for i in l:
			if i not in d2:
				d2[i] = 0
		for w in l:
			if k ^ w in d2:
				d2[k ^ w] += 1
			else:
				break
		for w in d1:
			if d1[w] != d2[w]:
				flag = 1
		if flag == 0:
			print(k)
			f = 1
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	input()
	x = set([int(i) for i in input().split()])
	for j in range(1, 1025):
		s = set([int(i ^ j) for i in x])
		if x == s:
			flag = 1
			print(j)
			break
	else:
		print(-1)


import sys

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

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

def Num():
	return int(input())

def solve():
	n = Num()
	a = List()
	s = set(a)
	for i in range(1, 1024):
		temp = set()
		for j in a:
			temp.add(j ^ i)
		if temp == s:
			return i
	return -1
for _ in range(Num()):
	print(solve())


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	ans = 0
	for i in range(1, 1025):
		p = []
		for j in s:
			p.append(j ^ i)
		p.sort()
		if p == s:
			ans = i
			break
	if ans == 0:
		print(-1)
	else:
		print(ans)


t = int(input())

def getBin(n):
	return bin(n)[2:]
while t > 0:
	n = input()
	arr = [int(x) for x in input().split()]
	d = {}
	for num in arr:
		d[num] = d.get(num, 0) + 1
	ans = -1
	for i in range(1, 1025):
		dt = {}
		for num in arr:
			dt[num ^ i] = dt.get(num ^ i, 0) + 1
		if d == dt:
			ans = i
			break
	print(ans)
	t -= 1


import sys
from typing import List
input = sys.stdin.readline
import math

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

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

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

def from_file(f):
	return f.readline

def solution(A: List[int]):
	S = set(A)

	def test(k):
		for a in A:
			if a ^ k not in S:
				return False
		return True
	for k in range(1, 1025):
		if test(k):
			return k
	return -1
t = inp()
for _ in range(t):
	n = inp()
	A = inlt()
	print(solution(A))


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def bo(i):
	return ord(i) - ord('a')
from copy import *
from bisect import *
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	p = max(a)
	flag = -1
	for i in range(1, 2 ** 11 + 1):
		d = []
		for j in range(n):
			d.append(a[j] ^ i)
		if sorted(a) == sorted(d):
			flag = i
			break
	print(flag)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

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

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		arr = RLL()
		arr.sort()
		rec = [0] * 1025
		tres = []
		for i in range(n):
			rec[arr[i]] = 1
			if i > 0:
				tres.append(arr[i] ^ arr[0])
		if n % 2 == 1:
			print(-1)
		else:

			def c(num):
				for i in range(n):
					if rec[arr[i] ^ num] == 0:
						return False
				return True
			for i in range(len(tres)):
				if c(tres[i]):
					print(tres[i])
					break
			else:
				print(-1)
main()


import math
t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	aval = [0 for i in range(5000)]
	for i in range(n):
		aval[arr[i]] = 1
	ans = 10 ** 7
	for i in range(1, 2049):
		flag = True
		for j in range(n):
			temp = arr[j] ^ i
			if aval[temp] == 0:
				flag = False
		if flag:
			ans = min(ans, i)
			break
	if ans == 10 ** 7:
		print(-1)
	else:
		print(ans)


def solve(s):
	k = 0
	mx = 0
	mn = 1024
	for v in s:
		k = k ^ v
		mx = max(v, mx)
		mn = min(v, mn)
	if k == 0:
		return 1 if mn == 0 and mx == len(s) - 1 else -1
	t = set([k ^ v for v in s])
	return k if len(s ^ t) == 0 else -1

def solve2(s):
	for k in range(1, 1025):
		t = set([k ^ v for v in s]) ^ s
		if len(t) == 0:
			return k
	return -1
tc = int(input())
while tc > 0:
	n = int(input())
	s = set(map(int, input().split()))
	print(solve2(s))
	tc -= 1


for _ in range(int(input())):
	n = int(input())
	a = []
	a = list(map(int, input().split()))
	k = 0
	for i in range(1, 1025):
		l = []
		for j in a:
			if j ^ i in a:
				l.append(j ^ i)
			else:
				break
		if len(a) == len(l):
			if sorted(a) == sorted(l):
				k = i
				break
	if k != 0:
		print(k)
	else:
		print(-1)


from math import sqrt
t = int(input())
for q in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	flagflag = False
	for k in range(1, 1025):
		new_arr = []
		for i in range(0, n):
			new_arr.append(arr[i] ^ k)
		new_arr.sort()
		flag = True
		for i in range(0, n):
			if arr[i] != new_arr[i]:
				flag = False
				break
		if flag:
			flagflag = True
			print(k)
			break
	if not flagflag:
		print(-1)


from operator import xor
ans = []
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	l.sort()
	for p in range(1, 1025):
		l1 = []
		for x in l:
			l1.append(xor(x, p))
		l1.sort()
		if l == l1:
			ans.append(p)
			break
		if p == 1024:
			ans.append(-1)
for a in ans:
	print(a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	m = a[-1]
	i = 1
	while i <= m:
		i *= 2
	for j in range(1, i + 1):
		b = list(map(lambda x: x ^ j, a))
		b.sort()
		if a == b:
			print(j)
			break
	else:
		print(-1)


from sys import stdin
from math import ceil

def func():
	return
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = sorted(list(map(int, stdin.readline().split())))
	cond = False
	for i in range(1, 1025):
		m = []
		for ele in arr:
			m.append(ele ^ i)
		if sorted(m) == arr:
			cond = True
			break
	if cond == True:
		print(i)
	else:
		print(-1)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = list(map(int, input().split()))
	m = max(l)
	flag = 0
	s = set(l)
	z = len(bin(m)) - 2 - 1
	no = 2 ** z
	for i in range(z):
		no += 2 ** i
	for i in range(1, no + 1):
		p = [j ^ i for j in l]
		if set(p) == s:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


def f(q):
	cnt = 0
	while q % 2 == 0:
		cnt += 1
		q //= 2
	if q > 1:
		return -1
	res = 0
	res += cnt // 3
	cnt %= 3
	res += cnt // 2
	cnt %= 2
	res += cnt
	return res

def solve():
	n = int(input())
	L = list(map(int, input().split()))
	L.sort()
	for i in range(1, 1024):
		T = []
		for l in L:
			T.append(i ^ l)
		T.sort()
		if T == L:
			print(i)
			return
	print(-1)
for cnt in range(int(input())):
	solve()


for T in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	arr.sort()
	res = 1000000000.0
	for i in range(n):
		d[arr[i]] = 1
	for i in range(1, n):
		k = arr[0] ^ arr[i]
		count = 2
		for i in range(1, n):
			if d.get(arr[i] ^ k, 0):
				count += 2
			else:
				break
		if count == 2 * n:
			res = k
			break
	if res == 1000000000.0:
		print(-1)
	else:
		print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	for dig in range(1, 1024):
		find = True
		patern = s.copy()
		for char in s:
			res = char ^ dig
			if res in patern:
				patern.remove(res)
			else:
				find = False
				break
		if find:
			break
	if find:
		print(dig)
	else:
		print(-1)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
for _ in range(ii()):
	n = ii()
	a = il()
	c = 0
	for i in range(1, 1025):
		b = []
		if c >= 1:
			break
		for j in range(0, n):
			b.append(i ^ a[j])
		if set(b) == set(a):
			print(i)
			c = c + 1
			break
	if c == 0:
		print(-1)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	A = set(map(int, input().split()))
	upper = 1024
	ans = -1
	for i in range(1, upper + 1):
		if ans != -1:
			break
		B = set()
		for a in A:
			x = a ^ i
			if x not in A:
				break
			B.add(x)
		if len(B) == n:
			ans = i
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	a = []
	ans = 0
	flag = 0
	s.sort()
	for k in range(1, 1025):
		for j in range(0, n):
			a.append(s[j] ^ k)
		a.sort()
		if a == s:
			ans += k
			flag = 1
			break
		else:
			a = []
	if flag == 1:
		print(ans)
	else:
		print(-1)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	s = list(map(int, stdin.readline().split()))
	dd = {}
	ans = -1
	for i in range(n):
		dd[s[i]] = True
	for a in range(1, 1024, 1):
		d = {}
		rs = True
		for i in range(n):
			d[s[i] ^ a] = True
		for k in dd:
			if k not in d:
				rs = False
				break
		if rs:
			ans = a
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for elem in a:
		d[elem] = 1
	ans = -1
	for k in range(1, 1024):
		flag = 1
		for elem in a:
			if d.get(elem ^ k, 0) == 0:
				flag = 0
				break
		if flag == 1:
			ans = k
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ans = -1
	word = list(map(int, input().split()))
	se = set(word)
	for j in range(1, 1024):
		s = set()
		for k in range(n):
			s.add(word[k] ^ j)
		if se == s:
			ans = j
			break
	print(ans)


from functools import reduce
import operator

def solve(li):
	z = set(li)
	for i in range(1, 1025):
		newset = set((x ^ i for x in li))
		if newset == z:
			return i
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().strip().split()))
	print(solve(li))


t = int(input())
for tcases in range(t):
	n = int(input())
	flag = 1
	s = set(map(int, input().split()))
	for k in range(1, 1024):
		r = {ki ^ k for ki in s}
		if s.difference(r) == set():
			print(k)
			flag = 0
			break
	if flag == 1:
		print(-1)


def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	afterXOR = {}
	for i in range(1, 1025):
		afterXOR = {}
		flag = 0
		for j in arr:
			ans = i ^ j
			if ans not in afterXOR:
				afterXOR[ans] = 1
		for val in range(n):
			if arr[val] not in afterXOR:
				flag = 1
				break
		if flag == 0:
			return i
	return -1
t = int(input())
while t:
	print(solve())
	t -= 1


import collections
import os
import sys
from io import BytesIO, IOBase
import math

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

def inputArray():
	return input().rstrip().split()

def inputVars():
	return map(int, input().rstrip().split())

def inputNum():
	return int(input())

def main():
	for _ in range(inputNum()):
		n = inputNum()
		arr = inputIntArray()
		arr.sort()
		hashMap = collections.defaultdict(int)
		for num in arr:
			hashMap[num] += 1
		maxVal = max(arr)
		res = 0
		for num in range(1, 1024):
			temp = num ^ maxVal
			check = True
			if temp in hashMap:
				for num2 in arr[:-1]:
					if num2 ^ num in hashMap:
						continue
					else:
						check = False
						break
				if check:
					res = num
					break
		if res:
			print(res)
		else:
			print(-1)
main()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	t = s[:]
	ans = -1
	for i in range(1, 1025):
		for j in range(len(s)):
			t[j] = s[j] ^ i
		t.sort()
		if s == t:
			ans = i
			break
	print(ans)


def hobby(n, array):
	s = set(array)
	for x in range(2047):
		f = 0
		for y in array:
			if x + 1 ^ y in s:
				continue
			else:
				f = 1
				break
		if f == 0:
			return x + 1
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(hobby(n, l))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def solve():
	for _ in range(int(input())):
		n = int(input())
		arr = read()
		sett = set(arr)
		flag = 0
		for i in range(1, 1025):
			tem = [0] * n
			for j in range(n):
				tem[j] = arr[j] ^ i
			if not sett.difference(set(tem)):
				print(i)
				flag = 1
				break
		if not flag:
			print(-1)
solve()


t = int(input())
results = []
for case in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	k = 1
	while k <= 1024:
		new = set((k ^ i for i in a.copy()))
		if new == a:
			break
		k += 1
	if k == 1025:
		k = -1
	results.append(k)
for result in results:
	print(result)


t = int(input())
for _ in range(t):
	n = int(input())
	S = list(map(int, input().split()))
	T = set(S)
	S.sort()
	if S[-1] == 0:
		print(-1)
		continue
	for k in range(1, 1024):
		temp = set()
		for s in S:
			temp.add(s ^ k)
		if temp == T:
			break
	else:
		print(-1)
		continue
	print(k)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		for j in range(n):
			if i != j:
				z = a[i] ^ a[j]
				if z not in d:
					d[z] = 1
				else:
					d[z] += 1
	f = 0
	fin = 1026
	for i in d.keys():
		if d[i] >= n and i > 0:
			for j in a:
				if j ^ i not in a:
					f = 1
					break
			if f == 0:
				fin = min(fin, i)
			f = 0
	if fin == 1026:
		print(-1)
	else:
		print(fin)


t = int(input())
for i in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	ans = [0] * 1024
	for i in range(n):
		for j in range(n):
			if s[i] != s[j]:
				ans[s[i] ^ s[j]] += 1
	for i in range(len(ans)):
		if ans[i] == n:
			print(i)
			break
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().rstrip().split()]
	st1 = set(arr)
	flag = 0
	for i in range(1, 1025):
		s2 = set((i ^ j for j in st1))
		if s2 == st1:
			flag = 1
			print(i)
			break
	if flag != 1:
		print(-1)


t = int(input())
while t != 0:
	k = int(input())
	num = list(map(int, input().split()))
	fa = -1
	for i in range(1, 1025):
		ans = list(map(lambda j: j ^ i, num))
		if set(ans) == set(num):
			fa = i
			break
	print(fa)
	t -= 1


def f(arr):
	a = max(arr)
	arr = set(arr)
	for i in range(1, 2048):
		new_set = set()
		for x in arr:
			new_set.add(x ^ i)
		if new_set == arr:
			print(i)
			return
	print('-1')
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(a) for a in input().split(' ')]
	f(arr)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	d = defaultdict(int)
	dcopy = defaultdict(int)
	for ele in x:
		d[ele] = True
		dcopy[ele] = True
	flag = 0
	for i in range(1, 1025):
		for ele in x:
			if ele ^ i in d:
				d.pop(ele ^ i)
			else:
				break
		if len(d) == 0:
			flag = 1
			break
		d = dcopy.copy()
	if flag == 1:
		print(i)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	s = set()
	for i in a:
		s.add(i)
	flag = 0
	for i in range(1, 1025):
		c = 0
		for j in range(0, n):
			x = i ^ a[j]
			if x in s:
				c += 1
		if c == n:
			flag = 1
			print(i)
			break
	if flag == 0:
		print('-1')


tests = int(input())
for _ in range(tests):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(1, 1025):
		d = []
		t = 1
		for j in range(n):
			d.append(arr[j] ^ i)
		if set(d) == set(arr):
			t = 0
			break
	if t == 0:
		print(i)
	else:
		print(-1)


for test in range(int(input())):
	n = int(input())
	nums = set(map(int, input().split()))
	res = set()
	flag = False
	for k in range(1, 1024):
		res.clear()
		for e in nums:
			n = e ^ k
			res.add(n)
		set_difference = res - nums
		if not set_difference:
			print(k)
			flag = True
			break
	if flag == False:
		print('-1')


t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	flag = 0
	ans = -1
	for j in range(1, 2 * max(l) + 1):
		arr = []
		for k in range(n):
			arr.append(j ^ l[k])
		arr.sort()
		if arr == l:
			ans = j
			break
	print(ans)


from sys import *
input = stdin.readline
for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	f = 0
	for i in range(1, 1025):
		d = []
		for j in range(n):
			s = i ^ l[j]
			d.append(s)
		if sorted(d) == l:
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	found_flag = False
	for k in range(1, 1025):
		for i in A:
			if i ^ k not in A:
				break
		else:
			print(k)
			found_flag = True
			break
	if not found_flag:
		print(-1)


import math
t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	b = list()
	flag = False
	if n % 2 == 1:
		print(-1)
		continue
	y = max(a)
	a.sort()
	count = 0
	while y // 2 > 0:
		count += 1
		y = y // 2
	an = 2 ** (count + 1) - 1
	for i in range(1, an + 1):
		b = []
		for j in range(n):
			z = a[j] ^ i
			b.append(z)
		b.sort()
		c = 0
		for j in range(n):
			if a[j] == b[j]:
				c += 1
			else:
				break
		if c == n:
			flag = True
			break
	if flag == True:
		print(i)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d1 = {}
	for i in range(n):
		if d1.get(l[i]) == None:
			d1[l[i]] = 1
		else:
			d1[l[i]] += 1
	x = 0
	for k in range(1, 1025):
		d = {}
		for i in range(n):
			a = k ^ l[i]
			if d.get(a) == None:
				d[a] = 1
			else:
				d[a] += 1
		if d == d1:
			x = 1
			print(k)
			break
	if x == 0:
		print(-1)


z = int(input())
for h in range(z):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	x = max(a)
	flag = 0
	for i in range(1, 2 * x):
		m = []
		for j in range(n):
			m.append(a[j] ^ i)
		m.sort()
		if m == a:
			flag = 1
			break
	if flag == 1:
		print(i)
	else:
		print(-1)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	f = 0
	for i in range(1, 2 * max(a) + 1):
		e = Counter()
		for j in range(n):
			p = a[j] ^ i
			e.update({p: 1})
		if e == d:
			f = 1
			break
	if f == 1:
		print(i)
	else:
		print(-1)


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

def print_list(l):
	print(' '.join(map(str, l)))
N = int(input())
for _ in range(N):
	n = int(input())
	s = read_list()
	s = set(s)
	ans = -1
	for k in range(1, 1025):
		flag = True
		for x in s:
			if x ^ k not in s:
				flag = False
				break
		if flag:
			ans = k
			break
	print(ans)


def isSame(arr):
	s = set(arr)
	for i in range(1, 1025):
		p = set()
		for j in range(len(arr)):
			p.add(i ^ arr[j])
		if s == p:
			return i
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = isSame(arr)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	k = set()
	f = 0
	for i in range(1, 1025):
		for j in s:
			x = i ^ j
			k.add(x)
		if s == k:
			f = 1
			break
		k = set()
	if f == 0:
		print(-1)
	else:
		print(i)


for _ in [0] * int(input()):
	sz = int(input())
	arr = [int(i) for i in input().split()]
	total = []
	for x in arr:
		indiv = []
		for y in arr:
			if x != y:
				indiv.append(x ^ y)
		total.append(indiv)
	ans = set()
	[ans.add(a) for a in total[0]]
	for var in total[1:]:
		ans = ans.intersection(set(var))
	if len(ans) != 0:
		print(min(ans))
	else:
		print(-1)


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	s = list(map(int, sys.stdin.readline().rstrip().split()))
	mx = max(s)
	re = -1
	for k in range(1, 2049):
		t = []
		for i in s:
			chk = i ^ k
			if chk not in t and chk in s:
				t.append(chk)
			else:
				break
		if len(t) == n:
			re = k
			break
	print(re)


t = int(input())
while t:
	n = int(input())
	d = {}
	b = [0] * 100000
	a = list(map(int, input().split()))
	m = 0
	for i in range(n):
		m = max(m, a[i])
		b[a[i]] = 1
	k = 1
	while k <= 2500:
		f = 0
		for i in range(n):
			d = a[i] ^ k
			if b[d] != 1:
				f = 1
				break
		if f == 1:
			k += 1
		else:
			f = 0
			print(k)
			break
	if f == 1:
		print(-1)
	t -= 1


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	i = 1
	p = [1, 2, 3]
	o = [2, 1, 3]
	o.sort()
	l.sort()
	while i > 0:
		p = []
		for j in l:
			p.append(i ^ j)
		p.sort()
		if p == l:
			break
		else:
			i += 1
		if i > 1024:
			break
	if i > 1024:
		print(-1)
	else:
		print(i)


mxbit = 12
inf = 1 << 31

def solve():
	n = int(input())
	s = {int(v) for v in input().split()}
	bitpos = [0] * mxbit
	for i in range(mxbit):
		for v in s:
			if v & 1 << i:
				bitpos[i] += 1
	for k in range(1, 1024):
		goodk = True
		for i in range(mxbit):
			if 1 << i & k and (bitpos[i] == n or bitpos[i] == 0):
				goodk = False
		if goodk:
			for v in s:
				if v ^ k not in s:
					goodk = False
		if goodk:
			print(k)
			return
	print(-1)
t = int(input())
for _ in range(t):
	solve()


for p in range(int(input())):
	n = int(input())
	arr = input().split()
	arr = map(int, arr)
	arr = list(arr)
	s = set()
	r = set()
	for d in arr:
		s.add(d)
	ans = -10
	for i in range(1, 1024):
		for d in arr:
			r.add(d ^ i)
		if s == r:
			ans = i
			break
		r = set()
	if ans == -10:
		print(-1)
	else:
		print(ans)


import math
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	if m == 0:
		print(-1)
		continue
	c = set(a)
	ans = -1
	for i in range(1, 2 ** len(bin(m)[2:])):
		s = {}
		flag = 0
		for j in a:
			if j ^ i not in c:
				flag = 1
				break
			s[j ^ i] = 1
		if not flag and len(s) == n:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	v = {}
	for i in a:
		v[i] = True
	flag = False
	for k in range(1, 1 + 1024):
		for i in a:
			b = i ^ k
			if v.get(b) == None:
				flag = True
				break
		if flag == False:
			print(k)
			break
		flag = False
	else:
		print(-1)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	seta = set(arr)
	flag = False
	for i in range(1, 1025):
		temp = set()
		for j in arr:
			temp.add(j ^ i)
		if temp == seta:
			print(i)
			flag = True
			break
	else:
		print(-1)


t = int(input())
for k in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	ans = False
	for res in range(1, 1025):
		count = 0
		for elem in a:
			if res ^ elem in a:
				count += 1
			else:
				break
		if count == len(a):
			print(res)
			ans = True
			break
	if not ans:
		print(-1)


from io import BytesIO, IOBase
import sys
import os
inf = float('inf')

def Iint():
	return int(input())

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

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

def Plist(li, s=' '):
	print(s.join(map(str, li)))

def bin_(num, length=-1):
	s = bin(num)[2:]
	if length == -1:
		return s
	return s.zfill(length)

def check(no, s, n):
	c = 0
	for si in s:
		if si ^ no in s:
			c += 1
		else:
			return 0
	return 1

def give(bi):
	li = []
	for (i, bii) in enumerate(bi[::-1]):
		if bii == 0:
			continue
		else:
			li.append(1 << i)
	li = li[::-1]
	l = len(li)
	for no in range(1, 1 << l):
		b = bin_(no, l)
		ans = 0
		for i in range(l):
			ans += li[i] * (1 if b[i] == '1' else 0)
		yield ans

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		s = Ilist()
		ss = set(s)
		if n % 2 == 1:
			print(-1)
			continue
		s = [bin_(si, 11) for si in s]
		ans = 0
		flag = [0] * 11
		for i in range(11):
			(n0, n1) = (0, 0)
			for si in s:
				if si[10 - i] == '0':
					n0 += 1
				elif si[10 - i] == '1':
					n1 += 1
			if not (n0 % 2 == 0 and n1 % 2 == 0 or n1 == n0):
				print(-1)
				break
			elif n0 == n1:
				flag[10 - i] = 1
		else:
			for g in give(flag):
				if g == 0:
					continue
				if check(g, ss, n):
					print(g)
					break
			else:
				print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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


ans = []
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	s = set(u)
	for k in range(1, 1025):
		w = set()
		for i in range(n):
			w.add(u[i] ^ k)
		if s == w:
			ans.append(k)
			break
	else:
		ans.append(-1)
print('\n'.join(map(str, ans)))


from atexit import register
from io import StringIO
import sys
_IB = sys.stdin.read().splitlines()
input = iter(_IB).__next__
_OB = StringIO()
sys.stdout = _OB

@register
def write():
	sys.__stdout__.write(_OB.getvalue())

def Iint():
	return int(input())

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

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

def Plist(li, s=' '):
	print(s.join(map(str, li)))

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		s = set(Imap())
		ans = -1
		for k in range(1, 1024):
			for x in s:
				if k ^ x not in s:
					break
			else:
				ans = k
				break
		print(ans)
main()


import sys
import math as mt
input = sys.stdin.buffer.readline
t = int(input())
for __ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	xori = 0
	d = {}
	for i in l:
		d[i] = 1
	ans = -1
	for i in range(1, 1024):
		d1 = {}
		for j in range(n):
			d1[i ^ l[j]] = 1
		ch = 1
		for j in d:
			if d1.get(j, -1) == -1:
				ch = 0
				break
		if ch == 1:
			ans = i
			break
	print(ans)


def find(l, d, n):
	for i in l:
		k = i ^ n
		if k not in d:
			return 0
	return 1
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	f = 0
	for i in l:
		d[i] = 1
	for i in range(1, 1024):
		if find(l, d, i):
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


for t in range(int(input())):
	n = int(input())
	S = list(map(int, input().split()))
	S.sort()
	ans = -1
	for k in range(1, 1024 + 1):
		newSet = set(S)
		for s in S:
			x = k ^ s
			if x not in newSet:
				break
			else:
				newSet.remove(x)
		if len(newSet) == 0:
			ans = k
			break
	print(ans)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def preorder_postorder(root, paths, parent, left, level):
	l = 0
	queue = [root]
	while queue != []:
		node = queue[-1]
		flag = 0
		for i in paths[node]:
			if i != parent[node] and level[i] == sys.maxsize:
				parent[i] = node
				level[i] = level[node] + 1
				flag = 1
				queue.append(i)
		if flag == 0:
			left[parent[node]] += 1 + left[node]
			queue.pop()
	return [parent, left, level]

def nCr(n, r, p):
	if r == 0:
		return 1
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

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

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

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

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

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

def make_tree(n):
	root = 1
	paths2 = [[] for i in range(n + 1)]
	parent = [0 for i in range(n + 1)]
	for i in range(n - 1):
		[p, child] = inlt()
		paths2[p].append(child)
		paths2[child].append(p)
	path = [[] for i in range(n + 1)]
	root = 1
	queue = [root]
	parent[1] = -1
	while queue != []:
		curr = queue.pop(0)
		for i in paths2[curr]:
			if parent[i] == 0:
				parent[i] = curr
				queue.append(i)
				path[curr].append(i)
	parent[1] = 0
	return [path, parent]

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	for test in range(tests):
		n = inp()
		a = inlt()
		hashm = {}
		for i in a:
			hashm[i] = 1
		ans = 0
		for i in range(1, 1025):
			flag = 0
			for j in a:
				if j ^ i not in hashm:
					flag = 1
					break
			if flag == 0:
				ans = i
				break
		if ans == 0:
			print(-1)
		else:
			print(ans)
main()


strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	n = int(input())
	arr = intInp()
	arrSet = set(arr)
	ans = -1
	for i in range(1, 1024 * 2):
		b = set([])
		for j in arr:
			if i ^ j in arrSet:
				b.add(i ^ j)
			else:
				break
		if len(b) == n:
			ans = i
			break
	print(ans)


def check(k, available, arr):
	for i in arr:
		if i ^ k >= 1025:
			return False
		if available[i ^ k] == False:
			return False
	return True

def fun(arr):
	N = 1025
	available = [False] * N
	for i in range(len(arr)):
		available[arr[i]] = True
	for i in range(1, N):
		k = i
		if check(k, available, arr):
			return k
	return -1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(fun(arr))


test = int(input())
while test > 0:
	test -= 1
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	rs = -1
	for i in range(1, 1025):
		b = []
		for j in a:
			b.append(j ^ i)
		b.sort()
		check = True
		for j in range(n):
			if a[j] != b[j]:
				check = False
				break
		if check == True:
			rs = i
			break
	print(rs)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	p = list(map(int, input().split(' ')))
	p.sort()
	ans = -1
	for i in range(1, 1024):
		np = []
		for x in p:
			np.append(x ^ i)
		np.sort()
		if np == p:
			ans = i
			break
	print(ans)


import sys
input = sys.stdin.readline
a = int(input())
from collections import defaultdict
for i in range(a):
	m = int(input())
	al = defaultdict(int)
	z = list(map(int, input().split()))
	for i in range(len(z)):
		al[z[i]] += 1
	rem = z.copy()
	rem = list(set(z))
	flag = 0
	glag = 1
	ans = 0
	tra = 0
	for j in range(1, 1025):
		dl = defaultdict(int)
		glag = 0
		for i in range(len(z)):
			if al[z[i] ^ j] == 0:
				glag = 1
				break
			else:
				dl[z[i] ^ j] += 1
		if glag == 0:
			flag = 0
			for i in range(len(rem)):
				if al[rem[i]] != dl[rem[i]]:
					flag = 1
					break
			if flag == 0:
				print(j)
				tra = 1
				break
	if tra == 0:
		print(-1)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1024):
		for j in range(n):
			r = a[j] ^ i
			if r in a:
				pass
			else:
				break
		else:
			ans = i
			break
	print(ans)


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

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

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

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def Solution(arr, n):
	for i in range(1, 1025):
		hm = {}
		for v in arr:
			hm[v] = True
		counter = 0
		for v in arr:
			t = v ^ i
			if t in hm:
				hm[t] = False
				counter += 1
			else:
				break
		if counter == n:
			print(i)
			return
	print(-1)

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = get_ints_in_list()
		Solution(arr, n)
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(s) for s in input().split()]
	a.sort()
	x = 1
	f = 0
	while x <= 1024:
		a1 = [s ^ x for s in a]
		a1.sort()
		if a == a1:
			f = 1
			break
		x += 1
	if f:
		print(x)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	hval = l[-1]
	flag = 0
	for i in range(1, 1025):
		l2 = []
		for j in l:
			l2.append(i ^ j)
		if l == sorted(l2):
			flag = 1
			print(i)
			break
	if flag == 0:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	st = list(map(int, input().split()))
	ans = -1
	for i in range(1, 1025):
		good = True
		for j in st:
			if j ^ i not in st:
				good = False
				break
		if good:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(list(map(int, input().split())))
	ans = -1
	for i in range(1, 1024):
		s1 = set()
		for x in s:
			s1.add(x ^ i)
		if s1 & s == s:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	for x in range(1, 1025):
		n = {c ^ x for c in a}
		if n == a:
			print(x)
			break
	else:
		print(-1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	a = sorted(a)
	flag = 0
	for i in range(1, 1025):
		l = []
		for j in range(n):
			res = a[j] ^ i
			l += [res]
		if a == sorted(l):
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


tc = int(input())
for j in range(0, tc):
	n = int(input())
	ar = [int(i) for i in input().split()]
	flag = 1
	maxar = max(ar)
	var = [0] * (maxar + 1)
	for i in ar:
		var[i] += 1
	res = 0
	while 1:
		counted = var[:]
		temp = 0
		for i in ar:
			if i ^ flag <= maxar and counted[i ^ flag] == 1:
				counted[i ^ flag] = 0
			else:
				temp = 1
				break
		if temp == 0:
			res = 1
			break
		if flag > 1035:
			break
		flag += 1
	if res == 1:
		print(flag)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def solve(n, S):
	for k in range(1, 1025):
		transformed = set()
		for e in S:
			t = e ^ k
			if t in S:
				transformed.add(t)
			else:
				break
		if transformed == S:
			return k
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	S = set(map(int, input().split()))
	print(solve(n, S))


from sys import stdin
input = lambda : stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
mod = 10000000007
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	n = INT()
	l = mp()
	ch = set(l)
	flag = 0
	for i in range(1, 1025):
		check1 = set()
		for j in range(n):
			check1.add(i ^ l[j])
		if check1 == ch:
			flag = i
			break
	if flag == 0:
		print(-1)
	else:
		print(flag)


def mapit():
	temp = list(map(int, input().split()))
	return temp
from math import log2
import heapq

def solution():
	n = int(input())
	arr = mapit()
	ele = set(arr)
	op = []
	for i in range(1, n):
		op.append(arr[0] ^ arr[i])
	op.sort()
	for el in op:
		new = ele.copy()
		nw = []
		for nm in arr:
			tp = nm ^ el
			nw.append(tp)
			if tp in new:
				new.remove(tp)
			else:
				break
		if len(new) == 0:
			print(el)
			return
	print(-1)
	return
t = int(input())
while t:
	t -= 1
	solution()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	res = -1
	for i in range(1, 1025):
		a = []
		for j in range(n):
			a.append(arr[j] ^ i)
		if sorted(a) == arr:
			res = i
			break
	print(res)


import atexit
import io
import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	m = pow(10, 9)
	l = {}
	flag = 0
	for j in range(n):
		for k in range(n):
			if j != k:
				s1 = l.setdefault(s[j] ^ s[k], 0)
				l[s[j] ^ s[k]] += 1
				if l[s[j] ^ s[k]] == n:
					m = min(m, s[j] ^ s[k])
					flag = 1
	if flag == 0:
		print(-1)
	else:
		print(m)


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	s = set(a)
	flag = 0
	for i in range(1, 1025):
		for j in range(len(a)):
			if a[j] ^ i in s:
				flag = 1
				continue
			else:
				flag = 0
				break
		if flag == 1:
			print(i)
			break
	if flag == 0:
		print(-1)
		continue


import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	dict = {}
	for num in s:
		dict[num] = 1
	done = 0
	for i in range(1, 1024):
		flag = 0
		for j in range(len(s)):
			if s[j] ^ i in dict:
				continue
			else:
				flag = 1
				break
		if flag == 0:
			done = 1
			print(i)
			break
	if done == 0:
		print(-1)


import math
test = int(input())
for tes in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	done = False
	for i in range(1, 1025):
		y = set()
		for j in range(n):
			y.add(l[j] ^ i)
		if y == s:
			print(i)
			done = True
			break
	if not done:
		print(-1)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	ch = -1
	for i in range(1, 1025):
		s1 = set()
		for j in a:
			s1.add(j ^ i)
		if s == s1:
			ch = i
			break
	print(ch)


from operator import xor
t = int(input())
for tc in range(t):
	n = int(input())
	s = [int(si) for si in input().split()]
	revlist = [-1] * 1025
	for i in range(len(s)):
		revlist[s[i]] = i
	possible = [xor(a, b) for a in s for b in s]
	setPossible = set(possible)
	setPossible.remove(0)
	possible = list(setPossible)
	possible = sorted(possible)
	ans = -1
	for i in possible:
		bm = [False] * n
		curr = i
		correct = False
		for j in range(len(s)):
			sol = xor(curr, s[j])
			if sol <= 1024 and revlist[sol] != -1 and (not bm[revlist[sol]]):
				bm[revlist[sol]] = True
			else:
				break
			if j == len(s) - 1:
				correct = True
		if correct:
			ans = curr
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	b = set(map(int, input().split()))
	ans = -1
	for k in range(1, 1025):
		flag = 1
		for i in b:
			if i ^ k not in b:
				flag = 0
		if flag == 1:
			ans = k
			break
	print(ans)


t = int(input())
results = []
for i in range(t):
	n = int(input())
	vals = input().split()
	S = set()
	for val in vals:
		S.add(int(val))
	for i in range(1, 1025):
		S2 = set()
		for s in S:
			x = s ^ i
			if x in S2:
				break
			else:
				S2.add(x)
		if S2 == S:
			results.append(i)
			break
	else:
		results.append(-1)
for result in results:
	print(result)


from sys import stdin

def solve():
	return

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		n = int(inp())
		s = list(map(int, inp().split()))
		k = set(s[:])
		ans = True
		for i in range(1, 1025):
			d = []
			for j in range(n):
				d.append(s[j] ^ i)
			if set(d) == k:
				print(i)
				ans = False
				break
		if ans:
			print(-1)
main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	setList = [int(var) for var in input().split()]
	origSet = set(setList)
	flag = True
	for k in range(1, 1025):
		visited = set()
		for var in setList:
			visited.add(k ^ var)
		if len(visited) == len(setList):
			if visited == origSet:
				print(k)
				flag = False
				break
	if flag is True:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 0
	for i in range(1, 1024):
		cnt = 0
		for j in range(n):
			if i ^ arr[j] in arr:
				cnt += 1
		if cnt == n:
			flag = 1
			break
	if flag == 1:
		print(i)
	else:
		print(-1)


import sys
from math import log
from collections import *
ip = lambda : sys.stdin.readline().rstrip()
for test in range(int(ip())):
	n = int(ip())
	a = list(map(int, ip().split()))
	s = set(a)
	ans = -1
	for i in range(1, 1025):
		fl = True
		for j in a:
			v = i ^ j
			if v not in s:
				fl = False
				break
		if fl:
			ans = i
			break
	print(ans)


test = int(input())
while test:
	num = int(input())
	arr = list(map(int, input().split()))
	arr = set(arr)
	res = set()
	flag = 0
	for k in range(1, 1024):
		res.clear()
		for i in arr:
			num = i ^ k
			res.add(num)
		diff = res - arr
		if not diff:
			print(k)
			flag = 1
			break
	if flag == 0:
		print('-1')
	test -= 1


t = int(input())
for ab in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	kx = set()
	for al in a:
		kx.add(al)
	ts = True
	for px in range(1, 1024):
		ux = set()
		count = 0
		for tl in a:
			if tl ^ px in kx and tl ^ px not in ux:
				ux.add(tl ^ px)
			else:
				break
			count += 1
		if ux == kx and count == len(a):
			print(px)
			ts = False
			break
	if ts:
		print(-1)


import math

def Divisors(n):
	l = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l

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

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(n)
	return l

def Factors(n):
	result = []
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if i == n / i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	result.append(1)
	result.append(n)
	return result

def maxSubArraySum(a):
	max_so_far = 0
	max_ending_here = 0
	size = len(a)
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def main():
	l1 = []
	for j in range(0, 1025):
		l1.append(0)
	n = int(input())
	l = list(map(int, input().split()))
	x = 0
	if n % 2 == 1:
		print(-1)
	else:
		for j in l:
			l1[j] = 1
		for j in range(1, 1024):
			f = 0
			for k in range(0, n):
				if l1[l[k] ^ j] == 0:
					f = 1
					break
			if f == 0:
				print(j)
				break
		if f == 1:
			print(-1)
t = int(input())
for i in range(0, t):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	for i in range(1, 1025):
		s2 = set()
		for j in s:
			s2.add(i ^ j)
		if s == s2:
			print(i)
			break
	if i == 1024:
		print(-1)


import sys
ans = []
for kek in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	s_list = list(map(int, sys.stdin.readline().split()))
	s_set = set(s_list)
	s_list.sort()
	flag = False
	for k in range(1, 1024):
		if flag:
			break
		test = set()
		for j in s_set:
			test.add(j ^ k)
		if test == s_set and k > 0:
			ans.append(k)
			flag = True
	if flag:
		continue
	else:
		ans.append(-1)
print(*ans, sep='\n')


import sys
import math

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()
for _ in range(int(input())):
	n = int(input())
	s = get_array()
	x = []
	ans = 0
	for i in range(1, 1024):
		for j in range(n):
			x.append(i ^ s[j])
		if set(s) == set(x):
			ans = i
			break
		x.clear()
	if ans == 0:
		print(-1)
	else:
		print(ans)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = max(l)
	s = set()
	flag = 0
	for i in range(1, 2 * m + 1):
		s.clear()
		for j in l:
			s.add(i ^ j)
		if set(l) == s:
			flag = 1
			break
	if flag == 1:
		print(i)
	else:
		print(-1)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for testc in range(ntest):
		n = int(stdin.readline())
		a = [int(c) for c in stdin.readline().split()]
		A = set(a)
		res = -1
		for k in range(1, 2 * max(A) + 1):
			B = set([x ^ k for x in A])
			if B == A:
				res = k
				break
		stdout.write('%d\n' % res)
	return 0
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = set(map(int, input().split()))
	for i in range(1, 1024):
		tmp = set()
		for j in a:
			tmp.add(i ^ j)
		if tmp == a:
			print(i)
			break
	else:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	S = set(map(int, input().split()))
	for k in range(1, 1024):
		ns = {v ^ k for v in S}
		if S == ns:
			print(k)
			break
	else:
		print(-1)


import sys
input = sys.stdin.readline
f = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = set(f())
	ans = -1
	for i in range(1, 1025):
		b = set([])
		for j in a:
			b.add(j ^ i)
		if not a.difference(b):
			ans = i
			break
	print(ans)


def work(b, i):
	c = []
	for x in b:
		c.append(x ^ i)
	return set(c)

def cc(b):
	c = max(b)
	cc = set(b)
	ccc = 0
	if len(cc) == 2:
		v = list(cc)
		v.sort()
		if v[0] == 0:
			ccc = ccc + v[1]
		else:
			for x in range(1, 1024):
				if cc == work(b, x):
					ccc = ccc + x
					break
	else:
		for x in range(1, 1024):
			if cc == work(b, x):
				ccc = ccc + x
				break
	if ccc != 0:
		print(ccc)
	else:
		print(-1)
t = int(input())
for x in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	cc(b)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = list(map(int, stdin.readline().rstrip().split()))
	d = {}
	for i in s:
		d[i] = True
	ans = -1
	for j in range(1, 1025):
		e = {}
		for i in s:
			if i ^ j in d:
				e[i ^ j] = True
			else:
				break
		if len(d) == len(e):
			ans = j
			break
	print(ans)


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	s = set(a)
	xor = 0
	ans = -1
	for i in range(1, 1025):
		t = set()
		for j in s:
			t.add(j ^ i)
		if t == s:
			ans = i
			break
	print(ans)


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	n = int(I())
	arr = list(map(int, I().split()))
	sett_a = set(arr)
	sett_b = set()
	flag = 0
	for k in range(1, 1025):
		for j in arr:
			sett_b.add(j ^ k)
		if sett_a == sett_b:
			flag = k
			break
		sett_b.clear()
	if flag == 0:
		ans += '-1\n'
	else:
		ans += str(flag) + '\n'
print(ans)


def check(d, arr, k, n):
	for i in range(n):
		try:
			tt1 = d[arr[i] ^ k]
		except:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		flag = True
		d = {}
		for i in range(n):
			d[arr[i]] = True
		for k in range(1, 1025):
			if check(d, arr, k, n):
				print(k)
				flag = False
				break
		if flag:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	lst = [int(x) for x in input().split()]
	lst.sort()
	ans = False
	for k in range(1, 1025):
		ls = []
		for i in lst:
			ls.append(i ^ k)
		ls.sort()
		if ls == lst:
			ans = True
			print(k)
			break
	if ans == False:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = [0] * 1025
	for i in range(n):
		p[l[i]] = 1
	ans = -1
	for i in range(1, 1024):
		f = 0
		for j in range(0, n):
			if p[l[j] ^ i] != 1:
				f = 1
				break
		if f == 0:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	S = [int(s) for s in input().split()]
	it = iter(S)
	first = next(it)
	if len(S) == 1:
		print(-1)
		continue
	deltas = sorted((first ^ v for v in it))
	min_d = None
	for (i, d) in enumerate(deltas):
		all_in = True
		for v in S:
			if d ^ v not in S:
				all_in = False
				break
		if all_in and (min_d is None or d < min_d):
			min_d = d
	print(-1 if min_d is None else min_d)


def solve(n, s):
	ss = set(s)
	p = sorted([t ^ s[0] for t in s if t != s[0]])
	for x in p:
		for t in s:
			if x ^ t not in ss:
				break
		else:
			return x
	return -1
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	s = list(map(int, input().strip().split()))
	print(solve(n, s))


t = int(input())
for testcase in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = 1
	T = True
	while k < 1025:
		for i in range(n):
			v = l[i] ^ k
			if v not in l:
				T = False
				break
		if T == False:
			k = k + 1
			T = True
		else:
			break
	if k != 1025:
		print(k)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	t = 0
	mm = max(a)
	for kk in range(11):
		if 2 ** kk > mm:
			break
	for k in range(1, 2 ** kk + 1):
		b = [None] * n
		for i in range(n):
			b[i] = a[i] ^ k
		if a == sorted(b):
			t = 1
			break
	if t == 0:
		print('-1')
	else:
		print(k)


t = int(input())
for hh in range(0, t):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = dict()
	for i in a:
		d[i] = 1
	for i in range(1, 1025):
		flag = 0
		for j in range(0, n):
			if i ^ a[j] not in d:
				flag = -1
				break
		if flag == 0:
			print(i)
			break
	if flag == -1:
		print(-1)


for _ in ' ' * int(input()):
	a = int(input())
	b = sorted(map(int, input().split()))
	k = 0
	for i in range(1, 1025):
		c = []
		for j in b:
			c += [j ^ i]
		if b == sorted(c):
			k = i
			break
	print(k if k else -1)


def solve():
	n = int(input())
	S = set(map(int, input().split()))
	ans = -1
	for k in range(1, 1025):
		Srev = set([k ^ s for s in S])
		if Srev == S:
			ans = k
			break
	print(ans)
T = int(input())
while T > 0:
	T -= 1
	solve()


import math
from decimal import *
getcontext().prec = 30
t = int(input())
while t:
	t -= 1
	n = int(input())
	s = set(map(int, input().split()))
	printed = 0
	for i in range(1, 1024):
		temp = set()
		for val in s:
			temp.add(val ^ i)
		if temp == s:
			print(i)
			printed = 1
			break
	if not printed:
		print(-1)


for t in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	a = set(data)
	for i in range(1, 1025):
		s = set()
		for j in data:
			s.add(i ^ j)
		if a == s:
			print(i)
			break
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	S = set(map(int, input().split()))
	S2 = set()
	r = -1
	for num in range(1, 1025):
		flag = True
		for i in S:
			if i ^ num not in S:
				flag = False
				break
		if flag:
			r = num
			break
	print(r)


for test in range(int(input())):
	num = int(input())
	arr = set(map(int, input().split()))
	res = set()
	flag = False
	for k in range(1, 1024):
		res.clear()
		for el in arr:
			num = el ^ k
			res.add(num)
		set_difference = res - arr
		if not set_difference:
			print(k)
			flag = True
			break
	if flag == False:
		print('-1')


a = int(input())
for i in range(0, a):
	a = int(input())
	arr = list(map(int, input().split()))
	k = 1
	pivot = 0
	while k <= 1024:
		brr = []
		for i in arr:
			if k ^ i in arr:
				brr.append(k ^ i)
			else:
				break
		if len(set(brr)) == len(arr):
			pivot = 1
			print(k)
			break
		k = k + 1
	if pivot == 0:
		print(-1)


def f(list1):
	if len(list1) < 2:
		return -1
	successes = []
	set1 = set(list1)
	for item in list1[1:]:
		cand = list1[0] ^ item
		if set([t ^ cand for t in list1]) == set1:
			successes.append(cand)
	if len(successes) == 0:
		return -1
	return min(successes)
numberofcases = int(input())
for _ in range(numberofcases):
	n = int(input())
	l = [int(t) for t in input().split()]
	print(f(l))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	f = 0
	for i in range(1, 1025):
		c = i
		s = set()
		for j in arr:
			if c ^ j in arr:
				s.add(c ^ j)
			else:
				break
		if len(s) == n:
			f = 1
			print(c)
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst2 = lst[:]
	lst.sort()
	ans = True
	f = False
	for i in range(1, 1024):
		for j in lst2:
			j = j ^ i
			if j not in lst:
				ans = False
				break
		if not ans:
			ans = True
			continue
		else:
			lst2.sort()
			if lst == lst2:
				f = True
				break
	if ans and f:
		print(i)
	else:
		print(-1)


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	a = sorted(arr)
	minn = 1
	maxx = 1024
	r = True
	for x in range(minn, maxx):
		b = []
		for y in range(n):
			c = arr[y] ^ x
			b.append(c)
		b.sort()
		if a == b:
			print(x)
			r = False
			break
	if r == True:
		print(-1)
	i = i + 1


class Solution:

	def __init__(self):
		for t in range(int(input())):
			n = int(input())
			s = list(map(int, input().split()))
			self.solve(n, s)

	def solve(self, n, s):
		s.sort()
		for i in range(1, 1024):
			t = sorted([j ^ i for j in s])
			if t == s:
				print(i)
				return
		print(-1)
Solution()


import math
import os
import random
import re
import sys
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	is_ans = True
	for i in range(1, 1024):
		res = {}
		is_ans = True
		for j in range(len(arr)):
			r = arr[j] ^ i
			if r in res:
				res[r] += 1
			else:
				res[r] = 1
		for j in range(len(arr)):
			if arr[j] in res and res[arr[j]] > 0:
				res[arr[j]] -= 1
			else:
				is_ans = False
				break
		if is_ans:
			print(i)
			break
	if not is_ans:
		print(-1)


from math import *
from collections import *
from operator import itemgetter
import bisect
from heapq import *
i = lambda : input()
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()

def find(x):
	y = bin(x + 1)[2:]
	x = bin(x)[2:]
	x = '0' * (len(y) - len(x)) + x
	c = 0
	for i in range(len(y)):
		if x[i] != y[i]:
			c += 1
	return c
for _ in range(t):
	n = ii()
	a = set(iia())
	flag = 1
	for i in range(1, 1024):
		new = set()
		for j in a:
			new.add(j ^ i)
		if new == a:
			print(i)
			flag = 0
			break
	if flag == 1:
		print(-1)


for t in range(int(input())):
	(n, S, ans) = (int(input()), sorted(set([int(x) for x in input().split()])), -1)
	for k in range(1, 1025):
		temp = sorted(set([i ^ k for i in S]))
		if temp == S:
			ans = k
			break
	print(ans)


import sys
import math
t = int(sys.stdin.readline().strip())
for _ in range(t):
	n = int(sys.stdin.readline().strip())
	l = list(map(int, sys.stdin.readline().strip().split(' ')))
	s = set(l)
	found = False
	for k in range(1, 1024):
		rs = set([])
		for e in s:
			r = e ^ k
			if r not in s:
				break
			else:
				rs.add(r)
		if len(rs) == len(s):
			print(k)
			found = True
			break
	if not found:
		print(-1)


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = rl()
	a = set(a)
	ans = -1
	l = []
	for i in range(1, 10 ** 4):
		l = set()
		for j in a:
			l.add(i ^ j)
		if l == a:
			ans = i
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	if n % 2 == 1:
		print(-1)
	else:
		a = {}
		for i in range(n):
			x = s[i]
			for j in range(i + 1, n):
				y = s[j]
				z = x ^ y
				if z in a:
					a[z] += 1
				else:
					a[z] = 1
		m = float('inf')
		for k in a:
			if a[k] >= n // 2:
				m = min(k, m)
		if m == float('inf'):
			print(-1)
		else:
			print(m)


t = int(input())

def perebor(number, mas):
	new_mas = []
	for l in mas:
		new_mas.append(l ^ number)
	return new_mas
for i in range(t):
	n = int(input())
	mas = [int(y) for y in input().split()]
	k = -1
	for j in range(1, 1024):
		ans = perebor(j, mas)
		if set(ans) == set(mas):
			k = j
			break
	print(k)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(c) for c in input().split()]
	inp = [0 for i in range(1025)]
	oup = [0 for i in range(1025)]
	for i in arr:
		inp[i] = 1
		oup[i] = 1
	for i in range(1, 1025):
		tempoup = oup.copy()
		for ele in arr:
			k = i ^ ele
			if k < len(tempoup):
				if tempoup[k] == 1:
					tempoup[k] = 0
				else:
					break
			else:
				break
		else:
			if set(tempoup) == {0}:
				print(i)
				break
	else:
		print(-1)


import sys
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	n = int(sys.stdin.readline())
	s = list(map(int, sys.stdin.readline().split()))
	s.sort()
	s_ = set(s)
	contestants = []
	for i in range(1, n):
		contestants.append(s[i] ^ s[0])
	for contestant in contestants:
		for one in s:
			if one ^ contestant not in s_:
				break
		else:
			print(contestant)
			break
		continue
	else:
		print(-1)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	cr = [0] * 1025
	x = set(l)
	an = 1025
	ar = [0] * n
	if n % 2:
		print(-1)
	else:
		for j in range(1, n):
			ar[j] = l[0] ^ l[j]
		for i in ar:
			if i != 0:
				d = set()
				for j in range(n):
					d.add(i ^ l[j])
				if d == x:
					an = min(an, i)
		print(an if an != 1025 else -1)


import sys

def arr():
	return list(map(int, input().split()))
input = sys.stdin.readline
from collections import defaultdict
import math
for _ in range(int(input())):
	N = int(input())
	L = arr()
	L.sort()
	flag = False
	for i in range(1, 1025):
		x = L[:]
		for j in range(N):
			x[j] = x[j] ^ i
		x.sort()
		x.sort()
		if x == L:
			ans = i
			flag = True
			break
	if flag:
		print(ans)
	else:
		print(-1)


import math
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	c = defaultdict(int)
	(max1, max2) = (-1, -1)
	for i in range(n - 1):
		for j in range(i + 1, n):
			d = l[i] ^ l[j]
			c[d] += 1
			if max1 == -1:
				max1 = c[d]
				max2 = d
			elif max1 < c[d]:
				max1 = c[d]
				max2 = d
			elif max1 == c[d]:
				if d < max2:
					max2 = d
	if max1 >= math.ceil(n / 2):
		print(max2)
	else:
		print(-1)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s = set(s)
	for i in range(1, 1024):
		ns = set()
		for si in s:
			ns.add(si ^ i)
		if s == ns:
			print(i)
			break
	else:
		print(-1)


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
t = int(input())
for tc in range(t):
	n = int(stdin.readline())
	ar = list(map(int, stdin.readline().split()))
	arset = set(ar)
	res = -1
	if n % 2 == 1:
		pass
	else:
		totxor = 0
		for v in ar:
			totxor ^= v
		if n % 4 == 0 and totxor > 0:
			pass
		else:
			for target in range(1, 1026):
				all_good = True
				for val in ar:
					if target ^ val not in arset:
						all_good = False
						break
				if all_good:
					res = target
					break
	stdout.write(str(res) + '\n')


x = int(input())
for i in range(x):
	n = int(input())
	num = list(map(int, input().split()))
	maxi = max(num)
	dix = {}
	for j in num:
		dix[j] = 0
	dix2 = dix.copy()
	flag = 0
	for j in range(1, 1025):
		dix = dix2.copy()
		for k in num:
			xor = k ^ j
			dix[xor] = 1
		if 0 in list(dix.values()):
			continue
		else:
			flag = 1
			print(j)
			break
	if flag == 0:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	for i in range(1, 1024):
		temp = [*s]
		yep = False
		for j in range(n):
			temp[j] = temp[j] ^ i
		res = all((item in temp for item in s))
		if res:
			yep = True
			print(i)
			break
	if not yep:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	ans1 = -1
	for i in range(1, 1024):
		s1 = set()
		for x in s:
			s1.add(x ^ i)
		if s1 & s == s:
			ans1 = i
			break
	print(ans1)


from sys import stdin, stdout
from math import gcd, log2, log10, floor
import math
from collections import defaultdict, OrderedDict

def xor(l, no):
	a = l.copy()
	for i in range(len(l)):
		a[i] = l[i] ^ no
	return a

def fun(l):
	l.sort()
	for i in range(1, 1030):
		a = xor(l, i)
		if sorted(a) == l:
			return i
	return -1
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = fun(l)
	print(ans)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

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

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

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

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

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def solve():
	n = int(input())
	arr = list_input()
	hash_map = {}
	for i in arr:
		hash_map[i] = 0
	for i in range(1, 1025):
		visited = {}
		flag = True
		for j in arr:
			x = i ^ j
			try:
				m = hash_map[x]
				try:
					m = visited[x]
					flag = False
					break
				except KeyError:
					visited[x] = 1
			except KeyError:
				flag = False
				break
		if flag:
			print(i)
			return
	print(-1)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	c = -1
	for k in range(1, 1024 + 1):
		s_a = set()
		for i in a:
			if i ^ k not in s:
				break
			else:
				s_a.add(i ^ k)
		if s_a == s:
			c = k
			break
	print(c)


MAX = 2000
t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	ans = -1
	for k in range(1, MAX + 1):
		s2 = set([i ^ k for i in s])
		if s == s2:
			ans = k
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	S = list(map(int, input().split(' ')))
	done = False
	for k in range(1, 1024):
		s = set(S)
		while len(s) > 0:
			x = s.pop()
			if k ^ x not in s:
				s.add(x)
				break
			else:
				s.remove(k ^ x)
		if len(s) == 0:
			print(k)
			done = True
			break
	if not done:
		print(-1)


for t in range(int(input())):
	n = int(input())
	arr = sorted([int(i) for i in input().split()])
	k = 1
	check = False
	while k <= 1024:
		test = sorted([i ^ k for i in arr])
		if test == arr:
			print(k)
			check = True
			break
		k += 1
	if not check:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	(ans, f) = (-1, 0)
	for k in range(1, 1025):
		for x in a:
			if k ^ x not in s:
				f = 1
				break
		else:
			ans = k
			break
	print(ans)


import sys
import math
from sys import stdin, stdout

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

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

def get_string():
	return sys.stdin.readline().strip()

def main():
	for t in range(int(input())):
		n = int(input())
		a = get_list()
		s = set(a)
		f = 0
		for i in range(1, 1025):
			c = 0
			for j in a:
				z = j ^ i
				if z in s:
					c += 1
				else:
					break
			if c == n:
				f = 1
				break
		if f == 1:
			print(i)
		else:
			print(-1)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	count = 0
	for k in range(1, 1025):
		for i in s:
			i ^= k
			if i in s:
				count += 1
		if count == n:
			print(k)
			break
		count = 0
		if k == 1024:
			print(-1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ch = []
	dic = defaultdict(bool)
	if n == 1:
		print(-1)
	else:
		st = arr[0]
		ch = []
		for i in range(1, n):
			ch.append(st ^ arr[i])
		temp = list(arr)
		temp.sort()
		ans = []
		for j in ch:
			a = []
			for i in range(n):
				a.append(arr[i] ^ j)
			if sorted(a) == temp:
				ans.append(j)
		if len(ans) == 0:
			print(-1)
		else:
			print(min(ans))


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, S):
	candidates = set()
	for i in range(N):
		for j in range(i + 1, N):
			candidates.add(S[i] ^ S[j])
	S = set(S)
	for c in sorted(candidates):
		S2 = set((x ^ c for x in S))
		if S == S2:
			return c
	return -1
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	S = [int(x) for x in input().split()]
	ans = solve(N, S)
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	maxi = 1024
	f = 0
	for j in range(1, maxi + 1):
		temp = [i for i in arr]
		for i in range(n):
			temp[i] ^= j
		temp.sort()
		if temp == arr:
			f = j
			break
	if f == 0:
		print(-1)
	else:
		print(f)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	for i in range(1, 1025):
		tarr = []
		for j in range(n):
			tarr.append(arr[j] ^ i)
		tarr.sort()
		f = 1
		for j in range(n):
			if arr[j] != tarr[j]:
				f = 0
				break
		if f == 1:
			ans = i
			break
	if ans == 0:
		ans = -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	r = []
	num = 1
	for i in range(1, 1025):
		r.sort()
		if a == r:
			print(i - 1)
			num = 0
			break
		r = []
		for j in a:
			r.append(j ^ i)
	if num:
		print(-1)


from sys import stdin, stdout

def main():
	for t in range(int(stdin.readline())):
		n = int(stdin.readline())
		a = set(map(int, stdin.readline().strip().split()))
		ans = -1
		ma = 1024
		for i in range(1, ma + 1):
			flag = 0
			for j in a:
				if i ^ j not in a:
					flag = 1
					break
			if flag == 0:
				ans = i
				break
		stdout.write(str(ans) + '\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	sl = list(map(int, input().split(' ')))
	ss = set(sl)
	ans = -1
	for g in range(1, 1024):
		if ss == set((v ^ g for v in sl)):
			ans = g
			break
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split(' ')))
	a = set(s)
	for k in range(1, 1026):
		s = list(a)
		for i in range(n):
			s[i] = s[i] ^ k
		if a == set(s):
			print(k)
			break
	if k == 1025:
		print(-1)


import sys
from functools import reduce
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
cin = lambda : int(read())
for _ in range(cin()):
	n = cin()
	a = list(mi())
	flag = False
	ans = -1
	for i in range(1, 1024):
		flag = True
		for el in a:
			if el ^ i not in a:
				flag = False
				break
		if flag:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	current = set(data)
	result = -1
	for k in range(1, 1025):
		temp = []
		for i in data:
			temp.append(k ^ i)
		if set(temp) == current:
			result = k
			break
	print(result)


from collections import defaultdict
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(list)
	for i in range(n):
		for j in range(i + 1, n):
			x = a[i] ^ a[j]
			d[x].append(a[i])
			d[x].append(a[j])
	m = 9999999999999
	for i in d:
		if set(d[i]) == set(a):
			if i < m:
				m = i
	if m == 9999999999999:
		print(-1)
	else:
		print(m)
	t -= 1


import sys
input = sys.stdin.buffer.readline
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	(*s,) = map(int, input().split())
	if n % 2:
		ans.append(-1)
		continue
	inS = [0] * 1024
	for x in s:
		inS[x] = True
	for i in range(1, 1024):
		if all((inS[x ^ i] for x in s)):
			ans.append(i)
			break
	else:
		ans.append(-1)
print('\n'.join(map(str, ans)))


import sys, math, os
from io import BytesIO, IOBase
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1000000000.0) + 7
from decimal import Decimal

def main():
	for i in range(int(data())):
		n = int(data())
		s = mdata()
		if n == 1:
			out(-1)
		ans = 0
		s1 = set()
		for j in s[1:]:
			s1.add(j ^ s[0])
		flag = True
		s1 = sorted(s1)
		s2 = set(s)
		for j in s1:
			flag = True
			for k in s:
				if j ^ k not in s2:
					flag = False
					break
			if flag == True:
				out(j)
				break
		if flag == False:
			out(-1)
main()


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007

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

def out(var):
	sys.stdout.write(str(var))

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

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def solve():
	n = int(inp())
	a = lis()
	temp = []
	for i in range(0, n):
		for j in range(0, n):
			if i != j:
				temp.append(a[i] ^ a[j])
	temp = list(set(temp))
	temp = sorted(temp)
	f = True
	for i in range(0, len(temp)):
		ans = []
		for j in range(0, n):
			if a[j] ^ temp[i] in a:
				ans.append(a[j] ^ temp[i])
		if set(ans) == set(a):
			print(temp[i])
			f = False
			break
	if f:
		print(-1)
testcase(int(inp()))


def isvalid(k, co):
	for i in li:
		x = i ^ k
		if x not in co:
			return 0
		if co[x] == 0:
			return 0
		co[x] -= 1
	return 1
t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	if n % 2:
		print(-1)
		continue
	else:
		li = [int(i) for i in l]
		hashi = dict()
		for i in li:
			if i in hashi:
				hashi[i] += 1
			else:
				hashi[i] = 1
		mina = []
		for i in range(1, n):
			co = hashi.copy()
			k = li[0] ^ li[i]
			if isvalid(k, co) and k > 0:
				mina.append(k)
		if mina == []:
			print(-1)
		else:
			print(min(mina))


import os
import sys, math
from io import BytesIO, IOBase

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		mx = max(l)
		v = pow(2, len(bin(mx)[2:])) - 1
		ans = -1
		for j in range(1, v + 1):
			s = set()
			for k in range(n):
				s.add(j ^ l[k])
			chk = True
			for k in l:
				if k not in s:
					chk = False
					break
			if chk:
				ans = j
				break
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
from typing import List

def solve(N, S):
	for i in range(1, 1024):
		if all({i ^ v in S for v in S}):
			return i
	return -1
T = int(input())
ans = []
for ti in range(T):
	N = int(input())
	S = [int(x) for x in input().split()]
	ans.append(solve(N, S))
print('\n'.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n & 1:
		print(-1)
		continue
	l.sort()
	x = []
	for i in range(1, n):
		x.append(l[0] ^ l[i])
	ans = -1
	for i in x:
		l2 = []
		for j in l:
			l2.append(i ^ j)
		if sorted(l2) == l:
			ans = i
			break
	print(ans)


def main():
	for _ in range(int(input())):
		n = int(input())
		s = list(map(int, input().split()))
		k1 = set(s)
		w = 0
		for j in range(1, pow(2, 1 + len(bin(max(s)))) + 1):
			k2 = set()
			for i in range(n):
				k2.add(s[i] ^ j)
			if k1 == k2:
				k2 = j
				w = 1
				break
		print(-1 if w == 0 else k2)
main()


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

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

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mod = 1000000007
import math
import bisect
from decimal import *
getcontext().prec = 50
abc = 'abcdefghijklmnopqrstuvwxyz'

def fn(n):
	c = -1
	while n:
		c += 1
		n //= 2
	return c

def isPowerOfTwo(n):
	if n == 0:
		return False
	while n != 1:
		if n % 2 != 0:
			return False
		n = n // 2
	return True
pi = 3.141592653589793

def main():
	for _ in range(ii()):
		n = ii()
		a = li()
		a.sort()
		f = 0
		for i in range(1, 1025):
			if f:
				break
			l = []
			for j in range(n):
				l.append(a[j] ^ i)
			l.sort()
			if a == l:
				print(i)
				f = 1
			else:
				l = []
		if f == 0:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	ans = -1
	for i in range(1, 1024):
		st = set()
		for j in range(n):
			st.add(l[j] ^ i)
		if s == st:
			ans = i
			break
	print(ans)


N = int(input())
for i in range(N):
	n = int(input())
	L = list(map(int, input().split()))
	L.sort()
	F = 0
	for j in range(1, 1025):
		if F == 1:
			break
		l = []
		for k in range(n):
			l.append(j ^ L[k])
		l.sort()
		if L == l:
			print(j)
			F = 1
	if F == 0:
		print(-1)


t = int(input())

def decbinary(num):
	if num < 2:
		return str(num)
	else:
		num1 = num % 2
		return decbinary(num // 2) + str(num1)
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	for k in range(1, 2 * arr[-1] + 1, 1):
		if k == 0:
			continue
		ct = 0
		arr1 = arr.copy()
		while len(arr1) > 0:
			if arr1[0] ^ k in arr1:
				arr1.remove(arr1[0] ^ k)
				del arr1[0]
				ct += 2
			else:
				break
		if ct == n:
			if k == 0:
				ct = 0
			else:
				print(k)
				break
	if ct != n:
		print(-1)


import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = -1
	for i in range(1, 1100):
		tmp = [0] * n
		for j in range(n):
			tmp[j] = i ^ l[j]
		tmp.sort()
		if tmp == l:
			ans = i
			break
	print(ans)


t = int(input())
for __ in range(t):
	n = int(input())
	l = set(map(int, input().split()))
	flag = 0
	for i in range(1, 1025):
		x = set()
		for j in l:
			x.add(i ^ j)
		if x == l:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


import sys
input = sys.stdin.readline
for ttt in range(int(input())):
	n = int(input())
	l = set(list(map(int, input().split())))
	for k in range(1, 1025):
		ns = {k ^ j for j in l}
		if ns == l:
			print(k)
			break
	else:
		print(-1)


test_case = int(input())
my_list = []
my_array = []
for i in range(test_case):
	my_list.append(int(input()))
	my_array.append([int(j) for j in input().split()])
for x in range(test_case):
	length = len(bin(max(my_array[x]))) - 2
	total = 0
	y = 1
	my_array[x].sort()
	while y <= 2 ** length:
		m = []
		for z in range(my_list[x]):
			m.append(my_array[x][z] ^ y)
		m.sort()
		if m == my_array[x]:
			total += 1
			break
		y += 1
	if total == 0:
		print(-1)
	else:
		print(y)


import math
tc = int(input())
for _ in range(tc):
	m = int(input())
	lis = [0] * 1025
	nums = [int(i) for i in input().split()]
	for i in nums:
		lis[i] = 1
	nums.sort()
	possible = []
	for i in range(1, 1025):
		broken = False
		for j in nums:
			new = i ^ j
			if new not in nums or lis[new] != 1:
				broken = True
				break
		if not broken:
			print(i)
			break
	if broken:
		print(-1)


t = int(input())

def aze(i):
	s = {j: False for j in a}
	for k in a:
		if k ^ i in s.keys():
			s[k ^ i] = True
		else:
			return False
	if False in s.values():
		return False
	return True
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, 2 ** 10 + 1):
		if aze(i):
			print(i)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	S = [int(s) for s in input().split()]
	if n % 2 == 1:
		print(-1)
		continue
	setS = set(S)
	ans = 10 ** 4
	for i in range(1, n):
		k = S[0] ^ S[i]
		flag = True
		for j in range(n):
			if k ^ S[j] not in setS:
				flag = False
				break
		if flag:
			ans = min(ans, k)
	if ans == 10 ** 4:
		ans = -1
	print(ans)


ll = lambda : list(map(int, input().split()))
s = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
import math
from collections import Counter
for _ in range(ii()):
	a = ii()
	x = ll()
	x.sort()
	f = 0
	for i in range(1, 1029):
		l = []
		for j in x:
			l.append(i ^ j)
		l.sort()
		if x == l:
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


def answer(n, A):
	dp = [0] * 1024
	for i in A:
		dp[i] += 1
	for i in range(1, 1024):
		count = 0
		for j in range(n):
			k = A[j] ^ i
			if dp[k] == 0:
				break
			count += 1
		if count == n:
			return i
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(int(answer(n, A)))


import sys
import math
from collections import defaultdict

def solve(test):
	n = int(input())
	s = list(map(int, input().split()))
	rec = set(s)
	for i in range(1, 1024):
		temp = set()
		for j in range(n):
			if s[j] ^ i in rec:
				temp.add(s[j])
		if len(temp) == n:
			print(i)
			return
	print(-1)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


from sys import stdin, stdout
from collections import deque
input = stdin.readline

def print(x='', end='\n'):
	stdout.write(str(x) + end)

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	for i in range(1, 1025):
		tmp = []
		for j in range(n):
			tmp.append(l[j] ^ i)
		tmp.sort()
		if tmp == l:
			print(i)
			return
	print(-1)
tt = 1
tt = int(input())
for __ in range(tt):
	solve()


import sys

def solve(S):
	MAX_K = 1024
	setS = set(S)
	for k in range(1, MAX_K + 1):
		setK = set([k ^ i for i in S])
		if setK == setS:
			return k
	return -1
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
for i in range(T):
	n = int(IN[1 + 2 * i])
	S = [int(x) for x in IN[2 + 2 * i].split(' ')]
	result = solve(S)
	print(result)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	length = -1
	for i in range(n):
		length = max(length, len(bin(a[i])[2:]))
	for i in range(1, 2 ** length):
		flag = True
		d = {}
		for x in a:
			d[x] = 0
		for x in a:
			if d.get(x ^ i) == None:
				flag = False
				break
			else:
				d[x ^ i] += 1
		if not flag:
			continue
		for x in d:
			if d[x] == 0:
				flag = False
				break
		if flag:
			print(i)
			return
	print(-1)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for _ in range(INT()):
	n = INT()
	arr = LIST()
	x = max(arr)
	s = set()
	for i in arr:
		s.add(i)
	f1 = 0
	for i in range(1, 1024):
		flag = 0
		s1 = s.copy()
		for j in range(n):
			x = arr[j] ^ i
			if x not in s1:
				flag = 1
				break
			else:
				s1.remove(x)
		if flag == 0:
			print(i)
			f1 = 1
			break
	if f1 == 0:
		print(-1)


import math
tc = int(input().strip())
while tc:
	n = int(input().strip())
	arr = set(map(int, input().strip().split()))
	ans = -1
	mx = max(arr)
	if mx:
		mx = 2 ** (int(math.ceil(math.log2(mx))) + 1)
		for tv in range(1, mx + 1):
			flag = True
			for a in arr:
				if tv ^ a not in arr:
					flag = False
			if flag:
				ans = tv
				break
	print(ans)
	tc -= 1


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	has = [0] * 5000
	for i in range(n):
		for j in range(n):
			has[lis[i] ^ lis[j]] += 1
	c = 1
	for i in range(1, 4000):
		if has[i] == n:
			print(i)
			c = 0
			break
	if c:
		print(-1)


def solve(x):
	for i in range(1, 1024):
		if func(i, x) == x:
			return i
	return -1

def func(i, x):
	l = set()
	for j in x:
		l.add(j ^ i)
	return l
t = int(input())
while t > 0:
	n = int(input())
	x = set(map(int, input().split()))
	print(solve(x))
	t -= 1


ans = ''
for _ in range(int(input())):
	n = int(input())
	flag = False
	arr = list(map(int, input().split()))
	for i in range(1, 1024):
		arr1 = []
		for j in arr:
			arr1.append(i ^ j)
		arr.sort()
		arr1.sort()
		if arr == arr1 and i <= 1024:
			print(i)
			flag = True
			break
	if flag == False:
		print('-1')


def solution(x):
	for k in range(1, 1025):
		new_s = set()
		for elem in x:
			new_s.add(elem ^ k)
		if new_s == x:
			return k
	return -1
tests = int(input())
for _ in range(tests):
	n = int(input())
	x = set(map(int, input().split()))
	print(solution(x))


def solve(a):
	m = len(a)
	a = set(a)
	k = 0
	loop = True
	while loop and k < 1024:
		k += 1
		loop = False
		for x in a:
			if x ^ k not in a:
				loop = True
				break
	if k == 1024:
		k = -1
	return k
t = int(input())
for case in range(t):
	n = int(input())
	s = [int(x) for x in input().split(' ')]
	print(solve(s))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	S = list(map(int, input().split(' ')))
	S.sort()
	ans = -1
	for i in range(1, 1024):
		B = [k for k in S]
		for j in range(n):
			B[j] = B[j] ^ i
		B.sort()
		if B == S:
			ans = i
			break
	print(ans)


def main():
	test = int(input())
	for _ in range(test):
		n = int(input())
		s = list(map(int, input().split()))
		s.sort()
		ans = -1
		for var in range(1, 1024):
			current = []
			for index in range(n):
				current.append(var ^ s[index])
			current.sort()
			for index in range(n):
				if current[index] != s[index]:
					break
			else:
				ans = var
				break
		print(ans)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = set(map(int, input().split()))
	if n % 2 != 0:
		print(-1)
		continue
	count = dict()
	m = -1
	min1 = 1025
	min2 = 1026
	for e in s:
		e_string = bin(e)[2:]
		for i in range(len(e_string)):
			if i not in count.keys():
				m = i
				count[i] = 0
			if e_string[-i - 1] == '1':
				count[i] += 1
		if e < min1:
			min2 = min1
			min1 = e
		elif e < min2:
			min2 = e
	k = list()
	for i in range(m + 1):
		if count[i] == n // 2:
			curr = 1 << i
			if len(k) == 0:
				k.append(0)
				k.append(curr)
			else:
				l = list()
				for e in k:
					l.append(e + curr)
				for e in l:
					k.append(e)
	k.sort()
	for c in k:
		if c == 0:
			continue
		for e in s:
			if c ^ e not in s:
				break
		else:
			print(c)
			break
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().rstrip().split(' ')))
	l.sort()
	r = -1
	for i in range(1, 1024):
		test = []
		for j in l:
			test.append(int(bin(j ^ i), 2))
		test.sort()
		if l == test:
			r = i
			break
	print(r)


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		x = [0] * 1024
		ans = -1
		if n % 2:
			print(ans)
			continue
		for i in range(1, 1024):
			x = [0] * 1024
			p = 0
			for j in range(n):
				x[(i ^ a[j]) - 1] += 1
			for j in range(n):
				if x[a[j] - 1] == 1:
					p += 1
			if p == n:
				ans = i
				break
		print(ans)


def check(k):
	for i in val:
		if not pre[i ^ k]:
			return False
	return True

def solve():
	for i in val:
		pre[i] = True
	for k in range(1, 1024):
		if check(k):
			print(k)
			return
	print(-1)
t = int(input())
for _ in range(t):
	pre = [False] * 1025
	n = int(input())
	val = list(map(int, input().split()))
	solve()


import sys
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	lst = get_int_list()
	a = lst[0]
	res = []
	for i in range(1, n):
		res.append(lst[i] ^ a)
	res.sort()
	ans = []
	for i in range(n - 1):
		m = res[i]
		valid = True
		for j in range(n):
			if lst[j] ^ m not in lst:
				valid = False
				break
		if valid:
			ans.append(m)
	if len(ans) > 0:
		print(min(ans))
	else:
		print(-1)


from sys import stdin
from collections import Counter

def xor(a, b):
	return a ^ b

def answer(arr):
	dic = Counter(arr)
	ks = []
	for i in range(len(arr) - 1):
		k = xor(arr[0], arr[i + 1])
		allPass = True
		for j in range(len(arr)):
			x = xor(k, arr[j])
			if dic.get(x) is None:
				allPass = False
				break
		if allPass is True:
			ks.append(k)
	if len(ks) == 0:
		return -1
	return min(ks)
testCount = int(input())
for _ in range(testCount):
	n = int(input())
	arr = list(map(int, stdin.readline().split()))
	print(answer(arr))


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

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

def fi():
	return float(stdin.readline())

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

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
res = ['Yes', 'No']
test_case = ii()
while test_case:
	test_case -= 1
	n = ii()
	a = set(li())
	f = 1
	for i in range(1, max(a) + 1 + min(a)):
		s = set()
		for j in a:
			s.add(i ^ j)
		if a == s:
			print(i)
			f = 0
			break
	if f:
		print(-1)


from sys import stdin
from math import log

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

def sep_input():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	l = list_input()
	if n % 2 == 0:
		s = set(l)
		ff = 0
		for j in range(1, 1025):
			f = 1
			for i in range(n):
				if l[i] ^ j not in s:
					f = 0
					break
			if f == 1:
				print(j)
				ff = 1
				break
		if ff == 0:
			print(-1)
	else:
		print(-1)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	M = 0
	for a in A:
		M = max(M, a.bit_length())
	ans = INF
	for bit in range(1, 1 << M):
		B = [0] * N
		for i in range(N):
			B[i] = A[i] ^ bit
		if set(A) == set(B):
			ans = min(ans, bit)
	if ans == INF:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, max(a) * 2):
		f = 0
		for j in a:
			if j ^ i not in a:
				f = 1
				break
		if f == 0:
			break
	if f == 0:
		print(i)
	else:
		print(-1)


from sys import stdin, stdout
from math import ceil

def solve(n, s):
	s.sort()
	for i in range(1, 1025):
		b = []
		for j in range(n):
			b.append(s[j] ^ i)
		b.sort()
		flag = True
		for j in range(n):
			if s[j] != b[j]:
				flag = False
				break
		if flag:
			print(i)
			return True
	print(-1)
for _ in range(int(input())):
	n = int(stdin.readline())
	s = list(map(int, stdin.readline().split()))
	solve(n, s)


def process(n, a):
	a.sort()
	for k in range(1, 1025):
		b = list()
		for i in range(0, n):
			b.append(k ^ a[i])
		b.sort()
		if a == b:
			print(k)
			print('\n')
			return
	print('-1\n')
test = int(input())
while test > 0:
	test -= 1
	n = int(input())
	a = list(map(int, input().split()))
	process(n, a)


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	d = dict()
	for j in b:
		if j in d.keys():
			d[j] += 1
		else:
			d[j] = 1
	k = 1
	while k < 1024:
		c = []
		for j in range(n):
			c.append(b[j] ^ k)
		e = dict()
		for i in c:
			if i in e.keys():
				e[i] += 1
			else:
				e[i] = 1
		f = 0
		for j in set(b):
			if j not in e.keys():
				f = 1
				break
			elif d[j] != e[j]:
				f = 1
				break
		if f == 0:
			break
		k += 1
	if f == 0:
		print(k)
	else:
		print(-1)


a = int(input())
for i in range(a):
	b = int(input())
	c = [int(x) for x in input().split()]
	c = sorted(c)
	if b % 2 != 0:
		print(-1)
	else:
		n = c[b - 1]
		cnt = 0
		while n:
			cnt += 1
			n >>= 1
		cnt = pow(2, cnt)
		uuuu = 0
		for i in range(1, cnt):
			temp = c.copy()
			chk = 0
			while len(temp) != 0:
				k = temp[0] ^ i
				if k in temp:
					temp.remove(k)
					temp.pop(0)
				else:
					break
			if len(temp) == 0:
				print(i)
				uuuu = 1
				break
		if uuuu == 0:
			print(-1)


t = int(input())
results = []
for i in range(t):
	n = int(input())
	vals = input().split()
	S = {int(val) for val in vals}
	for k in range(1, 1025):
		S2 = set()
		hasDup = False
		for s in S:
			x = s ^ k
			if x in S2:
				hasDup = True
				break
			else:
				S2.add(x)
		if not hasDup and S == S2:
			results.append(k)
			break
	else:
		results.append(-1)
for result in results:
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	f = 0
	for i in range(1, 1025):
		b = [0] * n
		for j in range(n):
			b[j] = a[j] ^ i
		b.sort()
		if a == b:
			f = 1
			print(i)
			break
	if f == 0:
		print(-1)


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

def readn():
	return int(input())
for _ in range(readn()):
	n = readn()
	nums = readintlst()
	if n ^ 1 == 1:
		print(-1)
	else:
		s = set(nums)
		for k in range(1, 1025):
			sn = {v ^ k for v in s}
			if sn == s:
				break
		else:
			print(-1)
			continue
		print(k)


for _ in range(int(input())):
	n = int(input())
	se = set(map(int, input().split()))
	i = 1
	while i < 1025:
		nw = set()
		for j in se:
			nw.add(j ^ i)
		if nw == se:
			break
		i += 1
	if se == nw:
		print(i)
	else:
		print(-1)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	k = -1
	for i in range(1, 1025):
		ns = set()
		for e in s:
			ns.add(i ^ e)
		if ns == s:
			k = i
			break
	print(k)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, ceil
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	n = iinp()
	arr = lmp()
	if max(arr) == 0:
		print(-1)
		continue
	mx = 1024
	flg = False
	sarr = sorted(arr)
	for i in range(1, mx + 1):
		tmp = [i ^ x for x in arr]
		if sorted(tmp) == sarr:
			flg = True
			break
	print(i if flg else -1)


for i in range(int(input())):
	po = int(input())
	l = set(map(int, input().split()))
	skl = set()
	res = 0
	de = 'art'
	bc = 1
	cchh = 1
	for j in range(1, 1026):
		skl = set()
		for k in l:
			skl.add(k ^ j)
		if skl == l:
			print(j)
			res = 1
			break
	if res == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, 1025):
		f = 0
		for j in a:
			if i ^ j not in a:
				f = 1
				break
		if f == 0:
			ans = i
			break
		else:
			ans = -1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	d = Counter(a)
	flag = 0
	for i in range(1, 1025):
		e = {}
		for j in range(n):
			b = a[j] ^ i
			e[b] = 1
		if e == d:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


for i in range(int(input())):
	n = int(input())
	s = set(map(int, input().split()))
	k = -1
	for j in range(1, 1025):
		ans = set()
		for h in s:
			ans.add(h ^ j)
		if ans == s:
			k = j
			break
	print(k)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 1025
	for i in a:
		cnt[i] += 1
	cntc = cnt.copy()
	imposs = False
	ans = 1
	for j in range(1, 1025):
		imposs = False
		cnt = cntc.copy()
		for i in a:
			if i ^ j <= 1024 and cnt[i ^ j]:
				cnt[i ^ j] -= 1
			else:
				imposs = True
				break
		if not imposs:
			ans = j
			break
	if imposs:
		print(-1)
	else:
		print(ans)


import sys
t = int(sys.stdin.readline())
while t:
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	sett = set()
	maxx = 0
	for x in arr:
		maxx = max(maxx, x)
		sett.add(x)
	k = 1
	flag = 0
	while k <= 2048 and flag == 0:
		count = 0
		if arr[0] ^ k in sett:
			for j in arr:
				if j ^ k in sett:
					count += 1
				else:
					break
		if count == n:
			print(k)
			flag = 1
			break
		k += 1
	if flag == 0:
		print(-1)
	t -= 1


import math
from sys import stdin, stdout
from heapq import heapify, heappop, heappush
T = int(stdin.readline())
for i in range(T):
	N = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	if len(a) == 1:
		print(-1)
		continue
	mx = max(a)
	if mx == 0:
		mx = 1
	mx = max(mx, int(math.ceil(math.pow(2, math.log2(mx) + 1))) - 1)
	new = set(a)
	f = 0
	for i in range(1, mx + 1):
		vis = set()
		for nm in a:
			if nm ^ i not in new:
				break
			vis.add(nm ^ i)
		if len(vis) == N:
			print(i)
			f = 1
			break
	if not f:
		print(-1)


from sys import stdin, stdout
from math import log2
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	hm = [True] * 1024
	a = list(map(int, stdin.readline().split()))
	for i in a:
		hm[i] = False
	ok = False
	for k in range(1, 1024):
		for x in a:
			if hm[x ^ k]:
				break
		if not hm[x ^ k]:
			ok = True
			break
	stdout.write(str(k) + '\n' if ok else '-1\n')


def check(l, k):
	for i in l:
		if i ^ k not in l:
			return False
	return True

def solve(l):
	for k in range(1, 1025):
		if check(l, k):
			print(k)
			return
	print('-1')
t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	solve(l)


import math
import copy
for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	f = 0
	if n % 2 != 0:
		print(-1)
		continue
	l.sort()
	b = copy.deepcopy(l)
	for i in range(1, 1025):
		for j in range(n):
			b[j] = l[j] ^ i
		b.sort()
		if l == b:
			f = 1
			print(i)
			break
	if not f:
		print(-1)


import sys
from collections import defaultdict as dd
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	for i in range(1, 1025):
		for j in range(n):
			if i ^ k[j] in k:
				r = i
			else:
				r = -1
				break
		if r != -1:
			break
	print(r)


for i in range(int(input())):
	n = int(input())
	k = [int(j) for j in input().split()]
	se = set(k)
	for d in range(1, 1088):
		s = set()
		for j in k:
			s.add(d ^ j)
		if se == s:
			print(d)
			break
	else:
		print('-1')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	flag = 0
	for j in range(1, 1025):
		count = 0
		r = []
		for x in range(n):
			p = l[x] ^ j
			r.append(p)
		r.sort()
		if r == l:
			flag = 1
			print(j)
			break
	if flag == 0:
		print('-1')


import sys
input = sys.stdin.readline
t = int(input())
import math
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	d = {}
	if n % 2 != 0:
		print(-1)
	else:
		for i in range(n):
			for j in range(n):
				temp = l[i] ^ l[j]
				d[temp] = d.get(temp, 0) + 1
		count = 100000000
		x = count
		for i in d:
			if d[i] == n and i != 0:
				count = min(count, i)
		if count == x:
			print(-1)
		else:
			print(count)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = False
	for j in range(1, 1024):
		l1 = l.copy()
		for k in range(n):
			l1[k] ^= j
		l1.sort()
		if l1 == l:
			print(j)
			ans = True
			break
	if ans == False:
		print(-1)


tc = int(input())
for _ in range(tc):
	n = int(input())
	l = list(map(int, input().split()))
	tmp = sorted([i ^ 1 for i in l])
	l.sort()
	ans = 1
	while tmp != l:
		ans += 1
		tmp = sorted([i ^ ans for i in l])
		if 1024 < ans:
			ans = -1
			break
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	x = set(l)
	f = 0
	for i in range(1, 1025):
		s = set()
		for j in l:
			s.add(i ^ j)
		if s == x:
			print(i)
			f = 1
			break
	if f == 0:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, 1025):
		res = []
		for items in a:
			b = items ^ i
			if b in a:
				res.append(b)
		if a.sort() == res.sort() and len(a) == len(res):
			print(i)
			break
		elif i == 1024:
			print('-1')
			break


def merge(arr, l, m, r):
	n1 = m - l + 1
	n2 = r - m
	L = [0] * n1
	R = [0] * n2
	for i in range(0, n1):
		L[i] = arr[l + i]
	for j in range(0, n2):
		R[j] = arr[m + 1 + j]
	i = 0
	j = 0
	k = l
	while i < n1 and j < n2:
		if L[i] <= R[j]:
			arr[k] = L[i]
			i += 1
		else:
			arr[k] = R[j]
			j += 1
		k += 1
	while i < n1:
		arr[k] = L[i]
		i += 1
		k += 1
	while j < n2:
		arr[k] = R[j]
		j += 1
		k += 1

def mergeSort(arr, l, r):
	if l < r:
		m = (l + (r - 1)) // 2
		mergeSort(arr, l, m)
		mergeSort(arr, m + 1, r)
		merge(arr, l, m, r)
t = int(input())
for i in range(t):
	n = int(input())
	num1 = list(map(int, input().split()))
	mergeSort(num1, 0, n - 1)
	a = 0
	for j in range(1, 1025):
		lis = []
		for k in range(n):
			lis.append(num1[k] ^ j)
		mergeSort(lis, 0, n - 1)
		if lis == num1:
			a = j
			break
	if a != 0:
		print(a)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	p = 0
	lis = []
	for i in range(1024 + 1):
		a = 0
		lis = []
		if i != 0:
			for j in arr:
				lis.append(i ^ j)
			arr.sort()
			lis.sort()
			if arr == lis:
				a = 1
				p = i
				break
		if a == 1:
			break
	if a == 1:
		print(p)
	else:
		print(-1)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	B = [0 for i in range(1025)]
	for val in A:
		B[val] = 1
	ans = -1
	for i in range(1, 1025):
		fa = i
		flag = 0
		for i in A:
			if i ^ fa > 1024 or B[i ^ fa] == 0:
				flag = 1
				break
		if flag == 0:
			ans = fa
			break
	print(ans)


def isPossible(li, x, d):
	d1 = {}
	for i in li:
		y = i ^ x
		if y in d1:
			return False
		else:
			d1[y] = 1
	for i in d:
		if i not in d1:
			return False
	return True
T = int(input())
for _ in range(T):
	n = int(input())
	li = list(map(int, input().split()))
	d = {}
	minn = 10000000000000000
	for i in li:
		d[i] = 1
	f = 0
	for i in range(1, len(li)):
		x = li[0] ^ li[i]
		if isPossible(li, x, d):
			if x < minn:
				minn = x
	if minn == 10000000000000000:
		print(-1)
	else:
		print(minn)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	ok = False
	for k in range(1, max(a) * 2 + 1):
		if ok == True:
			break
		s = set()
		for j in range(n):
			temp = a[j] ^ k
			if temp not in a:
				break
			s.add(temp)
			s.add(a[j])
			if j == n - 1 and s == set(a):
				print(k)
				ok = True
	if ok == True:
		continue
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	lst1 = [int(x) for x in input().split()]
	chk = False
	index = 0
	for i in range(1, 1025):
		lst2 = []
		for j in lst1:
			lst2.append(j ^ i)
		lst1.sort()
		lst2.sort()
		if lst1 == lst2:
			chk = True
			index = i
			break
		lst2.clear()
	if chk == True:
		print(i)
	else:
		print(-1)


def myXOR(x, y):
	return (x | y) & (~x | ~y)
for _ in range(int(input())):
	x = int(input())
	lt = list(map(int, input().split()))
	lt.sort()
	for n in range(1, 1025):
		mt = []
		for i in range(x):
			mt.append(myXOR(n, lt[i]))
		mt.sort()
		if mt == lt:
			print(n)
			break
	else:
		print('-1')


for T in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	mx = max(arr)
	save = -1
	for k in range(1, 1024 + 1):
		s2 = set()
		for i in arr:
			if i ^ k not in s:
				break
			else:
				s2.add(i ^ k)
		if s2 == s:
			save = k
			break
	print(save)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for j in range(1, 1025):
		z = [k ^ j for k in l]
		if set(z) == set(l):
			print(j)
			break
	else:
		print(-1)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	numbers = set([int(x) for x in stdin.readline().split()])
	for k in range(1, 1025):
		set2 = {x ^ k for x in numbers}
		ans = k
		if numbers == set2:
			break
	if ans == 1024:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	ans = -1
	for i in range(1, 1024):
		k = set()
		for j in range(n):
			k.add(a[j] ^ i)
		if s == k:
			ans = i
			break
	print(ans)


import math as mt
import sys, string
input = sys.stdin.readline
from collections import defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
t = I()
for _ in range(t):
	n = I()
	l = L()
	l.sort()
	k = l[0]
	x = []
	for i in range(n):
		if k ^ l[i] != 0:
			x.append(k ^ l[i])
	flag = 0
	for i in x:
		res = []
		for j in l:
			res.append(j ^ i)
		res.sort()
		if res == l:
			print(i)
			flag = 1
			break
	if flag == 0:
		print(-1)


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(-1)
		return
	xor = []
	for i in range(1, n):
		xor.append(arr[0] ^ arr[i])
	xor.sort()
	ans = set(arr)
	for num in xor:
		temp = []
		for i in range(n):
			temp.append(arr[i] ^ num)
		if len(temp) == n:
			if set(temp) == ans:
				print(num)
				return
	print(-1)
	return
t = int(input())
for _ in range(t):
	main()


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	st = set(a)
	f = 1
	for i in range(1, 1025):
		tmp = set()
		for it in st:
			tmp.add(it ^ i)
		if st == tmp:
			f = 0
			break
	if f:
		print(-1)
	else:
		print(i)


def find_answer():
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	for trial_number in range(1, 1024):
		ss = []
		for i in s:
			ss.append(i ^ trial_number)
		ss.sort()
		if ss == s:
			print(trial_number)
			return
	print(-1)
t = int(input())
for _ in range(t):
	find_answer()


from collections import defaultdict
for _ in range(int(input())):
	N = int(input())
	List = [int(x) for x in input().split()]
	Dict = defaultdict(bool)
	for i in List:
		Dict[i] = True
	p = 0
	for i in range(1, 1025):
		fl = 0
		for value in List:
			if Dict[i ^ value]:
				continue
			else:
				fl = 1
				break
		if not fl:
			print(i)
			p = 1
			break
	if not p:
		print(-1)


from sys import stdin, stdout
T = int(stdin.readline().strip())
for caso in range(T):
	n = int(stdin.readline().strip())
	s = set(map(int, stdin.readline().strip().split()))
	f = -1
	for i in range(1, 1025):
		s1 = set()
		for j in s:
			s1.add(j ^ i)
		if s1 == s:
			f = i
			break
	stdout.write('%d\n' % f)


t = int(input())
import math
delay = []
for _ in range(t):
	n = int(input())
	arr = input().split()
	arr = [int(a) for a in arr]
	res = 1
	found = False
	while res <= 1024:
		go = True
		result = [res ^ a for a in arr]
		dic = {}
		for r in result:
			if r in dic:
				dic[r] += 1
			else:
				dic[r] = 1
		for a in arr:
			if a not in dic:
				go = False
				break
			else:
				dic[a] -= 1
		if go:
			for k in dic:
				if dic[k] == 0:
					continue
				else:
					go = False
					break
		if go:
			print(res)
			found = True
			break
		res += 1
	if not found:
		print(-1)


for i in range(int(input())):
	a = input()
	s = list(map(int, input().rstrip().split()))
	z = []
	fi = -1
	for i in range(1, 1025):
		z = []
		for x in s:
			if x ^ i in s:
				z.append(x ^ i)
		if set(z) == set(s):
			break
	if i != 1024:
		print(i)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	k = -1
	for i in range(1, 1025):
		am = True
		for j in s:
			if j ^ i not in s:
				am = False
				break
		if am:
			k = i
			break
	print(k)


import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)

def main():
	ntc = next(ints)
	for tc in range(1, ntc + 1):
		n = next(ints)
		s = [next(ints) for i in range(n)]
		P = None
		for x in s:
			p = set((x ^ y for y in s))
			if P == None:
				P = p
			else:
				P &= p
		ans = next(iter(sorted(P)[1:]), -1)
		print(ans)
	return
main()


t = int(input())
for _ in range(t):
	n = int(input())
	values = list(map(int, input().split()))
	answer = [0] * (max(values) + 1)
	result = -1
	for i in range(1, 1024, 1):
		changed = [0] * (max(values) + 1)
		for (j, val) in enumerate(values):
			answer[val] = 1
			temp = i ^ val
			if temp <= max(values):
				changed[temp] = 1
		count = 0
		failed = False
		for (j, val) in enumerate(values):
			if answer[val] != changed[val]:
				failed = True
				break
		if not failed:
			result = i
			break
	print(result)


def yes(a, x):
	b = [0] * 1025
	for i in a:
		b[i ^ x] = 1
	for i in a:
		if b[i] == 0:
			return False
	return True
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	xx = a[0]
	if n % 2 == 1:
		print(-1)
	else:
		good = False
		ans = 10000
		for i in range(1, n):
			x = xx ^ a[i]
			if good:
				if x < ans and yes(a, x):
					ans = x
			elif yes(a, x):
				ans = x
				good = True
		if good:
			print(ans)
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	s = sorted(list(map(int, input().split())))
	if n % 2 == 1:
		print(-1)
	else:
		for i in range(1, n):
			dif = s[i] ^ s[0]
			for j in range(n):
				if s[j] ^ dif not in s:
					break
			else:
				print(dif)
				break
		else:
			print(-1)


def solution():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		l.sort()
		flag = 0
		for z in range(1, 1025):
			a = []
			for i in range(n):
				x = l[i] ^ z
				a.append(x)
			a.sort()
			if a == l:
				print(z)
				flag = 1
				break
		if flag == 0:
			print(-1)
solution()


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(nmbr()):
	n = nmbr()
	ans = -1
	s = set(lst())
	for k in range(1, 1025):
		new = set()
		for v in s:
			new.add(k ^ v)
		if s == new:
			ans = k
			break
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = set(map(int, input().split()))
	flag = True
	for i in range(1, 1025):
		new = set()
		for num in arr:
			new.add(num ^ i)
		if new == arr:
			print(i)
			flag = False
			break
	if flag:
		print(-1)


from sys import stdin, stdout
import sys

def equal(s1, s2):
	if len(s1) != len(s2):
		return False
	for el in s1:
		if el not in s2:
			return False
	return True

def main():
	t = 1
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		s = {int(i) for i in stdin.readline().split()}
		start = s.pop()
		bad = -1
		best = bad
		for number in s:
			k = start ^ number
			if k == 0:
				continue
			other_s = set()
			other_s.add(start ^ k)
			for el in s:
				other_s.add(el ^ k)
			s.add(start)
			if equal(s, other_s) and (k < best or best == bad):
				best = k
			s.remove(start)
		stdout.write(str(best) + '\n')
main()


T = int(input())
for t in range(T):
	n = int(input())
	S = list(map(int, input().strip().split(' ')))
	S = set(S)
	x = set()
	f = False
	for i in range(1, 1025):
		for a in S:
			x.add(a ^ i)
		if S == x:
			print(i)
			f = True
			break
		x = set()
	if not f:
		print(-1)


def answer():
	n = int(input())
	s = map(int, input().split())
	s = sorted(s)
	for i in range(1, 1025):
		s1 = []
		for j in s:
			s1.append(j ^ i)
		if s == sorted(s1):
			return i
	else:
		return -1
for _ in range(int(input())):
	print(answer())


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sl = set(l)
	k = -1
	for i in range(1, 1025):
		se = []
		for j in l:
			se.append(i ^ j)
		sse = set(se)
		if sl == sse:
			k = i
			break
	print(k)


T = int(input())

def findRang(n):
	p = 0
	a = 1
	if n == 0:
		return 3
	while n >= a:
		a = pow(2, p)
		p += 1
	return 2 ** (p - 1) - 1

def solve(arr):
	el_max = max(arr)
	el = findRang(el_max)
	for i in range(1, el + 1):
		count = 0
		for j in range(0, len(arr)):
			val = i ^ arr[j]
			if arr.count(val) > 0:
				count += 1
			else:
				break
		if count == len(arr):
			return i
	return -1

def solve2(arr):
	a = []
	for i in range(0, len(arr)):
		for j in range(i, len(arr) - 1):
			a.append(arr[i] ^ arr[j + 1])
	n = len(arr) // 2
	b = []
	for el in a:
		c = a.count(el)
		if c == n:
			b.append(el)
	print(a)
	print(b)
	if len(b) != 0:
		return min(b)
	else:
		return -1

def check(k, arr, ids):
	for j in range(0, len(arr)):
		val = k ^ arr[j]
		if ids[val] == False:
			return -1
	return k

def solve3(arr):
	el_max = 2047
	ids = []
	ret = False
	for i in range(0, el_max + 1):
		ids.append(False)
	for el in arr:
		ids[el] = True
	for k in range(1, el_max + 1):
		ret = check(k, arr, ids)
		if ret != -1:
			return ret
	return -1
while T > 0:
	n = int(input())
	arr = [int(el) for el in input().split(' ')]
	print(solve3(arr))
	T -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	x = set(a)
	f = 0
	for i in range(1, 1024 + 1):
		aa = a[:]
		for j in range(n):
			aa[j] = aa[j] ^ i
		y = set(aa)
		if y == x:
			print(i)
			f = -1
			break
	if f == 0:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	f = 0
	s = set(list1)
	for i in range(1, 1025):
		ss = set([])
		for j in range(n):
			ss.add(list1[j] ^ i)
		if ss == s:
			f = 1
			break
	if f == 1:
		print(i)
	else:
		print(-1)