NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to $\operatorname{or} z$. Can you solve this problem too?

You are given a 1-indexed array of $n$ integers, $a$, and an integer $z$. You can do the following operation any number (possibly zero) of times:

Select a positive integer $i$ such that $1\le i\le n$. Then, simutaneously set $a_i$ to $(a_i\operatorname{or} z)$ and set $z$ to $(a_i\operatorname{and} z)$. In other words, let $x$ and $y$ respectively be the current values of $a_i$ and $z$. Then set $a_i$ to $(x\operatorname{or}y)$ and set $z$ to $(x\operatorname{and}y)$.

Here $\operatorname{or}$ and $\operatorname{and}$ denote the bitwise operations OR and AND respectively.

Find the maximum possible value of the maximum value in $a$ after any number (possibly zero) of operations.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.

The first line of each test case contains two integers $n$ and $z$ ($1\le n\le 2000$, $0\le z<2^{30}$).

The second line of each test case contains $n$ integers $a_1$,$a_2$,$\ldots$,$a_n$ ($0\le a_i<2^{30}$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.


-----Output-----

For each test case, print one integer — the answer to the problem.


-----Examples-----

Input
5
2 3
3 4
5 5
0 2 4 6 8
1 9
10
5 7
7 15 30 29 27
3 39548743
10293834 10284344 13635445
Output
7
13
11
31
48234367


-----Note-----

In the first test case of the sample, one optimal sequence of operations is:

Do the operation with $i=1$. Now $a_1$ becomes $(3\operatorname{or}3)=3$ and $z$ becomes $(3\operatorname{and}3)=3$.

Do the operation with $i=2$. Now $a_2$ becomes $(4\operatorname{or}3)=7$ and $z$ becomes $(4\operatorname{and}3)=0$.

Do the operation with $i=1$. Now $a_1$ becomes $(3\operatorname{or}0)=3$ and $z$ becomes $(3\operatorname{and}0)=0$.

After these operations, the sequence $a$ becomes $[3,7]$, and the maximum value in it is $7$. We can prove that the maximum value in $a$ can never exceed $7$, so the answer is $7$.

In the fourth test case of the sample, one optimal sequence of operations is:

Do the operation with $i=1$. Now $a_1$ becomes $(7\operatorname{or}7)=7$ and $z$ becomes $(7\operatorname{and}7)=7$.

Do the operation with $i=3$. Now $a_3$ becomes $(30\operatorname{or}7)=31$ and $z$ becomes $(30\operatorname{and}7)=6$.

Do the operation with $i=5$. Now $a_5$ becomes $(27\operatorname{or}6)=31$ and $z$ becomes $(27\operatorname{and}6)=2$.

Source: codeforces | Difficulty: EASY
Tags: Bit manipulation

Greedy algorithms


Solutions:
for _ in range(int(input())):
	(n, z) = map(int, input().split())
	l1 = list(map(int, input().split()))
	for i in range(n):
		l1[i] = l1[i] | z
	print(max(l1))


for _ in range(int(input())):
	(q, z) = map(int, input().split())
	print(max((x | z for x in list(map(int, input().split())))))


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	print(max((x | z for x in a)))


t = int(input())
while t:
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	res = a[0]
	for ele in a:
		res = max(res, z | ele)
	print(res)
	t -= 1


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, z) = map(int, input().split())
	lst = list(map(int, input().split()))
	lst1 = []
	for j in lst:
		lst1.append(j | z)
	print(max(lst1))


def main():
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	mx = 0
	for i in range(n):
		mx = max(mx, arr[i] | z)
	return mx
for _ in range(int(input())):
	print(main())


t = int(input())
for _ in range(t):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	k = -float('inf')
	for i in range(n):
		k = max(k, arr[i] | z)
	print(k)


from collections import *
from functools import lru_cache

def readline():
	s = input()
	return s.split(' ')

def readlineint():
	return [*map(int, readline())]

def readint():
	s = input()
	return int(s)

def testcase():
	[N, z] = readlineint()
	t = readlineint()
	m = t[0] | z
	for i in range(1, N):
		m = max(m, t[i] | z)
	print(m)
N = readint()
for _ in range(N):
	testcase()


import sys
input = sys.stdin.readline

def solve():
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = max(arr)
	for i in range(n):
		ans = max(ans, arr[i] | z)
	return ans
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	arr = (*map(int, input().split()),)
	print(max((num | z for num in arr)))


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


t = int(input())
for tt in range(t):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	m = a[0] | z
	for i in range(n):
		m = max(m, a[i] | z)
	print(m)


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


