You are given an array A consisting of N integers.

From array A, we create a new array B containing all pairwise bitwise ANDs of elements from A. That is, B consists of N\cdot(N-1)/2 elements, each of the form A_{i} \mathbin{\&} A_{j} for some 1 ≤ i < j ≤ N.

In array B, we repeat the following process till there is only one element remaining:
Let the current maximum and minimum element of array B be X and Y respectively.
Remove X and Y from array B and insert X \mathbin{|} Y into it.

Determine the last remaining element in array B.

Here, \mathbin{\&} denotes the [Bitwise AND operation] and \mathbin{|} denotes the [Bitwise OR operation].

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

- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.

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

For each test case, print a single line containing one integer — the last remaining element in array B.

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

$1 ≤ T ≤ 10^{4}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} ≤ 10^{9}$
- The sum of $N$ over all test cases does not exceed $2\cdot 10^{5}$.

----- Sample Input 1 ------ 
3
2
1 3
3
2 7 1
4
4 6 7 2
----- Sample Output 1 ------ 
1
3
6

----- explanation 1 ------ 
Test Case $1$: Array $B$ will be $[A_{1} \mathbin{\&} A_{2}] = [1 \mathbin{\&} 3] = [1]$. There is only one element in $B$ so the answer is $1$.

Test Case $2$: Array $B$ will be $[A_{1} \mathbin{\&} A_{2}, A_{1} \mathbin{\&} A_{3}, A_{2} \mathbin{\&} A_{3}] = [2 \mathbin{\&} 7, 2 \mathbin{\&} 1, 7 \mathbin{\&} 1] = [2, 0, 1]$.

Then, we do the following operations on $B$:
1. Remove $2$ and $0$ from $B$ and insert $2\mathbin{|} 0 = 2$ into it. Now, $B = [1, 2]$.
2. Remove $2$ and $1$ from $B$ and insert $2\mathbin{|} 1 = 3$ into it. Now, $B = [3]$.

The last remaining element is thus $3$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Constructive algorithms

Mathematics

Ad-hoc


Solutions:
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	l = [0] * 32
	for i in range(32):
		for j in a:
			if 1 << i & j:
				l[i] += 1
	s = 0
	for i in range(32):
		if l[i] >= 2:
			s |= 1 << i
	print(s)


from collections import defaultdict

def AndOrUnion(arr):
	d = defaultdict(lambda : 0)
	for i in arr:
		x = bin(i)[2:]
		for j in range(len(x)):
			if x[j] == '1':
				d[len(x) - j] += 1
	s = ''
	for j in range(1, 32):
		if d[j] >= 2:
			s = '1' + s
		else:
			s = '0' + s
	print(int(s, 2))
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	AndOrUnion(arr)


case = int(input())
for i in range(case):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	i = 30
	onebits = []
	while i > 0:
		count = 0
		for j in range(n):
			count += arr[j] & 1
			arr[j] = arr[j] >> 1
		onebits.append(count)
		i -= 1
	res = 0
	power = 0
	for counts in onebits:
		if counts >= 2:
			res += pow(2, power)
		power += 1
	print(res)


t = int(input())
while t > 0:
	n = int(input())
	lst = list(map(int, input().split()))
	or_lst = []
	initial_or = 0
	for i in range(n - 1, -1, -1):
		or_lst.insert(0, initial_or)
		initial_or |= lst[i]
	or_component = 0
	for i in range(n):
		temp = or_lst[i] & lst[i]
		or_component |= temp
	print(or_component)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	q = [0] * 32
	a = list(map(int, input().split()))
	for i in a:
		p = 0
		while i:
			q[p] += i & 1
			i >>= 1
			p += 1
	for i in range(32):
		if q[i] > 1:
			q[i] = '1'
		else:
			q[i] = '0'
	print(int(''.join(q[::-1]), 2))


for i in range(int(input())):
	N = int(input())
	arr = [int(x) for x in input().split()]
	brr = []
	dict = set()
	while len(arr) > 0:
		count = 0
		dict = sorted(dict)
		dict = reversed(dict)
		for i in dict:
			arr.pop(i)
		n = len(arr)
		dict = set()
		for i in range(len(arr)):
			if arr[i] == 0:
				dict.add(i)
			if arr[i] & 1 == 1:
				count += 1
			arr[i] = arr[i] >> 1
			if arr[i] == 0:
				dict.add(i)
		if count > 1:
			brr.append(1)
		else:
			brr.append(0)
	sum = 0
	k = 0
	for i in brr:
		sum += i * 2 ** k
		k += 1
	print(sum)


