Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef once had a deep epiphany and ended up saying: Given a sequence of positive integers $a_{1}, a_{2}, \ldots, a_{N}$, if you take each of its $2^{N}$ subsequences and write down the sum of elements of this subsequence, what will the bitwise OR of the written integers be?

Yes, you read it right, but can you solve it?

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

------  Output ------
For each test case, print a single line containing one integer ― the bitwise OR of sums of all subsequences.

------  Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 10^{5}$
$1 ≤ a_{i} < 2^{30}$ for each valid $i$

------  Subtasks ------
Subtask #1 (100 points): original constraints

----- Sample Input 1 ------ 
2

2

1 1

3

1 9 8
----- Sample Output 1 ------ 
3

27

Difficulty: HARD
Tags: Dynamic programming

Bit manipulation

Mathematics
Skill types: Dynamic programming

Bit manipulation

Starter code:

Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	r = 0
	for j in range(n):
		r |= a[j]
		sum += a[j]
		r |= sum
	print(r)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pref_sum = 0
	res = 0
	for i in range(n):
		res |= a[i]
		pref_sum += a[i]
		res |= pref_sum
	print(res)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	x = 0
	summ = 0
	for i in range(N):
		x = x | A[i]
		summ = summ + A[i]
		x = x | summ
	print(x)


for _ in range(int(input())):
	n = int(input())
	arr = [int(c) for c in input().split()]
	dp = [0 for i in range(50)]
	for i in range(50):
		count = 0
		for j in arr:
			if j & 1 << i:
				count += 1
		dp[i] = count + dp[i - 1] // 2
	ans = 0
	s = sum(arr)
	for i in range(50):
		if dp[i]:
			ans = ans | 1 << i
	print(ans)


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


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


import sys, math, itertools, random, bisect
from collections import defaultdict
INF = 10 ** 18

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

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

def input():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	pref_sum = 0
	res = 0
	for i in range(n):
		res |= a[i]
		pref_sum += a[i]
		res |= pref_sum
	print(res)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(sum, ans) = (0, 0)
	for i in range(n):
		ans |= arr[i]
		sum += arr[i]
		ans |= sum
	print(ans)


from math import log2
import bisect
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
inf = float('inf')

def get_no(l, k):
	suma = sum(l)
	if suma >= k:
		return 1
	return 0

def list_mod(l, k):
	return [i % k for i in l]
twopowers = [1]
while twopowers[-1] < pow(2, 60):
	twopowers.append(twopowers[-1] * 2)
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	answer = []
	for k in twopowers:
		m = list_mod(l, 2 * k)
		theta = get_no(m, k)
		answer.append(theta)
	answer.reverse()
	a = ''
	for i in answer:
		a += str(i)
	ans = int(a, 2)
	print(ans)


def abc(l, n, i, x):
	if i == n:
		return x
	return abc(l, n, i + 1, x) | abc(l, n, i + 1, x + l[i])
t = int(input())
import random
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = [0] * 64
	for i in range(n):
		c = '{0:031b}'.format(l[i])
		c = c[-1::-1]
		for j in range(len(c)):
			if c[j] == '1':
				ans[j] += 1
	for i in range(64):
		d = ans[i]
		ans[i] = 0
		if d:
			ans[i] = 1
			ans[i + 1] += d // 2
	k = 1
	e = 0
	for i in range(64):
		e += k * ans[i]
		k *= 2
	print(e)


import math

def check(N, a):
	su = 0
	for i in a:
		su = su + i
		if su >= 2 ** N and su < 2 ** (N + 1):
			return 0
	return -1

def binary(N):
	a = []
	while N != 0:
		a.append(N % 2)
		N = N // 2
	return a

def decimal(a):
	s = 0
	for i in range(len(a)):
		s = s + 2 ** i * a[i]
	return s
for i in range(int(input())):
	N = int(input())
	a = [int(x) for x in input().split()]
	s = sum(a)
	k = binary(s)
	count = 0
	su = 0
	for i in range(len(a)):
		su = su + a[i]
		count = count | su | a[i]
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list((int(x) for x in input().split()))
	res = 0
	x = 0
	for i in range(0, len(arr)):
		res += arr[i]
		x = x | res | arr[i]
	print(x)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	sum = 0
	s = 0
	b = []
	for i in range(len(a)):
		sum += a[i]
		b.append(sum)
	for i in range(len(b)):
		s = s | b[i] | a[i]
	print(s)