from os import error
import sys
input = sys.stdin.readline
DEBUG = False

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

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

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

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

def solve():
	(n, z) = inlt()
	datum = inlt()
	return max([z | d for d in datum])
q = inp()
for t in range(q):
	ans = solve()
	print(ans)


for time in range(int(input())):
	(n, z) = map(int, input().split())
	nums = input().split()
	print(max((int(a) | z for a in nums)))


t = int(input())
for i in range(t):
	(n, z) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = []
	for i in l:
		ans.append(i | z)
	print(max(ans))


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	arr = list(map(int, input().split()))
	brr = []
	for j in range(a):
		brr.append(arr[j] | b)
		brr.append(arr[j] & b)
	print(max(brr))


class NITorz:

	def findMax(self, n, z, a):
		result = z | a[0]
		for i in range(n):
			result = max(result, z | a[i])
		print(result)
		return
T = int(input())
while T:
	T -= 1
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	NITorz().findMax(n, z, a)


I = lambda : [*map(int, input().split())]
for _ in ' ' * I()[0]:
	(_, z) = I()
	print(max((x | z for x in I())))


def solve():
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	m = max(arr)
	for i in range(len(arr)):
		arr[i] = arr[i] | z
	m2 = max(arr)
	print(max(m, m2))
	return
for i in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	maxy = 0
	for thing in a:
		if thing | z > maxy:
			maxy = thing | z
	print(maxy)


x = int(input())
for i in range(x):
	A = input().split()
	n = int(A[0])
	z = int(A[1])
	B = input().split()
	C = [0] * n
	for j in range(n):
		C[j] = int(B[j])
	max = C[0]
	for k in range(n):
		if z | C[k] > max:
			max = z | C[k]
	print(max)


T = int(input())
for i in range(T):
	(N, M) = map(int, input().split())
	K = list(map(int, input().split()))
	Maxx = 0
	for i in range(N):
		Maxx = max(Maxx, K[i] | M)
	print(Maxx)


tc = int(input())
for t in range(tc):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	maximum = -1
	for i in range(n):
		maximum = max(maximum, a[i] | z)
	print(maximum)


m = int(input())
for i in range(m):
	c = input().split(' ')[1]
	c = int(c)
	d = input().split(' ')
	d = [int(k) for k in d]
	d = [o | c for o in d]
	print(max(d))


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	ans = 0
	for i in arr:
		ans = max(ans, i | m)
	print(ans)


t = int(input())
for i in range(t):
	new_list = []
	(n, z) = list(map(int, input().split(' ')))
	nums = list(map(int, input().split(' ')))
	for num in nums:
		new_list.append(num | z)
	old_max = max(nums)
	new_max = max(new_list)
	print(max(old_max, new_max))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	l = 0
	for i in a:
		temp = i | k
		l = max(l, temp)
	print(l)


import sys
if sys.argv[0][6:10] == 'arun':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
test = int(input())
for rang in range(test):
	(n, z) = map(int, input().split())
	li = list(map(int, input().split()))
	li = [z | p for p in li]
	print(max(li))


t = int(input())
while t > 0:
	t = t - 1
	(n, z) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	ma = -1
	for i in l:
		ma = max(i | z, ma)
	print(ma)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	arrn = []
	for i in arr:
		arrn.append(i | x)
	print(max(arrn))


import math
t = int(input())
while t:
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = l[0] | m
	for i in range(1, n):
		ans = max(ans, l[i] | m)
	print(ans)
	t -= 1


def solution(n, z, arr):
	res = 0
	for num in arr:
		res = max(res, z | num)
	return res
T = int(input())
for _ in range(T):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(solution(n, z, arr))


t = int(input())
for _ in range(t):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	mele = max(arr)
	narr = list(map(lambda x: x | z, arr))
	mele2 = max(narr)
	print(max(mele, mele2))


import sys
import collections
from math import ceil, gcd, sqrt, log
import bisect
INF = float('inf')
mod = 1000000007

def solve():
	(n, z) = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort(reverse=True)
	maxi = 0
	for i in A:
		maxi = max(maxi, i | z)
	print(maxi)
t = int(input())
while t != 0:
	solve()
	t -= 1


for i in range(int(input())):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	maxv = 0
	for x in a:
		if x | z > maxv:
			maxv = x | z
	print(maxv)


for i in range(int(input())):
	(a, b) = map(int, input().strip().split())
	l = list(map(int, input().strip().split()))
	x = 0
	for i in l:
		if i | b > x:
			x = i | b
	print(x)