t = int(input())
while t:
	n = int(input())
	array = list(map(int, input().split(' ')[:n]))
	temp = [0] * 30
	for i in range(n):
		for j in range(30):
			if array[i] & 1 << j:
				temp[j] += 1
	ans = 0
	for i in range(30):
		if temp[i] > 1:
			ans |= 1 << i
	print(ans)
	t -= 1


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().strip().split()))
	b = [0 for i in range(30)]
	for i in range(n):
		for j in range(30):
			if a[i] & 1 << j:
				b[j] += 1
	ans = 0
	for i in range(30):
		if b[i] > 1:
			ans |= 1 << i
	print(ans)
	t -= 1


from math import log
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	f = [0] * 40
	p = 1
	for i in range(40):
		for j in range(n):
			num = l[j] & p
			if num == 0:
				continue
			x = int(log(num, 2))
			f[x] += 1
		p <<= 1
	ans = 0
	for i in range(40):
		if f[i] > 1:
			ans += 2 ** i
	print(ans)


mod = 1000000007
read_int = lambda : int(input().strip())
read_str = lambda : input().strip()
read_str_arr = lambda : input().strip().split()
read_int_arr = lambda : [int(x) for x in input().strip().split()]

def solve():
	N = read_int()
	A = read_int_arr()
	ans = 0
	or_acc = A[N - 1]
	for i in range(N - 2, -1, -1):
		ans |= A[i] & or_acc
		or_acc |= A[i]
	print(ans)
for _ in range(int(input())):
	solve()


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	for i in range(30):
		seen = 0
		for e in a:
			if e & 1 << i:
				if seen:
					res |= 1 << i
					break
				seen = 1
	print(res)


import math
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = [0] * 30
	for i in range(n):
		for j in range(30):
			if arr[i] & 1 << j:
				cnt[j] += 1
	ans = ''
	for i in cnt:
		if i > 1:
			ans += '1'
		else:
			ans += '0'
	print(int(ans[::-1], 2))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(ans, p) = (0, 1)
	for j in range(32):
		c = 0
		for k in range(n):
			if a[k] % 2 == 1:
				c += 1
			a[k] = a[k] // 2
		if c > 1:
			ans += p
		p *= 2
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	(ans, p) = (0, 1)
	for j in range(32):
		C = 0
		for i in range(N):
			if A[i] % 2 == 1:
				C += 1
			A[i] = A[i] // 2
		if C > 1:
			ans += p
		p *= 2
	print(ans)


t = int(input())
for l in range(t):
	n = int(input())
	a = [k for k in map(int, input().split())]
	b = [0 for i in range(32)]
	j = 0
	for k in range(0, n):
		z = 0
		for m in range(0, 31):
			if a[k] % 2 == 1:
				b[z] += 1
				z += 1
				a[k] = a[k] // 2
			else:
				a[k] = a[k] // 2
				z += 1
	ans = 0
	for e in b:
		if e > 1:
			ans = ans + 2 ** j
			j += 1
		else:
			j += 1
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	b = [0 for i in range(32)]
	for i in range(n):
		r = 0
		while a[i] > 0:
			if a[i] % 2:
				b[r] += 1
			a[i] = a[i] // 2
			r += 1
	ans = 0
	m = 0
	for i in range(32):
		if b[i] > 1:
			m = i
	for i in range(m, -1, -1):
		if b[i] > 1:
			ans = 2 * ans + 1
		else:
			ans = 2 * ans
	print(ans)
	t -= 1


for _ in range(int(input())):
	N = int(input())
	listx = list(map(int, input().split()))
	listy = [0] * 30
	for i in listx:
		for j in range(30):
			if i & 1 << j:
				listy[j] += 1
	ans = 0
	for k in range(30):
		if listy[k] > 1:
			ans |= 1 << k
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	l = []
	final = ''
	for k in A:
		l.append(format(k, '032b'))
	for j in range(32):
		cnt = 0
		for s in l:
			if s[j] == '1':
				cnt += 1
				if cnt == 2:
					break
		if cnt < 2:
			final += '0'
		else:
			final += '1'
	print(int(final, 2))