T = int(input())
for each in range(T):
	N = int(input())
	sm = 0
	mx = 0
	arr = map(int, input().split())
	for x in arr:
		mx += x
		sm = sm | x | mx
	print(sm)


from math import log2

def answer():
	x = int(log2(n * sum(a)))
	set_bit = [0] * (x + 2)
	ans = 0
	for bit in range(x + 1):
		for val in a:
			if val >> bit & 1:
				set_bit[bit] += 1
		set_bit[bit + 1] += set_bit[bit] // 2
		if set_bit[bit]:
			ans |= 1 << bit
	return ans
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(answer())


from math import log2
worst_bit = int(log2(10 ** 5 * 2 ** 30))

def answer():
	set_bit = [0] * (worst_bit + 2)
	ans = 0
	for bit in range(worst_bit + 1):
		for val in a:
			if val >> bit & 1:
				set_bit[bit] += 1
		set_bit[bit + 1] += set_bit[bit] // 2
		if set_bit[bit]:
			ans |= 1 << bit
	return ans
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(answer())


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	g = [0] * 75
	b = 1
	for j in range(n):
		b = 1
		c = 0
		while a[j] > 0:
			g[c] = g[c] + a[j] % 2 * b
			a[j] = a[j] // 2
			c = c + 1
			b = b * 2
	b = 1
	sumo = 0
	sum1 = 0
	for j in range(75):
		sumo = sumo + g[j]
		if sumo >= b:
			sum1 = sum1 + b
		b = b * 2
	print(sum1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sum1 = 0
	mx = 0
	for i in arr:
		sum1 += i
		mx = sum1 | i | mx
	print(mx)


from math import log2
for _ in range(int(input())):
	(n, a) = (int(input()), sorted(list(map(int, input().split()))))
	val = int(log2(pow(10, 5) * pow(2, 31)))
	(ans, old) = (0, 0)
	for i in range(0, val + 1):
		temp = 0
		for j in range(n):
			if a[j] & 1 << i:
				temp += 1
		if temp:
			ans += 1 << i
		elif old >= 1 << i:
			ans += 1 << i
		old += temp * (1 << i)
	print(ans)


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

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = [0] * 60
	s = 0
	for j in a:
		for k in range(30):
			if j & 1 << k:
				d[k] += 1
		s += j
	b = len(bin(s)) - 2
	l = ['0'] * b
	l[-1] = '1'
	for j in range(b - 1):
		c = 0
		cur = 0
		for k in range(j, -1, -1):
			cur += d[k]
			if cur >= 1 << c:
				l[j] = '1'
				break
			c += 1
			cur *= 2
	ans = ''.join(reversed(l))
	print(int(ans, 2))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	ans = 0
	count = 0
	for i in arr:
		ans = ans | i
		count = count + i
		ans = ans | count
	print(ans)


from math import log2, ceil, floor
maxBits = ceil(log2(10 ** 5 * 2 ** 31))
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	tot = 0
	ans = 0
	for num in arr:
		ans |= num
		tot += num
		ans |= tot
	print(ans)


from math import log2, ceil, floor
maxBits = ceil(log2(10 ** 5 * 2 ** 31))
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dp = [0 for i in range(maxBits + 1)]
	ans = 0
	for bit in range(maxBits + 1):
		c = 0
		for i in range(len(arr)):
			c += 1 if arr[i] & 1 << bit else 0
		dp[bit] = c
		if bit > 0:
			dp[bit] += dp[bit - 1] // 2
		if dp[bit]:
			ans += 2 ** bit
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = [int(x) for x in input().split()]
	res = 0
	tot = 0
	for a in A:
		tot = tot + a
		res = res | a
		res = res | tot
	print(res)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	finalOr = 0
	numOfBits = 0
	carryOverBits = 0
	for i in range(50):
		for j in l:
			temp = j
			temp = temp >> i
			numOfBits += temp & 1
		if numOfBits + carryOverBits >= 1:
			finalOr = finalOr | 1 << i
		carryOverBits = (numOfBits + carryOverBits) // 2
		numOfBits = 0
	print(finalOr)


T = int(input())
for _ in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	ans = 0
	Sum = 0
	for i in a:
		ans |= i
		Sum += i
		ans |= Sum
	print(ans)


for t in range(int(input())):
	n = int(input())
	sm = 0
	ans = 0
	lis = list(map(int, input().split()))
	for i in lis:
		ans |= i
		sm += i
		ans |= sm
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	ans = 0
	sum = 0
	for ele in arr:
		ans |= ele
		sum += ele
		ans |= sum
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	z = 0
	sm = 0
	for i in ls:
		sm += i
		z = z | i | sm
	print(z)


def log(a):
	c = 0
	while a > 0:
		a >>= 1
		c += 1
	return c

def sso(arr, n, brr, l):
	for i in range(l):
		count = 0
		for j in range(n):
			if arr[j] >> i & 1 == 1:
				count += 1
		if i == 0:
			brr[0] = count
		else:
			brr[i] = count + brr[i - 1] // 2
	res = 0
	for i in range(l):
		if brr[i] > 0:
			res += 1 << i
	return res
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	s = 0
	for i in arr:
		s += i
	l = log(s)
	brr = [0] * l
	print(sso(arr, n, brr, l))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	m = max(a).bit_length()
	i = ans = bits = 0
	while i < m or bits > 0:
		for j in range(n):
			if a[j] >> i & 1:
				bits += 1
		if bits > 0:
			ans |= 1 << i
		bits >>= 1
		i += 1
	print(ans)


t = int(input())
while t != 0:
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	sm = 0
	while n > 0:
		ans = ans | arr[n - 1]
		sm = sm + arr[n - 1]
		ans = ans | sm
		n -= 1
	print(ans)
	t -= 1


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	s = sum(a)
	m = 0
	while s > 0:
		s >>= 1
		m += 1
	ans = 0
	bits = 0
	for i in range(m):
		for j in range(n):
			if a[j] >> i & 1:
				bits += 1
		if bits > 0:
			ans |= 1 << i
		bits >>= 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	su = 0
	for i in a:
		ans |= i
		su += i
		ans |= su
	print(ans)


import sys

def main():
	test_cases = int(sys.stdin.readline())
	for _ in range(test_cases):
		N = int(sys.stdin.readline())
		A = [int(i) for i in sys.stdin.readline().split()]
		max_bit = 50
		bits_set = [0 for _ in range(max_bit)]
		for a in A:
			for b in range(max_bit):
				mask = 1 << b
				if a & mask:
					bits_set[b] += 1
		soln = 0
		for b in range(max_bit):
			if bits_set[b] > 0:
				mask = 1 << b
				carry = bits_set[b] // 2
				if b + 1 < max_bit:
					bits_set[b + 1] += carry
				soln |= mask
		print(soln)
main()


for _ in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	s = 0
	t = 0
	for i in l:
		t = t + i
		s = s | t | i
	print(s)


l1 = int(input())
for _ in range(l1):
	n = int(input())
	s = list(map(int, input().split()))
	count = 0
	a = []
	for i in s:
		a.append(count + i)
		count += i
	f = a[0]
	for i in range(n):
		f = f | a[i]
		f = f | s[i]
	print(f)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ct = ar[0]
	res = ar[0]
	for i in ar[1:]:
		ct += i
		res = res | ct | i
	print(res)


for i in range(int(input())):
	o = 0
	s = 0
	input()
	l = map(int, input().split())
	for j in l:
		s += j
		o = o | s | j
	print(o)


t = int(input())

def abc(l, n, i, x):
	if i == n:
		return x
	return abc(l, n, i + 1, x) | abc(l, n, i + 1, x + l[i])
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	l.sort()
	c = 0
	for i in l:
		c += i
		ans = ans | i
		ans = ans | c
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	ar = list(map(int, input().split()))
	for e in ar:
		ans |= e
	cur_sum = 0
	for e in ar:
		cur_sum += e
		ans |= cur_sum
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	h = 0
	for i in range(len(a)):
		x += a[i]
		h |= x | a[i]
	print(h)


for Jai_Shree_RAM in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = 0
	h = 0
	for i in arr:
		h += i
		s |= i | h
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	s = 0
	m = 0
	l = map(int, input().split())
	for i in l:
		s += i
		m = m | i | s
	print(m)


from sys import stdin as stin, stdout as stout, setrecursionlimit as srl

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

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

def get_int():
	return int(stin.readline())

def get_str():
	return stin.readline()
for _ in range(get_int()):
	n = get_int()
	nums = get_list()
	ans = 0
	s = 0
	for i in nums:
		ans |= i
		s += i
		ans |= s
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	ans = 0
	sum = 0
	for i in range(N):
		sum += A[i]
		ans = ans | sum | A[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sumi = sum(a)
	p = 0
	for i in range(0, len(a)):
		p += a[i]
		sumi = sumi | a[i] | p
	print(sumi)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	s = []
	ans = 0
	s = 0
	for x in a:
		s = s + x
		ans |= x
		ans |= s
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	curr = 0
	for i in a:
		curr += i
		ans |= i | curr
	print(ans)


from random import randint
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	tot = sum(arr)
	ans = tot
	for i in range(n):
		ans |= arr[i]
		ans |= tot - arr[i]
	arr.sort()
	current = 0
	for j in range(n):
		current += arr[j]
		ans |= current
	print(ans)


from itertools import accumulate
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	akk = list(accumulate(arr))
	ans = 0
	for i in range(n):
		ans = ans | akk[i] | arr[i]
	print(ans)


for _ in range(int(input())):
	N = int(input())
	temp = 0
	sum = 0
	arr = map(int, input().split())
	for x in arr:
		sum += x
		temp = temp | x | sum
	print(temp)


T = int(input())
for each in range(T):
	N = int(input())
	temp = 0
	sum = 0
	arr = map(int, input().split())
	for x in arr:
		sum += x
		temp = temp | x | sum
	print(temp)


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


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = sum(l)
	l.sort()
	m = [s - l[0]]
	for i in range(1, n):
		m.append(m[-1] - l[i])
	for i in range(n):
		s = s | m[i]
	for i in range(n):
		s = s | l[i]
	print(s)


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()
	s = [a[0]]
	for i in range(1, n):
		s.append(s[i - 1] + a[i])
	x = s[0]
	for i in range(1, n):
		x |= s[i]
	for i in range(n):
		x |= a[i]
	print(x)
for _ in range(I()):
	solve()


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


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	sm = 0
	smm = []
	for j in a:
		sm += j
		smm.append(sm)
	ans = 0
	for k in smm:
		ans = ans | k
	for k in a:
		ans = ans | k
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = A.copy()
	for i in range(1, n):
		A[i] = A[i] + A[i - 1]
	bitsum = 0
	for i in A:
		bitsum = bitsum | i
	for x in B:
		bitsum = bitsum | x
	print(bitsum)


def testcase():
	n = int(input())
	arr = list(map(int, input().split()))
	bf = [0 for _ in range(100)]
	bi = 0
	bv = 1
	mx = max(arr)
	while bv <= mx:
		f = 0
		for a in arr:
			if a & bv != 0:
				f += 1
		bf[bi] += f
		bi += 1
		bv <<= 1
	for i in range(1, 100):
		bf[i] += bf[i - 1] // 2
	bv = 1
	ans = 0
	for i in range(100):
		if bf[i] > 0:
			ans += bv
		bv <<= 1
	print(ans)
	return
import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t):
	testcase()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	l = a.copy()
	for i in range(n):
		s = s + a[i]
		l.append(s)
	k = l[0]
	for i in range(1, len(l)):
		k = k | l[i]
	print(k)


import math
from operator import ior
from functools import reduce
from itertools import combinations
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = [i for i in arr]
	some = 0
	for i in range(0, n):
		some += arr[i]
		lis.append(some)
	print(reduce(ior, lis))


def hello_bhai():
	for i in range(n):
		parameter = bin(hi[i])[2:]
		le = len(parameter)
		for j in range(le):
			good_night[j] += int(parameter[-j - 1])

def kaisa_hai():
	for i in range(100000):
		if good_night[i] > 1:
			byee(i)

def byee(i):
	good_night[i + 1] += good_night[i] // 2
	good_night[i] = 1
M = 100001
t = int(input())
for w in range(t):
	n = int(input())
	hi = list(map(int, input().split()))
	good_night = [0] * M
	hello_bhai()
	kaisa_hai()
	print(int('0b' + ''.join(list(map(str, good_night[::-1]))), 2))


from sys import stdin
input = stdin.readline
MAX = 60
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		done = set()
		for j in range(MAX):
			asd = 1 << j
			if asd >= a[i]:
				break
			x = a[i] % asd
			if x != 0 and x not in done:
				a.append(x)
				done.add(x)
	p = [0] * MAX
	for i in a:
		p[i.bit_length() - 1] += 1
	for i in range(1, MAX):
		p[i] += p[i - 1] >> 1
	s = 0
	for i in range(MAX):
		if p[i]:
			s += 1 << i
	print(s)


t = int(input())
for _ in range(t):
	bits = [0] * 50
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		j = l[i]
		index = 0
		while j > 0:
			val = j % 2
			bits[index] += val
			index += 1
			j //= 2
	ans = 0
	cur = 0
	for i in range(50):
		val = bits[i] + cur
		if val:
			ans += 2 ** i
		cur = val // 2
	print(ans)


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import re
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)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
for _ in range(I()):
	x = I()
	ls = lint()
	(s, p) = (0, 0)
	for i in ls:
		s += i
		p = s | p | i
	print(p)