t = int(input())
for x in range(t):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	mx = 0
	for i in range(n):
		a[i] |= z
	print(max(a))


tc = int(input())
for t in range(tc):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	mx = 0
	for j in range(n):
		cst = arr[j] | k
		if cst > mx:
			mx = cst
	print(mx)


test_case = int(input())
for l in range(test_case):
	n_z = [int(i) for i in input().split()]
	z = n_z[1]
	arr = [int(i) for i in input().split()]
	for i in range(n_z[0]):
		temp = arr[i]
		arr[i] = temp | z
	maxx = max(arr)
	print(maxx)


def solve(A, z):
	return max([x | z for x in A])

def read():
	A = []
	line = input()
	ss = line.split(' ')
	n = int(ss[0])
	z = int(ss[1])
	line = input()
	ss = line.split(' ')
	for s in ss:
		A.append(int(s))
	return (A, z)

def main():
	n = int(input())
	for _ in range(n):
		(A, z) = read()
		print(solve(A, z))
	pass
main()


import math
t = int(input())
while t:
	t -= 1
	(h, i) = input().split()
	a = int(h)
	b = int(i)
	lis = input().split()
	lis = [int(x) for x in lis]
	print(max([i | b for i in lis]))


t = int(input())
while t > 0:
	inp = input().split()
	n = int(inp[0])
	z = int(inp[1])
	inp = input().split()
	l = []
	for i in range(len(inp)):
		inp[i] = int(inp[i])
		l.append(inp[i] | z)
	m = max(l)
	ind = l.index(m)
	m = inp[ind]
	u = -1
	p = -1
	while z > 0 and u != m:
		u = m
		m = m | z
		p = z
		z = u & z
	print(m)
	t -= 1


for i in range(int(input())):
	in_ = input().split()
	n = int(in_[0])
	z = int(in_[1])
	a = list(map(int, input().split()))
	max_ = 0
	for j in a:
		if j | z > max_:
			max_ = j | z
	print(max_)


t = int(input())
for i in range(t):
	nums = input().split()
	n = int(nums[0])
	z = int(nums[1])
	nums = list(map(int, input().split()))
	maxtrue = True
	currMax = max(nums)
	for value in nums:
		if value | z > currMax:
			currMax = value | z
	print(currMax)


def max_val_a(a_list, z_val, n):
	max_a = 0
	z_list = [z_val]
	for a in a_list:
		for z in z_list:
			a_or_z = a | z
			a_and_z = a & z
			if a_or_z > max_a:
				max_a = a_or_z
			if a_and_z != z and z not in z_list:
				z_list.append(a_and_z)
	return max_a
t = int(input())
while t:
	t -= 1
	(n, z) = map(int, input().split())
	a_list = list(map(int, input().split()))
	ans = max_val_a(a_list, z, n)
	print(ans)


for i in range(int(input())):
	(n, a) = map(int, input().split(' '))
	lis = list(map(int, input().split(' ')))
	ans = 0
	for j in lis:
		ans1 = a | j
		ans = max(ans, ans1)
	print(ans)


t = int(input())
ans = []
for e in range(t):
	(n, z) = map(int, input().split())
	a = max(map(lambda x: x | z, map(int, input().split())))
	ans.append(a)
print('\n'.join(map(str, ans)))


def get_max(a, z):
	max_value = max(a)
	for e in a:
		max_value = max(max_value, e | z)
	return max_value
t = int(input())
for _ in range(t):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	print(get_max(a, z))


def solve():
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	mx = 0
	for i in range(n):
		a[i] = a[i] | z
	for i in range(n):
		mx = max(mx, a[i])
	print(mx)

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


amount_data = int(input())
for i in range(amount_data):
	(n, z) = list(map(int, input().split()))
	array = list(map(int, input().split()))
	max_num = max([z | num for num in array])
	print(max_num)


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	lst = list(map(int, input().split()))
	l = []
	for i in lst:
		l.append(z | i)
	print(max(l))


n = int(input())
for i in range(n):
	(b, z) = input().split()
	b = int(b)
	z = int(z)
	a = list(map(int, input().split()))
	ans = 0
	for j in range(b):
		ans = max(ans, a[j] | z)
	print(ans)


t = int(input())
for _ in range(t):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(max((arr[i] | z for i in range(n))))


def solution(n, z, a):
	for i in range(n):
		x = a[i]
		y = z
		a[i] = x | y
	return max(a)