try:
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		matrix = []
		for i in arr:
			s = '0' * 30
			temp = bin(i).replace('0b', '')
			matrix.append(s[len(temp):] + temp)
		result = ['0'] * 30
		for i in range(30):
			cnt = 0
			for j in range(n):
				if matrix[j][i] == '1':
					cnt += 1
				if cnt > 1:
					result[i] = '1'
		print(int(''.join(result), 2))
except:
	pass


for _ in range(int(input())):
	n = int(input())
	X = [int(x) for x in input().split()]
	l = []
	for i in X:
		l.append(format(i, '032b'))
	ans = ''
	for i in range(32):
		count = 0
		for s in l:
			if s[i] == '1':
				count += 1
			if count >= 2:
				break
		if count >= 2:
			ans += '1'
		else:
			ans += '0'
	print(int(ans, 2))


for _ in range(int(input())):
	n = int(input())
	res = list(map(int, input().split()))
	vect = [0] * 30
	for i in res:
		s = bin(i)[2:]
		k = -1
		for j in range(len(s) - 1, -1, -1):
			if s[j] == '1':
				vect[k] += 1
			k -= 1
	final_result = ['0'] * 30
	for i in range(29, -1, -1):
		if vect[i] > 1:
			final_result[i] = '1'
	print(int(''.join(final_result), 2))


import math

def join(x, bits):
	X = x
	for i in range(0, 32):
		if X % 2 == 1:
			bits[i] += 1
		X //= 2

def solve():
	n = int(input())
	A = [int(x) for x in input().split()]
	bits = [0] * 32
	for i in range(0, len(A)):
		join(A[i], bits)
	ans = 0
	for i in range(0, 32):
		if bits[i] > 1:
			ans += 2 ** i
	print(ans)

def main():
	t = int(input())
	while t > 0:
		solve()
		t -= 1
main()


for t in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	byte_list = [0] * 32
	ans = 0
	for num in a:
		i = 0
		bit = num >> i
		while bit > 0:
			byte_list[i] = byte_list[i] + (bit & 1)
			i += 1
			bit = num >> i
	for (i, bit_count) in enumerate(byte_list):
		if bit_count > 1:
			ans = ans | 1 << i
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split(' ')))
	A = A[:n]
	li = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	for k in A:
		for j in range(31):
			if k & 1 << j:
				li[j] += 1
	ans = 0
	for j in range(31):
		if li[j] > 1:
			ans = ans | 1 << j
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = [0] * 30
	for i in range(n):
		for j in range(30):
			if l[i] & 1 << j:
				m[j] += 1
	ans = 0
	for i in range(30):
		if m[i] > 1:
			ans |= 1 << i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	from collections import deque
	ans = deque()
	d = {}
	for i in a:
		i = bin(i)[2:]
		for j in range(-1, -len(i) - 1, -1):
			if i[j] == '1':
				if -j in d:
					d[-j] += 1
				else:
					d[-j] = 1
			if i[j] == '0':
				if -j not in d:
					d[-j] = 0
	positions = d.items()
	positions = sorted(positions)
	for (indx, count) in positions:
		if count > 1:
			ans.appendleft('1')
		else:
			ans.appendleft('0')
	res = int(''.join(ans), 2)
	print(res)


for i in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	l = []
	m = 1
	for i in range(0, 32):
		count = 0
		for j in ar:
			if j >> i & m == 1:
				count += 1
			if count == 2:
				l.append(2)
				break
		else:
			l.append(1)
	s = 0
	for i in range(0, 32):
		if l[i] == 2:
			s = s + pow(2, i)
	print(s)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	nl = [0] * 32
	for i in range(n):
		for j in range(32):
			nl[j] += l[i] & 1 << j > 0
	ans = 0
	for i in range(32):
		if nl[i] >= 2:
			ans |= 1 << i
	print(ans)


def binary(l, num):
	j = 0
	while num != 0:
		rem = num % 2
		l[j] = l[j] + rem
		j += 1
		num //= 2
	return (l, num)
n = int(input())
for _ in range(n):
	l = [0] * 30
	x = int(input())
	y = list(map(int, input().split()))
	for i in range(x):
		binary(l, y[i])
	for i in range(30):
		if l[i] > 1:
			l[i] = 1
		else:
			l[i] = 0
	l.reverse()
	l2 = map(str, l)
	st = ''.join(map(str, l2))
	print(int(st, 2))