import sys
import itertools as it
import math as mt
import bisect as bi
import collections as cc
import itertools as it
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	l.sort()
	f = cc.defaultdict(int)
	ans = 0
	prev = 0
	for i in range(0, 50):
		now = 0
		for k in range(n):
			if l[k] & 1 << i:
				now += 1
		if now:
			ans += pow(2, i)
		elif prev >= pow(2, i):
			ans += pow(2, i)
		prev += now * pow(2, i)
	print(ans)


I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	an = 0
	x = 1
	pr = 0
	for i in range(57):
		ct = 0
		for j in range(n):
			if x & l[j]:
				ct += 1
		if ct:
			an += x
		elif pr >= x:
			an += x
		pr += ct * x
		x *= 2
	print(an)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0 for x in range(30)]
	for i in range(n):
		x = a[i]
		j = 0
		while x > 0:
			b[j] = b[j] + x % 2
			x = x // 2
			j += 1
	k = 0
	rep = 1
	ans = 0
	for i in range(30):
		k = k // 2 + b[i]
		if k > 0:
			ans = ans + rep
		rep = rep * 2
	k = k // 2
	while k > 0:
		ans = ans + rep
		rep = rep * 2
		k = k // 2
	print(ans)


import sys, math
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))

def lcm(a, b):
	return a * b // math.gcd(a, b)