T = int(input())
for _ in range(T):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	out_ = solution(n, z, a)
	print(out_)


for _ in range(int(input())):
	(n, z) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	ans = -float('inf')
	for i in arr:
		ans = max(ans, i | z)
	print(ans)


for i in range(int(input())):
	(n, z) = map(int, input().split())
	l = sorted(list(map(int, input().split())))
	d = {item: item | z for item in l}
	ans = max(d.values())
	key = list(d.keys())[list(d.values()).index(ans)]
	nz = key & z
	nans = nz | d[key]
	print(max(nans, ans))


for _ in range(int(input())):
	(n, z) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	new_li = []
	for i in arr:
		new_li.append(i | z)
	print(max(new_li))


T = int(input())
while T:
	T -= 1
	(N, z) = map(int, input().split())
	li = list(map(int, input().split()))
	mx = -float('inf')
	for ele in li:
		mx = max(mx, ele | z)
	print(mx)


t = int(input())
while t:
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = a[i] | z
	print(max(a))
	t -= 1


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	a = [*map(int, input().split())]
	ans = 0
	for x in a:
		ans = max(ans, x | z)
	print(ans)


n = int(input())
for _ in range(n):
	(N, z) = tuple(map(int, input().split(' ')))
	List = list(map(int, input().split(' ')))
	List1 = []
	for i in range(N):
		List1.append(List[i] | z)
	print(max(List1))


def orz(arr, z, prev_max):
	max_value = -1
	i = -1
	for (idx, val) in enumerate(arr):
		if val | z > max_value:
			max_value = val | z
			i = idx
	z &= arr[i]
	arr[i] = max_value
	if max_value > prev_max:
		return orz(arr, z, max_value)
	else:
		return max_value

def main():
	t = int(input())
	for _ in range(t):
		(n, z) = list(map(int, input().split()))
		a = list(map(int, input().split()))
		print(orz(a, z, -1))
main()


testcases = int(input())
for j in range(testcases):
	(n, x) = [int(x) for x in input().split(' ')]
	l = [int(x) for x in input().split(' ')]
	maximum = 0
	for i in range(len(l)):
		maximum = max(maximum, l[i] | x)
	print(maximum)


t = int(input())
for x in range(t):
	(n, z) = [int(x) for x in input().split()]
	arr = [int(x) | z for x in input().split()]
	p = max(arr)
	print(p)


res = []
for _ in range(int(input())):
	(n, z) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	maxEl = a[0] | z
	for i in range(1, len(a)):
		maxEl = max(maxEl, a[i] | z)
	res.append(maxEl)
for e in res:
	print(e)


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	t = float('-inf')
	for i in range(len(a)):
		if a[i] | z > t:
			t = a[i] | z
	print(t)


x = int(input())
v = []
c = []
for _ in range(x):
	(n, z) = map(int, input().split())
	v = list(map(int, input().split()))
	ans = max(v)
	for i in range(n):
		if ans < v[i] | z:
			ans = v[i] | z
	c.append(ans)
for i in range(len(c)):
	print(c[i])


t = int(input())
for trials in range(t):
	(n, z) = input().split()
	arr = list(map(int, input().split()))
	n = int(n)
	z = int(z)
	curr_max = 0
	for i in arr:
		curr_max = max(curr_max, i | z)
	print(curr_max)


import sys
import math

def gt():
	return map(int, sys.stdin.readline().strip().split())
t = int(input())
while t > 0:
	(n, a) = gt()
	x = list(map(int, input().split()))
	mn = 0
	for i in range(n):
		mn = max(mn, x[i] | a)
	print(mn)
	t -= 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	list1 = [int(i) for i in input().split()]
	max1 = max(list1)
	list2 = [int(i) | m for i in list1]
	max2 = max(list2)
	print(max(max1, max2))


t = int(input())
max_list = []
for _ in range(t):
	(n, z) = map(int, input().split())
	lst = list(map(int, input().split()))
	new = []
	for i in range(n):
		new.append(lst[i] | z)
	max_list.append(max(new))
for m in max_list:
	print(m)


n = int(input())
while n:
	(length, number) = map(int, input().split(' '))
	array = list(map(int, input().split(' ')))
	maxi = max(array)
	for i in range(length):
		if array[i] | number > maxi:
			maxi = array[i] | number
	print(maxi)
	n -= 1


t = int(input())
for i in range(t):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	result = 0
	for i in arr:
		result = max(result, i | z)
	print(result)