num = int(input())
while num:
	num -= 1
	length = int(input())
	a1 = input().split()
	if length == 2:
		print(int(a1[0]) & int(a1[1]))
		continue
	length = 0
	answer = ''
	for i in a1:
		length = max(length, len(bin(int(i))))
	for j in range(0, length):
		gotans = False
		is1present = False
		for i in range(0, len(a1)):
			temp = 1 << j
			if temp & int(a1[i]) != 0:
				if is1present:
					gotans = True
					break
				else:
					is1present = True
		if gotans:
			answer = '1' + answer
		else:
			answer = '0' + answer
	print(int(answer, 2))


T = int(input())
for tc in range(T):
	s = int(input())
	a = [int(x) for x in input().split()]
	bits = [0] * 30
	for n in a:
		idx = 0
		while n > 0:
			bits[idx] += n & 1
			n = n >> 1
			idx += 1
	r = 0
	for (idx, n) in enumerate(bits):
		if n >= 2:
			r += 2 ** idx
	print(r)


t = input()
t = int(t)
while t:
	n = input()
	n = int(n)
	a = [int(a) for a in input().split()]
	setBitCount = [0] * 30
	for num in a:
		for i in range(0, 30):
			if num & 1 << i:
				setBitCount[i] += 1
	ans = 0
	for i in range(30):
		if setBitCount[i] > 1:
			ans |= 1 << i
	print(ans)
	t -= 1


t = int(input())
while t:
	n = int(input())
	li = list(map(int, input().strip().split()))
	l = []
	di = {}
	ml = 0
	c = 0
	for i in range(32):
		di[i] = '0'
	for i in li:
		bs = bin(i)
		l.append(bs[::-1])
		lon = len(l[len(l) - 1])
		if ml < lon:
			ml = lon
	for i in range(ml):
		c = 0
		for j in range(len(l)):
			if i < len(l[j]) and l[j][i] == '1':
				c += 1
		if c > 1:
			di[i] = '1'
	ans = ''
	for i in range(32):
		ans += di[i]
	print(int(ans[::-1], 2))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	res = [0] * 32
	for i in arr:
		for j in range(32):
			res[j] += i & 1 << j > 0
	ans = 0
	for i in range(32):
		if res[i] > 1:
			ans |= 1 << i
	print(ans)


from collections import Counter
from math import ceil, floor, log, gcd
import bisect as bs
import sys
input = sys.stdin.readline
inp_lis = lambda : list(map(int, input().split()))
inp_multi = lambda : map(int, input().split())
inp_int = lambda : int(input().strip())

def binary(s):
	s = bin(s)
	s = s[2:]
	s = s[::-1]
	return s
for _ in range(int(input().strip())):
	n = inp_int()
	a = inp_lis()
	l = [0] * 32
	for each in a:
		s = binary(each)
		l_s = len(s)
		for i in range(l_s):
			if s[i] == '1' and l[i] != 2:
				l[i] += 1
	ans = 0
	for i in range(32):
		if l[i] == 2:
			ans += 2 ** i
	print(ans)


try:
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		s = 0
		for j in range(32):
			c = 0
			for i in range(n):
				if l[i] % 2 != 0:
					c += 1
				l[i] = l[i] // 2
			if c > 1:
				s = s + 2 ** j
		print(s)
except:
	pass


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	for i in range(n):
		a[i] = (30 - len(bin(a[i])[2:])) * '0' + bin(a[i])[2:]
	y = [0] * 30
	for i in range(30):
		for j in range(n):
			if a[j][i] == '1':
				y[i] += 1
	s = ''
	for i in range(30):
		if y[i] > 1:
			s += '1'
		else:
			s += '0'
	print(int(s, 2))


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	lenA = len(a)
	if lenA == 2:
		print(a[0] & a[1])
		continue
	acc = 0
	x = a[lenA - 1]
	for i in range(lenA - 2, -1, -1):
		acc = acc | a[i] & x
		x = x | a[i]
	print(acc)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 30
	for i in range(32):
		for j in range(n):
			if 1 << i & a[j]:
				b[i] += 1
	ans = 0
	for i in range(30):
		if b[i] > 1:
			ans += 2 ** i
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = arr[0]
	left = [arr[0]]
	for i in range(1, len(arr)):
		x = x | arr[i]
		left.append(x)
	right = [arr[-1]]
	x = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		x = x | arr[i]
		right.append(x)
	right = list(reversed(right))
	ans = 0
	for i in range(len(arr)):
		if i == 0:
			z = 0
		else:
			z = left[i - 1]
		if i + 1 == len(arr):
			z1 = 0
		else:
			z1 = right[i + 1]
		z = z | z1
		ans = ans | z & arr[i]
	print(ans)