for _ in range(int(input())):
	(n,) = inp()
	a = list(map(int, input().split()))
	ans = [0 for i in range(47)]
	for i in range(n):
		s = bin(a[i]).replace('0b', '')
		s = s[::-1]
		for j in range(len(s)):
			if s[j] == '1':
				ans[j] += 1
	res = [0 for i in range(47)]
	for i in range(47):
		if ans[i] > 0:
			res[i] = 1
		if i < 46:
			ans[i + 1] += ans[i] // 2
	new = ''.join((str(i) for i in res))
	print(int(new[::-1], 2))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l1 = [int(i) for i in input().split(' ')]
	l2 = []
	k = 0
	for i in l1:
		k += i
		l2.append(k)
	ans = 0
	for i in range(n):
		ans = ans | l2[i]
		ans = ans | l1[i]
	print(ans)


for tc in range(int(input())):
	n = int(input())
	li = list(map(int, input().strip().split()))
	a = [0] * n
	a[0] = li[0]
	for i in range(1, n):
		a[i] = a[i - 1] + li[i]
	c = 0
	for i in range(n):
		c = c | a[i]
		c = c | li[i]
	print(c)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	num = [0] * 100000
	for i in l:
		x = bin(i)[2:]
		for j in range(len(x)):
			num[j] += int(x[-j - 1])
	for i in range(100000):
		if num[i] > 1:
			num[i + 1] += num[i] // 2
			num[i] = 1
	print(int('0b' + ''.join(list(map(str, num[::-1]))), 2))


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(a)
	ans = s
	prefsum = 0
	for i in range(n):
		prefsum += a[i]
		ans = ans | prefsum
		ans = ans | a[i]
	print(ans)


from sys import stdin, stdout
from collections import defaultdict, deque
T = int(stdin.readline())
for t in range(T):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	z = 0
	ans = 0
	for i in range(n):
		z += l[i]
		ans |= l[i]
		ans |= z
	print(ans)