n = int(input())
for _ in range(n):
	l = list(map(int, input().split()))
	t = l[0]
	z = l[1]
	s = list(map(int, input().split()))
	ans = 0
	for i in range(t):
		x = s[i] | z
		if x > ans:
			ans = x
	print(ans)


t = int(input())
while t:
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = arr[0] | z
	for i in range(1, n):
		ans = max(ans, arr[i] | z)
	print(ans)
	t -= 1


import math

def func():
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = z
	for num in arr:
		ans = max(ans, num | z)
	print(ans)
tc = int(input())
while tc > 0:
	func()
	tc -= 1


try:
	t = int(input())
	for i in range(t):
		(n, z) = map(int, input().split())
		nums = list(map(int, input().split()))[:n]
		for i in range(len(nums)):
			nums[i] |= z
		print(max(nums))
except:
	pass


count = int(input())

def algoth(a, z):
	a = [x | z for x in a]
	a.sort()
	return a[-1]
bar = []
for x in range(count):
	z = input()
	z = z.split()
	z = z[1]
	z = int(z)
	a = input()
	a = a.split()
	a = list(map(int, a))
	ans = algoth(a, z)
	bar.append(ans)
for j in bar:
	print(j)


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = []
	for b in a:
		ans.append(z | b)
	print(max(ans))


T = int(input())
for t in range(T):
	(n, z) = map(int, input().split())
	arr = list(map(int, input().split()))
	res = ['0'] * 32
	best = 0
	for v in arr:
		best = max(best, v | z)
	print(best)


t = int(input())
for k in range(t):
	(a, z) = map(int, input().split())
	user = input()
	user = user.split()
	for i in range(len(user)):
		user[i] = int(user[i])
	ans = -1
	for i in user:
		ans = max(ans, i | z)
	print(ans)


t = int(input())
for _ in range(t):
	(n, z) = map(int, input().split())
	A = list(map(int, input().split()))
	for i in range(n):
		A[i] = A[i] | z
	mx = A[0]
	for i in range(n):
		if A[i] > mx:
			mx = A[i]
	print(mx)


for _ in range(int(input())):
	(x, y) = map(int, input().split(' '))
	n = list(map(int, input().split(' ')))
	z = []
	for i in n:
		z.append(i | y)
		z.append(i & y)
	print(max(z))


import sys
input1 = sys.stdin.readline

def inp():
	return int(input1())
t = inp()
for _ in range(t):
	input2 = sys.stdin.readline
	input3 = sys.stdin.readline

	def inlt(input):
		return list(map(int, input().split()))
	(n, z) = inlt(input2)
	nums = inlt(input3)
	res = 0
	for p in nums:
		res = max(res, p | z)
	print(res)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	li = list(map(int, input().split()))
	maxi = 0
	for ele in li:
		maxi = max(maxi, ele | x)
	print(maxi)


T = int(input())
for test in range(T):
	(n, z) = [int(a) for a in input().split()]
	a = list(map(int, input().split()))
	maxi = 0
	for i in a:
		i = i | z
		z = i & z
		maxi = max(maxi, i)
	print(maxi)


for _ in range(int(input())):
	(n, z) = map(int, input().split())
	(*a,) = map(int, input().split())
	print(max((z | a[i] for i in range(n))))


n = int(input())
for i in range(n):
	(x, z) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	for m in range(x):
		l[m] = l[m] | z
	print(max(l))


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
t = int(input())

def biwise_and(a, b):
	return a & b

def biwise_or(a, b):
	return a | b
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	mx = 0
	for i in range(n):
		if biwise_and(a[i], m) > mx:
			mx = biwise_and(a[i], m)
		if biwise_or(a[i], m) > mx:
			mx = biwise_or(a[i], m)
	print(mx)


def input_n():
	return int(input())

def input_n_m():
	return (int(i) for i in input().split())

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

def solve(z, nums):
	res = max(nums)
	for n in nums:
		res = max(res, z | n)
	return res
rows = input_n()
for _ in range(rows):
	(n, z) = input_n_m()
	nums = input_nums()
	print(solve(z, nums))


t = int(input())
while t:
	(n, z) = map(int, input().split())
	a = list(map(int, input().split()))
	b = []
	res1 = 0
	res2 = 0
	for i in a:
		b.append(i | z)
		b.append(i & z)
	print(max(b))
	t -= 1


for i in range(int(input())):
	(a, b) = map(int, input().split())
	c = list(map(int, input().split()))
	max1 = -9999
	for i in c:
		m = i & b
		n = i | b
		if m > max1:
			max1 = m
		if n > max1:
			max1 = n
	print(max1)