t = int(input())
for t1 in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = [0] * 10000
	k = 0
	for i in a:
		j = i
		z = 0
		while j > 0:
			if j % 2 == 1:
				x[z] = x[z] + 1
			k = max(k, z)
			z = z + 1
			j = j // 2
	for i in range(k + 1):
		if x[i] >= 2:
			x[i] = 1
		else:
			x[i] = 0
	ans = 0
	for i in range(k + 1):
		if x[i] == 1:
			ans = ans + 2 ** i
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	bits = [0] * 32
	for i in range(n):
		cur = arr[i]
		for j in range(32):
			if cur & 1 << j:
				bits[j] += 1
	ans = 0
	for i in range(31, -1, -1):
		if bits[i] >= 2:
			ans += 2 ** i
	print(ans)


nn = int(input())
for i in range(nn):
	p = int(input())
	l = list(map(int, input().split()))
	o = 1
	a = 0
	for j in range(32):
		c = 0
		for k in range(p):
			if l[k] % 2 == 1:
				c += 1
			l[k] = l[k] // 2
		if c > 1:
			a += o
		else:
			pass
		o *= 2
	ppp = a
	print(ppp)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	bit = [0] * 32
	for i in range(n):
		for j in range(32):
			bit[j] += a[i] & 1 << j > 0
	a = 0
	for i in range(32):
		if bit[i] >= 2:
			a |= 1 << i
	print(a)


t = int(input())

def solve(ar):
	n = len(ar)
	out = [0] * 32
	for i in range(n):
		idx = 0
		tmp = ar[i]
		while tmp != 0:
			if tmp & 1 == 1:
				out[idx] += 1
			tmp = tmp >> 1
			idx += 1
	res = 0
	p = 0
	for i in out:
		if i > 1:
			res += 2 ** p
		p += 1
	return res
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	print(solve(x))


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	b = [0] * 30
	ct = 0
	for c in l:
		bi = format(c, '030b')
		for i in range(29, -1, -1):
			if bi[i] == '1':
				b[i] += 1
	op = '0b'
	for k in range(30):
		if b[k] > 1:
			op += '1'
		else:
			op += '0'
	print(int(op, 2))
	t -= 1


def to_find(n, ll):
	b = [0] * 30
	for i in range(n):
		for j in range(30):
			if ll[i] & 1 << j > 0:
				b[j] += 1
	res = 0
	for i in range(30):
		if b[i] >= 2:
			res = res | 1 << i
	return res
for i in range(int(input())):
	n = int(input())
	ll = list(map(int, input().split()))
	res = to_find(n, ll)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	for i in range(32):
		c = 0
		for x in a:
			if x & 1 << i:
				c += 1
			if c > 1:
				ans = ans | 1 << i
	print(ans)


try:
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		bits = []
		ans = ['0'] * 32
		for i in arr:
			temp1 = bin(i).replace('0b', '')
			temp2 = '0' * (32 - len(temp1))
			bits.append(temp2 + temp1)
		for i in range(32):
			cnt = 0
			for j in bits:
				if j[i] == '1':
					cnt += 1
				if cnt == 2:
					ans[i] = '1'
					break
		print(int(''.join(ans), 2))
except:
	pass


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().strip().split()))
	setcount = []
	for i in range(30):
		temp = 0
		setcount.append(temp)
	for i in range(n):
		for j in range(30):
			if arr[i] & 1 * 2 ** j:
				setcount[j] += 1
	answer = 0
	for i in range(30):
		if setcount[i] > 1:
			answer |= 1 * 2 ** i
	print(answer)
	T = T - 1


for i in range(int(input())):
	n = int(input())
	setbits = [0] * 32
	num = list(map(int, input().split()))[:n]
	for j in range(n):
		for k in range(32):
			if num[j] & 1 << k:
				setbits[k] += 1
	ans = 0
	for j in range(32):
		if setbits[j] > 1:
			ans |= 1 << j
	print(ans)


for case in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	arr = [0] * 31
	for val in a:
		for bit in range(0, 31):
			arr[bit] += bool(val & 1 << bit)
	out = 0
	for bit in range(0, 31):
		if arr[bit] > 1:
			out |= 1 << bit
	print(out)


def Binary(n):
	s = bin(n)
	s1 = s[2:]
	return s1

def binaryToDecimal(n):
	return int(n, 2)
for i in range(int(input())):
	N = int(input())
	elements = list(map(int, input().split()))
	newelements = [Binary(i) for i in elements]
	a = max(elements)
	a = Binary(a)
	a = len(a)
	for i in range(len(newelements)):
		while len(newelements[i]) != a:
			newelements[i] = '0' + newelements[i]

	def yes(i):
		count = 0
		for j in range(len(newelements)):
			if newelements[j][i] == '1':
				count += 1
		if count >= 2:
			return True
		else:
			return False
	ans = ''
	for i in range(a):
		if yes(i):
			ans = ans + '1'
		else:
			ans = ans + '0'
	print(binaryToDecimal(ans))


def main():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(lambda x: '{0:032b}'.format(int(x)), input().split()))
		ans = ['0' for _ in range(32)]
		for i in range(32):
			counter = 0
			for element in arr:
				if element[i] == '1':
					if counter == 0:
						counter += 1
					elif counter == 1:
						ans[i] = '1'
						break
		print(int(''.join(ans), 2))
main()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().split()))
	res = []
	for i in range(0, 32, 1):
		res.append(0)
	ans = 0
	for i in range(0, 32, 1):
		for j in range(n):
			if arr[j] % 2 == 1:
				res[i] = res[i] + 1
			else:
				res[i] = res[i]
			arr[j] = arr[j] // 2
	for i in range(0, len(res)):
		if res[i] > 1:
			ans = ans + 2 ** i
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	p = [int(i) for i in input().split()]
	l = []
	temp = 0
	for i in range(n - 1):
		temp = temp | p[n - i - 1]
		l.append(temp)
	temp = 0
	for i in range(n - 1):
		temp = temp | p[i] & l[n - i - 2]
	print(temp)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	ai = [0 for j in range(30)]
	for j in a:
		n = 0
		while n < 31:
			if j & 1 << 0 + n >= 1:
				ai[n] += 1
			n += 1
	for k in range(30):
		if ai[k] < 2:
			ai[k] = 0
		else:
			ai[k] = 1
	print(int(''.join(map(str, ai[::-1])), 2))
for _ in range(int(input())):
	main()


T = int(input())
for i in range(T):
	N = int(input())
	Arr = [int(i) for i in input().split()]
	pos = 1
	ans = 0
	for i in range(32):
		count = 0
		for j in range(len(Arr)):
			if Arr[j] % 2 == 1:
				count += 1
			Arr[j] = Arr[j] // 2
		if count > 1:
			ans = ans + pos
		pos = pos * 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	num_bits = [0] * 30
	res = 0
	for x in input().split():
		a = int(x)
		for j in range(len(num_bits)):
			if a & 1 << j:
				num_bits[j] += 1
				if num_bits[j] == 2:
					res += 2 ** j
	print(res)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	B = []
	maxlen = 0
	for each in A:
		binary = bin(each)[2:]
		B.append(binary)
		if len(binary) > maxlen:
			maxlen = len(binary)
	for i in range(len(B)):
		B[i] = '0' * (maxlen - len(B[i])) + B[i]
	d = dict()
	for i in range(maxlen):
		d[i] = 0
	final = ''
	for each in B:
		for i in range(maxlen):
			if each[i] == '1':
				d[i] += 1
	for i in d.values():
		if i > 1:
			final += '1'
		else:
			final += '0'
	print(int(final, 2))


t = int(input())
for i in range(t):
	n = int(input())
	li = input().split()
	for j in range(n):
		li[j] = int(li[j])
	m = [0] * 40
	for j in range(n):
		temp = bin(li[j])[2:]
		for k in range(len(temp)):
			m[-k - 1] = m[-k - 1] + int(temp[-k - 1])
	ans = 0
	for j in range(40):
		if m[j] > 1:
			ans = ans + 2 ** (40 - j - 1)
	print(ans)


def solve(ar):
	t = 0
	ans = 0
	for i in range(32):
		t = 0
		for x in ar:
			if x & 1 << i:
				t += 1
		if t > 1:
			ans |= 1 << i
	return ans
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve(l))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in range(30, -1, -1):
		d[i] = 0
	for bit in range(30, -1, -1):
		for j in arr:
			if 1 << bit & j:
				d[bit] += 1
	ans = 0
	for i in range(30, -1, -1):
		if d[i] > 1:
			ans += 1 << i
	print(ans)


t = int(input())
for tc in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	out = [0 for i in range(32)]
	for j in range(len(arr)):
		for i in range(32):
			k = arr[j] >> i
			if k & 1:
				out[i] += 1
	ans = 0
	for i in range(32):
		if out[i] > 1:
			ans += pow(2, i)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	res = 0
	for i in range(32, -1, -1):
		cnt = 0
		for j in range(n):
			if a[j] & 1 << i:
				cnt += 1
		if cnt > 1:
			res += pow(2, i)
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, list(input().split())))
	arr = [0] * 35
	for j in range(n):
		count = 0
		while l[j] > 0:
			x = l[j] & 1
			if x == 1:
				arr[count] = arr[count] + 1
			count = count + 1
			l[j] = l[j] >> 1
	ans = 0
	for i in range(len(arr)):
		if arr[i] > 1:
			ans = ans + pow(2, i)
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	o = 1
	a = 0
	for j in range(32):
		c = 0
		for k in range(n):
			if arr[k] % 2 == 1:
				c += 1
			arr[k] = arr[k] // 2
		if c > 1:
			a += o
		o *= 2
	print(a)


from functools import reduce

def till_or(arr):
	ele_operated = []
	till = None
	for x in reversed(arr):
		if not till:
			till = x
			continue
		ele_operated.append(x & till)
		till = till | x
	return ele_operated
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	individual_ored = till_or(arr)
	or_ans = reduce(lambda a, b: a | b, individual_ored)
	print(or_ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	or_Array = [0] * len(a)
	or_Array[n - 1] = a[n - 1]
	for i in range(n - 2, 0, -1):
		or_Array[i] = a[i] | or_Array[i + 1]
	res = 0
	for j in range(n - 2):
		res = res | a[j] & or_Array[j + 1]
	res = res | a[n - 1] & a[n - 2]
	print(res)


for t in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	suffixOR = [0]
	for i in range(len(A) - 1, 0, -1):
		suffixOR.append(suffixOR[-1] | A[i])
	suffixOR.reverse()
	ans = 0
	for i in range(N - 1):
		ans = ans | A[i] & suffixOR[i]
	print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split(' ')))
	B = []
	if n == 2:
		print(A[1] & A[0])
		continue
	for i in range(n - 1, 0, -1):
		if i == n - 1:
			B.append(A[i])
		else:
			B.append(B[-1] | A[i])
	ans = 0
	for i in range(n - 1):
		ans = ans | A[i] & B[-1]
		B.pop()
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ma = format(max(l), 'b')
	l1 = [0] * len(ma)
	i = 0
	for x in l:
		a = format(x, 'b')
		z = 0
		for y in a[::-1]:
			if y == '1':
				if l1[z] == 2:
					pass
				else:
					l1[z] += 1
			z += 1
	s = ''
	for a in l1[::-1]:
		if a <= 1:
			s += '0'
		else:
			s += '1'
	print(int(s, 2))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cn = 1
	ans = 0
	for j in range(32):
		c = 0
		for k in range(n):
			if arr[k] % 2:
				c += 1
			arr[k] = arr[k] // 2
		if c > 1:
			ans += cn
		cn *= 2
	print(ans)


T = int(input())
for i in range(T):
	N = int(input())
	arrA = [int(x) for x in input().split()]
	BinArr = [0] * 32
	for num in arrA:
		i = -1
		while i >= -32:
			if num % 2 == 1:
				BinArr[i] += 1
			num = num // 2
			i -= 1
	sum = 0
	for j in range(len(BinArr)):
		if BinArr[j] >= 2:
			sum += 2 ** (len(BinArr) - j - 1)
	print(sum)