JJ has an array A of length N and an integer X. JJ can perform the following operation at most once:
Select a [subsequence] of A and add X to all the elements of that subsequence.

For example, if A = [2, 1, 6, 3, 5] and X = 7, we can select the subsequence [2, 3, 5] and add X to all the elements. Now the array A becomes [2 + 7, 1, 6, 3 + 7, 5 + 7] = [9, 1, 6, 10, 12].

JJ wants to maximize the value of \displaystyle \sum_{i = 2}^{N} (A_{i - 1} \oplus A_{i}). Can you help him to do so?

Here, \oplus denotes the [bitwise XOR operation].

& & kya
& 

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

- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and X — the size of the array A and the parameter X mentioned in the statement.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the array A.

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

For each test case, output the maximum value of \displaystyle \sum_{i = 2}^{n} (A_{i - 1} \oplus A_{i}) which can be obtained after applying the given operation at most once.

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

$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤X ≤10^{9}$
$1 ≤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
1 2
4 1
2 2 3 3
5 2
5 3 6 2 8

----- Sample Output 1 ------ 
3
15
43

----- explanation 1 ------ 
Test case $1$: It is optimal to not perform the given operation. So the answer will equal $1 \oplus 2 = 3$.

Test case $2$: It is optimal to add $X = 1$ to the $2^{nd}$ and the $3^{rd}$ element. So $A$ will become $[2, 3, 4, 3]$ and the answer will be $(2 \oplus 3) + (3 \oplus 4) + (4 \oplus 3) = 15$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Bit manipulation

Mathematics


Solutions:
for t in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	(p, q) = (0, 0)
	for i in range(1, n):
		same = max(p + (a[i] ^ a[i - 1]), q + (a[i] ^ a[i - 1] + x))
		change = max(p + (a[i] + x ^ a[i - 1]), q + (a[i] + x ^ a[i - 1] + x))
		(p, q) = (same, change)
	print(max(p, q))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	dp = [[0 for i in range(n)] for i in range(2)]
	dp[0][0] = 0
	dp[1][0] = 0
	for i in range(1, n):
		cur = l[i]
		prev = l[i - 1]
		dp[0][i] = max((cur ^ prev) + dp[0][i - 1], (cur ^ prev + k) + dp[1][i - 1])
		dp[1][i] = max((cur + k ^ prev) + dp[0][i - 1], (cur + k ^ prev + k) + dp[1][i - 1])
	print(max(dp[0][n - 1], dp[1][n - 1]))


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0 for i in range(n)] for j in range(2)]
	dp[0][0] = 0
	dp[1][0] = 0
	for i in range(1, n):
		val1 = dp[0][i - 1] + (a[i] ^ a[i - 1])
		val2 = dp[1][i - 1] + (a[i] ^ a[i - 1] + x)
		dp[0][i] = max(val1, val2)
		val1 = dp[0][i - 1] + (a[i] + x ^ a[i - 1])
		val2 = dp[1][i - 1] + (a[i] + x ^ a[i - 1] + x)
		dp[1][i] = max(val1, val2)
	print(max(dp[0][n - 1], dp[1][n - 1]))


T = int(input())
for t in range(0, T):
	(N, X) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp1 = [0 for i in range(0, N)]
	dp2 = [0 for i in range(0, N)]
	for i in range(1, N):
		dp1[i] = max(dp1[i - 1] + (arr[i - 1] ^ arr[i]), dp2[i - 1] + (arr[i - 1] + X ^ arr[i]))
		dp2[i] = max(dp1[i - 1] + (arr[i - 1] ^ arr[i] + X), dp2[i - 1] + (arr[i - 1] + X ^ arr[i] + X))
	ans = max(dp1[N - 1], dp2[N - 1])
	print(ans)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	(p, q) = (0, 0)
	for i in range(1, n):
		s = max(p + (arr[i] ^ arr[i - 1]), q + (arr[i] ^ arr[i - 1] + x))
		c = max(p + (arr[i] + x ^ arr[i - 1]), q + (arr[i] + x ^ arr[i - 1] + x))
		(p, q) = (s, c)
	print(max(p, q))


t = int(input())
for k in range(t):
	(n, x) = input().split()
	(n, x) = (int(n), int(x))
	s = input().split()
	if n == 1:
		print(0)
		continue
	for i in range(n):
		s[i] = int(s[i])
	dp = []
	for i in range(n):
		dp.append([-1, -1])
	dp[1] = [max(s[0] ^ s[1], s[0] + x ^ s[1]), max(s[0] ^ s[1] + x, s[0] + x ^ s[1] + x)]
	for i in range(2, n):
		dp[i][0] = max((s[i] ^ s[i - 1]) + dp[i - 1][0], (s[i] ^ s[i - 1] + x) + dp[i - 1][1])
		dp[i][1] = max((s[i] + x ^ s[i - 1]) + dp[i - 1][0], (s[i] + x ^ s[i - 1] + x) + dp[i - 1][1])
	print(max(dp[n - 1][0], dp[n - 1][1]))


T = int(input())
for j in range(T):
	(N, X) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	dp = [[0 for i in range(N)] for i in range(2)]
	for i in range(1, N):
		dp[0][i] = max((A[i] ^ A[i - 1]) + dp[0][i - 1], (A[i] ^ A[i - 1] + X) + dp[1][i - 1])
		dp[1][i] = max((A[i] + X ^ A[i - 1]) + dp[0][i - 1], (A[i] + X ^ A[i - 1] + X) + dp[1][i - 1])
	print(max(dp[0][N - 1], dp[1][N - 1]))


n = int(input())
for z in range(n):
	(num, x) = map(int, input().split())
	arr = [int(i) for i in input().split()]
	ans = [0, 0]
	(new1, new2) = (0, 0)
	for i in range(1, num):
		new1 = max((arr[i] ^ arr[i - 1]) + ans[0], (arr[i] ^ arr[i - 1] + x) + ans[1])
		new2 = max((arr[i] + x ^ arr[i - 1]) + ans[0], (arr[i] + x ^ arr[i - 1] + x) + ans[1])
		ans[0] = new1
		ans[1] = new2
	print(max(ans))


for _ in range(int(input())):
	(n, x) = map(int, input().split(' '))
	arr = list(map(int, input().split(' ')))
	s = 0
	if n == 1:
		print(0)
	else:
		change = 0
		unchange = 0
		for i in range(1, n):
			temp1 = change
			temp2 = unchange
			change = max(temp2 + (arr[i - 1] ^ arr[i] + x), temp1 + (arr[i - 1] + x ^ arr[i] + x))
			unchange = max(temp2 + (arr[i - 1] ^ arr[i]), temp1 + (arr[i - 1] + x ^ arr[i]))
		print(max(change, unchange))


from math import ceil
from math import sqrt
from collections import Counter
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	dp1 = [0]
	dp2 = [0]
	for i in range(1, n):
		a = dp2[i - 1] + (l[i - 1] + x ^ l[i])
		b = dp1[i - 1] + (l[i - 1] ^ l[i])
		dp1.append(max(a, b))
		c = dp2[i - 1] + (l[i - 1] + x ^ l[i] + x)
		d = dp1[i - 1] + (l[i - 1] ^ l[i] + x)
		dp2.append(max(c, d))
	ans = max(dp1[-1], dp2[-1])
	print(ans)


for i in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = []
	for i in range(n):
		dp.append([0, 0])
	dp[0][0] = 0
	dp[0][1] = 0
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (a[i - 1] ^ a[i]), dp[i - 1][1] + (a[i - 1] + x ^ a[i]))
		dp[i][1] = max(dp[i - 1][0] + (a[i - 1] ^ a[i] + x), dp[i - 1][1] + (a[i - 1] + x ^ a[i] + x))
	print(max(dp[n - 1][0], dp[n - 1][1]))


import time
import sys
import math
from collections import defaultdict as dd
from functools import lru_cache
inty = lambda : int(input())
stringy = lambda : input().strip()
mappy = lambda : map(int, input().strip().split())
fmappy = lambda : map(float, input().strip().split())
listy = lambda : list(map(int, input().strip().split()))
tick = lambda : time.perf_counter()
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0] * 2 for _ in range(n)]
	for i in range(1, n):
		dp[i][0] = max((a[i] ^ a[i - 1]) + dp[i - 1][0], (a[i] ^ a[i - 1] + x) + dp[i - 1][1])
		dp[i][1] = max((a[i] + x ^ a[i - 1]) + dp[i - 1][0], (a[i] + x ^ a[i - 1] + x) + dp[i - 1][1])
	print(max(dp[n - 1][0], dp[n - 1][1]))


n = int(input())
for i in range(n):
	l1 = list(map(int, input().split()))
	l = list(map(int, input().split()))
	x = l1[1]
	sum0 = 0
	sum1 = 0
	for j in range(1, l1[0]):
		s0 = max(sum0 + (l[j] ^ l[j - 1]), sum1 + (l[j] ^ l[j - 1] + x))
		s1 = max(sum0 + (l[j] + x ^ l[j - 1]), sum1 + (l[j] + x ^ l[j - 1] + x))
		sum0 = s0
		sum1 = s1
	print(max(sum0, sum1))


def adjacent_xors(arr, n, X):
	(p, q) = (0, 0)
	for i in range(1, n):
		prev_same = max(p + (arr[i - 1] ^ arr[i]), q + (arr[i] ^ arr[i - 1] + X))
		prev_change = max(p + (arr[i - 1] ^ arr[i] + X), q + (arr[i - 1] + X ^ arr[i] + X))
		(p, q) = (prev_same, prev_change)
	return max(p, q)
T = int(input())
for _ in range(T):
	(N, X) = tuple(map(int, input().split()))
	arr = list(map(int, input().split()))
	print(adjacent_xors(arr, N, X))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	sum_with_changing_current = 0
	sum_with_same_current = 0
	for i in range(1, len(arr)):
		same = max(sum_with_same_current + (arr[i] ^ arr[i - 1]), sum_with_changing_current + (arr[i] ^ arr[i - 1] + x))
		change = max(sum_with_same_current + (arr[i] + x ^ arr[i - 1]), sum_with_changing_current + (arr[i] + x ^ arr[i - 1] + x))
		sum_with_changing_current = change
		sum_with_same_current = same
	print(max(sum_with_changing_current, sum_with_same_current))


ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input()
from collections import defaultdict
import sys
sys.setrecursionlimit(100000000)
t = ri()
for test in range(t):
	(n, x) = rl()
	l = rl()
	dp = [[0, 0] for i in range(n)]
	for i in range(1, n):
		dp[i][0] = max(dp[i][0], (l[i] ^ l[i - 1]) + dp[i - 1][0])
		dp[i][0] = max(dp[i][0], (l[i] ^ l[i - 1] + x) + dp[i - 1][1])
		dp[i][1] = max(dp[i][1], (l[i] + x ^ l[i - 1]) + dp[i - 1][0])
		dp[i][1] = max(dp[i][1], (l[i] + x ^ l[i - 1] + x) + dp[i - 1][1])
	print(max(dp[n - 1]))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	(p, q) = (0, 0)
	for i in range(1, n):
		s = max(p + (l[i] ^ l[i - 1]), q + (l[i] ^ l[i - 1] + x))
		c = max(p + (l[i] + x ^ l[i - 1]), q + (l[i] + x ^ l[i - 1] + x))
		(p, q) = (s, c)
	print(max(p, q))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	(prev, prev2) = (0, 0)
	for i in range(1, n):
		pick = max(prev + (arr[i - 1] ^ arr[i]), prev2 + (arr[i] ^ arr[i - 1] + x))
		pick2 = max(prev + (arr[i] + x ^ arr[i - 1]), prev2 + (arr[i - 1] + x ^ arr[i] + x))
		(prev, prev2) = (pick, pick2)
	print(max(prev, prev2))


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[0] * 2 for _ in range(n)]
	for i in range(1, n):
		dp[i][0] = max(dp[i][0], (arr[i] ^ arr[i - 1]) + dp[i - 1][0])
		dp[i][0] = max(dp[i][0], (arr[i] ^ arr[i - 1] + x) + dp[i - 1][1])
		dp[i][1] = max(dp[i][1], (arr[i] + x ^ arr[i - 1]) + dp[i - 1][0])
		dp[i][1] = max(dp[i][1], (arr[i] + x ^ arr[i - 1] + x) + dp[i - 1][1])
	print(max(dp[-1][0], dp[-1][1]))


from sys import *
setrecursionlimit(1000000)

def solve(i, n, x, arr, prev, dp):
	if i == n:
		return 0
	if dp[prev][i] != -1:
		return dp[prev][i]
	if prev:
		c1 = (arr[i - 1] + x ^ arr[i]) + solve(i + 1, n, x, arr, 0, dp)
		c2 = (arr[i - 1] + x ^ arr[i] + x) + solve(i + 1, n, x, arr, 1, dp)
	else:
		c1 = (arr[i - 1] ^ arr[i]) + solve(i + 1, n, x, arr, 0, dp)
		c2 = (arr[i - 1] ^ arr[i] + x) + solve(i + 1, n, x, arr, 1, dp)
	dp[prev][i] = max(c1, c2)
	return dp[prev][i]
for t in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[-1 for i in range(n + 1)] for j in range(2)]
	print(max(solve(1, n, x, arr, 0, dp), solve(1, n, x, arr, 1, dp)))


from math import *
from collections import *
import io, os
import sys
from bisect import *
from heapq import *
from itertools import permutations
from functools import *
import re
MOD = 1000000007

def subinp():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('op1.txt', 'w')

def subinp_1():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('op2.txt', 'w')

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

def binser(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
		else:
			r = mid - 1
	return mid

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

def sod(n):
	l = list(str(n))
	s = 0
	for i in l:
		s += int(i)
	return s

def prime_factors(num):
	l = []
	if num % 2:
		l.append(2)
	while num % 2 == 0:
		num = num / 2
	for i in range(3, int(sqrt(num)) + 1, 2):
		if not num % i:
			l.append(i)
		while num % i == 0:
			num = num / i
	if num > 2:
		l.append(num)
	return l

def factmod(n, p):
	f = defaultdict(int)
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % MOD
	return f

def largestPower(n, p):
	x = 0
	while n:
		n //= p
		x += n
	return x

def modFact(n, p):
	if n >= p:
		return 0
	result = 1
	for i in range(1, n + 1):
		result = result * i % p
	return result

def drec(x, y):
	if y == x + 1:
		return 'R'
	if y == x - 1:
		return 'L'
	if x < y:
		return 'D'
	return 'U'

def cellhash(x, y):
	return (x - 1) * m + y

def bins(l, x, n):
	i = bisect_left(l, x)
	if i < n:
		return i
	if i:
		return i - 1
	else:
		return n

def cond(l):
	for i in range(len(l) - 1):
		if l[i] == str(int(l[i + 1]) - 1):
			return False
	return True

def isvowel(s):
	if s in list('aeiou'):
		return 1
	return 0

def countOdd(L, R):
	N = (R - L) // 2
	if R % 2 != 0 or L % 2 != 0:
		N += 1
	return N

def tst(A, B, C):
	return (A | B) & (B | C) & (C | A)

def palcheck(n, s):
	(i, j) = (0, n - 1)
	while i <= j:
		if s[i] == s[j]:
			return False
		i += 1
		j -= 1
	return True

def sakurajima(n):
	if n < 9:
		n = 10
	l = [0]
	for i in range(1, n + 1):
		if i % 2:
			l.append(i)
		else:
			l.append(2)
	for i in range(3, int(n ** 0.5) + 1, 2):
		if l[i] == i:
			for j in range(i * i, n + 1, i):
				if l[j] == j:
					l[j] = i
	return l

def getfact(x):
	ret = []
	d = defaultdict(int)
	while x != 1:
		ret.append(spf[x] ** (d[spf[x]] + 1))
		d[spf[x]] += 1
		x = x // spf[x]
	return ret

def prchck(n):
	if n == 1:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if not n % i:
			return False
	return True

def ispal(s, n):
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			return False
	return True

def bfs(src, dest, ajl, vis):
	q = deque([src])
	vis[src] = True
	while q:
		i = q.popleft()
		if i == dest:
			return True
		for j in ajl[i]:
			if not vis[j]:
				vis[j] = True
				q.append(j)
	return False

def sieve(n):
	if n < 9:
		n = 10
	l = [1] * (n + 1)
	for i in range(2, int(n ** 0.5) + 1):
		if l[i]:
			for j in range(i ** 2, n + 1, i):
				if j % i == 0:
					l[j] = 0
	l[1] = 0
	return l

class DisjSet:

	def __init__(self, n):
		self.size = [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.size[xset] < self.size[yset]:
			self.parent[xset] = yset
			self.size[yset] += self.size[xset]
		else:
			self.parent[yset] = xset
			self.size[xset] += self.size[yset]

def dfs(i, ajl, vis, l, x):
	vis[i] = True
	l[i] = x
	for j in ajl[i]:
		if not vis[j]:
			dfs(j, ajl, vis, l, x)

def checkpo3(N):
	while N > 0:
		if N % 3 == 2:
			return False
		N //= 3
	return True

def sumofdig(n):
	ans = 0
	s = str(n)
	for i in s:
		ans += int(i)
	return ans

def convert(set):
	return [*set]

def Divisors(n):
	i = 1
	ans = []
	while i * i < n:
		if n % i == 0:
			ans.append(i)
		i += 1
	for i in range(int(sqrt(n)), 0, -1):
		if n % i == 0:
			ans.append(n // i)
	return ans
inp = lambda : int(input())
strin = lambda : input().strip()
strl = lambda : list(input().rstrip('\r\n'))
strlst = lambda : list(map(str, input().split()))
mult = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
lstin = lambda : list(map(int, input().strip().split()))
flush = lambda : stdout.flush()
stdpr = lambda x: stdout.write(str(x))

def isval(s):
	n = len(s)
	d = defaultdict(bool)
	if n == 1:
		return True
	if s[0] != s[1]:
		d[s[0]] = True
	for i in range(1, n):
		if s[i] != s[i - 1]:
			if d[s[i]]:
				return False
			d[s[i]] = True
	return True

class Point:

	def __init__(self, x, l1):
		self.x = x
		self.l1 = l1

	def __lt__(self, b):
		return self.l1[self.x] < self.l1[b.x]

	def getval(self):
		return self.x

def chck(n, l):
	ans = 0
	for i in range(n - 1):
		ans += l[i] ^ l[i + 1]
	return ans

def panda(i, n, l, x, k):
	if i >= n:
		return 0
	if dp[k][i] != -1:
		return dp[k][i]
	(c1, c2) = (0, 0)
	if x == 0:
		c1 = (l[i] ^ l[i - 1]) + panda(i + 1, n, l, 0, k)
		c2 = (l[i] + k ^ l[i - 1]) + panda(i + 1, n, l, 1, k)
	else:
		c1 = (l[i] ^ l[i - 1] + k) + panda(i + 1, n, l, 0, k)
		c2 = (l[i] + k ^ l[i - 1] + k) + panda(i + 1, n, l, 1, k)
	dp[k][i] = max(c1, c2)
	return dp[k][i]

def redpanda(n, k, l):
	dp = [[0 for i in range(n + 1)] for j in range(3)]
	for i in range(1, n):
		dp[0][i] = max((l[i] ^ l[i - 1]) + dp[0][i - 1], (l[i] ^ l[i - 1] + k) + dp[1][i - 1])
		dp[1][i] = max((l[i] + k ^ l[i - 1]) + dp[0][i - 1], (l[i] + k ^ l[i - 1] + k) + dp[1][i - 1])
	return max(dp[0][n - 1], dp[1][n - 1])
t = 1
t = int(input())
for _ in range(t):
	(n, k) = mult()
	l = lstin()
	ans = redpanda(n, k, l)
	print(ans)


for tc in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	(globsame, globdiff) = (0, 0)
	for i in range(1, n):
		same = max(globsame + (arr[i] ^ arr[i - 1]), globdiff + (arr[i] ^ arr[i - 1] + x))
		diff = max(globsame + (arr[i] + x ^ arr[i - 1]), globdiff + (arr[i] + x ^ arr[i - 1] + x))
		globsame = max(same, globsame)
		globdiff = max(diff, globdiff)
	print(max(globsame, globdiff))


for tc in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[0, 0]]
	for i in range(1, n):
		same = max(dp[-1][0] + (arr[i] ^ arr[i - 1]), dp[-1][1] + (arr[i] ^ arr[i - 1] + x))
		diff = max(dp[-1][0] + (arr[i] + x ^ arr[i - 1]), dp[-1][1] + (arr[i] + x ^ arr[i - 1] + x))
		dp.append([same, diff])
	print(max(dp[n - 1]))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	arr1 = [i + x for i in arr]
	dp = [[0, 0] for i in range(n)]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i]), dp[i - 1][1] + (arr1[i - 1] ^ arr[i]))
		dp[i][1] = max(dp[i - 1][0] + (arr[i - 1] ^ arr1[i]), dp[i - 1][1] + (arr1[i - 1] ^ arr1[i]))
	print(max(dp[-1][0], dp[-1][1]))


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	(added, notAdded) = (0, 0)
	for i in range(1, n):
		(v1, v2) = (a[i - 1], a[i])
		(added, notAdded) = (max((v2 + k ^ v1) + notAdded, (v2 + k ^ v1 + k) + added), max((v2 ^ v1) + notAdded, (v2 ^ v1 + k) + added))
	print(max(added, notAdded))


import sys
sys.setrecursionlimit(10 ** 6)
from functools import lru_cache

def solve():
	(N, X) = [int(x) for x in input().split(' ')]
	A = [int(x) for x in input().split(' ')]

	@lru_cache(None)
	def go(i, prev):
		if i == N:
			return 0
		pick = (A[i] + X ^ prev) + go(i + 1, A[i] + X)
		pss = (A[i] ^ prev) + go(i + 1, A[i])
		if pick > pss:
			return pick
		return pss
	print(max(go(1, A[0] + X), go(1, A[0])))
for _ in range(int(input())):
	solve()


from sys import stdin, stdout
read = stdin.readline
t = int(read())
for i in range(t):
	(a, x) = map(int, read().split())
	arr = list(map(int, read().split()))
	a1 = []
	a2 = []
	for j in range(a):
		if j == 0:
			a1.append(0)
			a2.append(0)
		else:
			n1 = max(a1[-1] + (arr[j - 1] ^ arr[j]), a2[-1] + (arr[j - 1] + x ^ arr[j]))
			n2 = max(a1[-1] + (arr[j - 1] ^ arr[j] + x), a2[-1] + (arr[j - 1] + x ^ arr[j] + x))
			a1.append(n1)
			a2.append(n2)
	print(max(a1[-1], a2[-1]))


T = int(input())
for _ in range(T):
	(n, x) = [int(i) for i in input().split()]
	A = [int(i) for i in input().split()]
	dp = [[0, 0] for _ in range(n)]
	for i in range(1, n):
		cur_val = A[i]
		prev_val = A[i - 1]
		a1 = cur_val ^ prev_val + x
		a2 = cur_val ^ prev_val
		dp[i][0] = max(dp[i - 1][1] + a1, dp[i - 1][0] + a2)
		b1 = cur_val + x ^ prev_val + x
		b2 = cur_val + x ^ prev_val
		dp[i][1] = max(dp[i - 1][1] + b1, dp[i - 1][0] + b2)
	print(max(dp[-1][0], dp[-1][1]))


import math
from bisect import bisect, bisect_left, bisect_right
import heapq
from math import gcd, floor, sqrt, log
from collections import defaultdict as ddc
from collections import Counter

def intin():
	return int(input())

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

def strin():
	return input().split()
INF = 10 ** 20
mod = 1000000007

def LIS(arr, n):
	dp = [10 ** 9] * (n + 1)
	for ele in arr:
		dp[bisect_left(dp, ele)] = ele
	return bisect_left(dp, 10 ** 9)

def exponentiation(bas, exp, mod=1000000007):
	t = 1
	while exp > 0:
		if exp % 2 != 0:
			t = t * bas % mod
		bas = bas * bas % mod
		exp //= 2
	return t % mod

def MOD(p, q=1, mod=1000000007):
	expo = 0
	expo = mod - 2
	while expo:
		if expo & 1:
			p = p * q % mod
		q = q * q % mod
		expo >>= 1
	return p
yes = 'YES'
no = 'NO'
even = 'EVEN'
odd = 'ODD'

def process(arr, n, x):
	ans = sum((arr[i - 1] ^ arr[i] for i in range(1, n)))
	ans = max(ans, sum((arr[i - 1] + x ^ arr[i] + x for i in range(1, n))))
	ans1 = [arr[0]]
	anns1 = 0
	anns2 = 0
	for i in range(1, n):
		y = arr[i - 1]
		if anns1 + (y ^ arr[i]) > anns2 + (y + x ^ arr[i]):
			temp1 = anns1 + (y ^ arr[i])
		else:
			temp1 = anns2 + (y + x ^ arr[i])
		if anns1 + (y ^ arr[i] + x) < anns2 + (y + x ^ arr[i] + x):
			temp2 = anns2 + (y + x ^ arr[i] + x)
		else:
			temp2 = anns1 + (y ^ arr[i] + x)
		anns1 = temp1
		anns2 = temp2
	ans = max(ans, anns1, anns2)
	return ans

def main():
	for _ in range(int(input())):
		(n, x) = mapin()
		arr = list(mapin())
		print(max(process(arr, n, x), process(arr[::-1], n, x)))
main()


import sys
sys.setrecursionlimit(10 ** 6)

def sol(prev, i):
	if i == N:
		return 0
	if (prev, i) in dp:
		return dp[prev, i]
	num1 = arr[i - 1]
	if prev == 1:
		num1 = arr[i - 1] + X
	dp[prev, i] = max((arr[i] ^ num1) + sol(0, i + 1), (arr[i] + X ^ num1) + sol(1, i + 1))
	return dp[prev, i]
T = int(input())
for _ in range(T):
	(N, X) = [int(x) for x in input().split()]
	arr = [int(s) for s in input().split()]
	dp = {}
	ans1 = sol(1, 1)
	dp = {}
	ans2 = sol(0, 1)
	print(max(ans1, ans2))


import math as m

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return abs(a)

def ncr(N, R):
	return m.factorial(N) // (m.factorial(R) * m.factorial(N - R))

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

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

def solve():
	(n, x) = getL()
	arr = getL()
	dp = [[0 for i in range(2)] for j in range(n)]
	withAdd = [i + x for i in arr]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i]), dp[i - 1][1] + (withAdd[i - 1] ^ arr[i]))
		dp[i][1] = max(dp[i - 1][0] + (arr[i - 1] ^ withAdd[i]), dp[i - 1][1] + (withAdd[i - 1] ^ withAdd[i]))
	print(max(dp[n - 1][0], dp[n - 1][1]))
for _ in range(getInt()):
	solve()


testcase = int(input())
for _ in range(testcase):
	(n, x) = [int(num) for num in input().split()]
	arr = [int(num) for num in input().split()]
	ans = [[0, 0] for _ in range(n)]
	for i in range(1, n):
		for j in range(2):
			t = arr[i] + x * j
			p0 = ans[i - 1][0] + (arr[i - 1] ^ t)
			p1 = ans[i - 1][1] + (arr[i - 1] + x ^ t)
			ans[i][j] = max(p0, p1)
	print(max(ans[-1]))


t = int(input())
for k in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	temp = [[0, 0] for i in range(n)]
	for i in range(1, n):
		temp[i][0] = max(temp[i - 1][0] + (arr[i - 1] ^ arr[i]), temp[i - 1][1] + (arr[i - 1] + x ^ arr[i]))
		temp[i][1] = max(temp[i - 1][0] + (arr[i - 1] ^ arr[i] + x), temp[i - 1][1] + (arr[i - 1] + x ^ arr[i] + x))
	ans = max(temp[n - 1][0], temp[n - 1][1])
	print(ans)


from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)
t = int(input())
while t:
	s = input().split()
	(n, x) = map(int, s)
	s = input().split()
	arr = list(map(int, s))
	dp = [[0 for i in range(n + 1)] for _ in range(2)]
	for i in range(1, n):
		dp[0][i] = (arr[i - 1] + x ^ arr[i]) + dp[1][i - 1]
		dp[0][i] = max(dp[0][i], (arr[i - 1] ^ arr[i]) + dp[0][i - 1])
		dp[1][i] = (arr[i - 1] + x ^ arr[i] + x) + dp[1][i - 1]
		dp[1][i] = max(dp[1][i], (arr[i - 1] ^ arr[i] + x) + dp[0][i - 1])
	ans = max(dp[0][n - 1], dp[1][n - 1])
	print(ans)
	t -= 1


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	g = [[0] * (n + 1) for _ in range(2)]
	for i in range(1, n):
		if i == 1:
			g[0][i] = max(a[i - 1] ^ a[i], a[i - 1] + x ^ a[i])
			g[1][i] = max(a[i - 1] ^ a[i] + x, a[i - 1] + x ^ a[i] + x)
		else:
			g[0][i] = max(g[0][i - 1] + (a[i - 1] ^ a[i]), g[1][i - 1] + (a[i - 1] + x ^ a[i]))
			g[1][i] = max(g[0][i - 1] + (a[i - 1] ^ a[i] + x), g[1][i - 1] + (a[i - 1] + x ^ a[i] + x))
	print(max(g[0][n - 1], g[1][n - 1]))


maxx = 2 * 10 ** 5
arr = [0] * maxx
arr2 = [[0] * 2 for i in range(maxx)]
for _ in range(int(input())):
	(n1, n2) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in range(1, len(l) + 1):
		arr[i] = l[i - 1]
	for i in range(2, n1 + 1):
		arr2[i][0] = max(arr2[i - 1][0] + (arr[i - 1] ^ arr[i]), arr2[i - 1][1] + (arr[i - 1] + n2 ^ arr[i]))
		arr2[i][1] = max(arr2[i - 1][0] + (arr[i - 1] ^ arr[i] + n2), arr2[i - 1][1] + (arr[i - 1] + n2 ^ arr[i] + n2))
	print(max(arr2[n1][0], arr2[n1][1]))


T = int(input())
import copy
for i in range(T):
	(N, X) = [int(z) for z in input().strip().split()]
	arr = [int(z) for z in input().strip().split()]
	(with_x, without_x) = ([arr[0] + X], [arr[0]])
	if N >= 2:
		without_x.append(max(arr[1] ^ arr[0] + X, arr[1] ^ arr[0]))
		with_x.append(max(arr[1] + X ^ arr[0], arr[1] + X ^ arr[0] + X))
	for i in range(2, N):
		wo = copy.deepcopy(max((arr[i] ^ arr[i - 1] + X) + with_x[-1], (arr[i] ^ arr[i - 1]) + without_x[-1]))
		wi = copy.deepcopy(max((arr[i] + X ^ arr[i - 1] + X) + with_x[-1], (arr[i] + X ^ arr[i - 1]) + without_x[-1]))
		with_x.append(wi)
		without_x.append(wo)
	if N == 1:
		print(0)
	else:
		print(max(without_x[-1], with_x[-1]))


for i in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	A = [0] * (n + 1)
	j = 1
	for x in input().split():
		A[j] = int(x)
		j += 1
	c = [[0 for i in range(2)] for j in range(n + 1)]
	for i in range(2, n + 1):
		c[i][0] = max(c[i - 1][0] + (A[i - 1] ^ A[i]), c[i - 1][1] + (A[i - 1] + k ^ A[i]))
		c[i][1] = max(c[i - 1][0] + (A[i - 1] ^ A[i] + k), c[i - 1][1] + (A[i - 1] + k ^ A[i] + k))
	print(max(c[n][0], c[n][1]))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	inputArray = list(map(int, input().split()))
	dynamic = [[0 for i in range(2)] for j in range(n)]
	for i in range(1, n):
		[prev0, prev1] = dynamic[i - 1]
		(p0, p1) = (inputArray[i], inputArray[i - 1])
		dynamic[i][1] = max(prev0 + (p1 ^ p0 + x), prev1 + (p1 + x ^ p0 + x))
		dynamic[i][0] = max(prev0 + (p1 ^ p0), prev1 + (p1 + x ^ p0))
	print(max(dynamic[n - 1][0], dynamic[n - 1][1]))


import threading
import sys
from sys import stdin
input = stdin.readline
sys.setrecursionlimit(10 ** 8)
from collections import defaultdict

def main():
	from functools import lru_cache
	for _ in range(int(input())):
		(n, x) = map(int, input().split())
		arr = list(map(int, input().split()))

		@lru_cache(None)
		def solve(i, p):
			if i == n:
				return 0
			elif p == 1:
				return max(solve(i + 1, 1) + (arr[i] + x ^ arr[i - 1] + x), solve(i + 1, 0) + (arr[i - 1] + x ^ arr[i]))
			else:
				return max(solve(i + 1, 1) + (arr[i] + x ^ arr[i - 1]), solve(i + 1, 0) + (arr[i - 1] ^ arr[i]))
		print(max(solve(1, 0), solve(1, 1)))
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()


T = int(input())
for t in range(T):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	l.insert(0, 0)
	mat = []
	for i in range(n + 1):
		q = [0] * 2
		mat.append(q)
	for i in range(2, n + 1):
		mat[i][0] = max(mat[i - 1][0] + (l[i - 1] ^ l[i]), mat[i - 1][1] + (l[i - 1] + x ^ l[i]))
		mat[i][1] = max(mat[i - 1][0] + (l[i - 1] ^ l[i] + x), mat[i - 1][1] + (l[i - 1] + x ^ l[i] + x))
	print(max(mat[n][0], mat[n][1]))


for t in range(int(input())):
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	dp2 = [[0, 0], [0, 0]]
	for i in range(2, n + 1):
		dp2[0].append(max(dp2[0][i - 1] + (arr[i - 2] ^ arr[i - 1]), dp2[1][i - 1] + (arr[i - 2] + x ^ arr[i - 1])))
		dp2[1].append(max(dp2[0][i - 1] + (arr[i - 2] ^ arr[i - 1] + x), dp2[1][i - 1] + (arr[i - 2] + x ^ arr[i - 1] + x)))
	print(max(dp2[0][n], dp2[1][n]))


t = int(input())
res = []
for _ in range(t):
	(n, X) = [int(x) for x in input().split()]
	nums = [int(x) for x in input().split()]
	dp = [[0 for _ in range(2)] for _ in range(n)]
	for i in range(1, n):
		choice1 = dp[i - 1][1] + (nums[i - 1] + X ^ nums[i])
		choice2 = dp[i - 1][1] + (nums[i - 1] + X ^ nums[i] + X)
		choice3 = dp[i - 1][0] + (nums[i - 1] ^ nums[i] + X)
		choice4 = dp[i - 1][0] + (nums[i - 1] ^ nums[i])
		dp[i][0] = max(choice1, choice4)
		dp[i][1] = max(choice2, choice3)
	res.append(max(dp[-1][0], dp[-1][1]))
for val in res:
	print(val)


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = [int(s) for s in input().split()]
	dp = []
	for _ in range(n):
		dp.append([0, 0])
	for i in range(1, n):
		dp1 = dp[i - 1][0] + (a[i - 1] ^ a[i])
		dp2 = dp[i - 1][1] + (a[i - 1] + x ^ a[i])
		dp[i][0] = max(dp1, dp2)
		dp1 = dp[i - 1][0] + (a[i - 1] ^ a[i] + x)
		dp2 = dp[i - 1][1] + (a[i - 1] + x ^ a[i] + x)
		dp[i][1] = max(dp1, dp2)
	print(max(dp[-1][0], dp[-1][1]))


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if n > 1:
		a = {}
		for i in range(1, n + 1):
			a[i] = l[i - 1]
		d1 = {}
		d2 = {}
		for i in range(2, n + 1):
			if i - 1 not in d1:
				d1[i - 1] = 0
			if i - 1 not in d2:
				d2[i - 1] = 0
			d1[i] = max(d1[i - 1] + (a[i - 1] ^ a[i]), d2[i - 1] + (a[i - 1] + x ^ a[i]))
			d2[i] = max(d1[i - 1] + (a[i - 1] ^ a[i] + x), d2[i - 1] + (a[i - 1] + x ^ a[i] + x))
		print(max(d1[n], d2[n]))
	else:
		print(0)


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	a.insert(0, 0)
	dp = defaultdict(int)
	for i in range(2, n + 1):
		dp[i, 0] = max(dp[i - 1, 0] + (a[i - 1] ^ a[i]), dp[i - 1, 1] + (a[i - 1] + x ^ a[i]))
		dp[i, 1] = max(dp[i - 1, 0] + (a[i - 1] ^ a[i] + x), dp[i - 1, 1] + (a[i - 1] + x ^ a[i] + x))
	print(max(dp[n, 0], dp[n, 1]))


def rec(i, A, X, N, prev):
	if i == 0:
		return 0
	if i == N:
		return max(rec(i - 1, A, X, N, 1), rec(i - 1, A, X, N, 0))
	if prev == 1:
		return max((A[i - 1] + X ^ A[i] + X) + rec(i - 1, A, X, N, 1), (A[i - 1] ^ A[i] + X) + rec(i - 1, A, X, N, 0))
	else:
		return max((A[i - 1] + X ^ A[i]) + rec(i - 1, A, X, N, 1), (A[i - 1] ^ A[i]) + rec(i - 1, A, X, N, 0))
T = int(input())
for _ in range(T):
	(N, X) = map(int, input().split())
	A = list(map(int, input().split()))
	dp = [[-1 for j in range(2)] for i in range(N + 1)]
	for j in range(2):
		dp[0][j] = 0
	for i in range(1, N + 1):
		for j in range(2):
			if i == N:
				dp[i][j] = max(dp[i - 1][0], dp[i - 1][1])
			elif j == 0:
				dp[i][j] = max((A[i - 1] + X ^ A[i]) + dp[i - 1][1], (A[i - 1] ^ A[i]) + dp[i - 1][0])
			else:
				dp[i][j] = max((A[i - 1] + X ^ A[i] + X) + dp[i - 1][1], (A[i - 1] ^ A[i] + X) + dp[i - 1][0])
	print(max(dp[N]))


t = int(input())
while t > 0:
	t -= 1
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(max([a[0] ^ a[1], a[0] + x ^ a[1], a[0] ^ a[1] + x, a[0] + x ^ a[1] + x]))
		continue
	xors = [a[i] ^ a[i + 1] for i in range(n - 1)]
	matrix = [[0 for i in range(n - 1)] for _ in range(4)]
	for i in range(n - 1):
		matrix[1][i] = (a[i] + x ^ a[i + 1]) - xors[i]
	for i in range(n - 1):
		matrix[2][i] = (a[i] ^ a[i + 1] + x) - xors[i]
	for i in range(n - 1):
		matrix[3][i] = (a[i] + x ^ a[i + 1] + x) - xors[i]
	for i in range(n - 3, -1, -1):
		matrix[3][i] = matrix[3][i] + max(matrix[1][i + 1], matrix[3][i + 1])
		matrix[2][i] = matrix[2][i] + max(matrix[1][i + 1], matrix[3][i + 1])
		matrix[1][i] = matrix[1][i] + max(matrix[0][i + 1], matrix[2][i + 1])
		matrix[0][i] = matrix[0][i] + max(matrix[0][i + 1], matrix[2][i + 1])
	print(sum(xors) + max(0, max([matrix[i][0] for i in range(4)])))


def miis():
	return map(int, input().split())
for _ in range(int(input())):
	(n, x) = miis()
	a = list(miis())
	dpgive = [0]
	dpdont = [0]
	for i in range(1, n):
		to_d = max(dpgive[-1] + (a[i - 1] + x ^ a[i]), dpdont[-1] + (a[i - 1] ^ a[i]))
		to_g = max(dpgive[-1] + (a[i - 1] + x ^ a[i] + x), dpdont[-1] + (a[i - 1] ^ a[i] + x))
		dpgive.append(to_g)
		dpdont.append(to_d)
	print(max(dpgive[-1], dpdont[-1]))


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	dp = [[0, 0] for i in range(n + 1)]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			dp[i][0] = 0
			dp[i][1] = 0
		else:
			dp[i][0] = max((arr[i] ^ arr[i + 1]) + dp[i + 1][0], (arr[i] ^ arr[i + 1] + x) + dp[i + 1][1])
			dp[i][1] = max((arr[i] + x ^ arr[i + 1]) + dp[i + 1][0], (arr[i] + x ^ arr[i + 1] + x) + dp[i + 1][1])
	print(max(dp[0][0], dp[0][1]))


import sys
sys.setrecursionlimit(10 ** 6)

def fun(i, n, f, arr, x, dp):
	if i == n:
		return 0
	ans = 0
	if dp[i - 1][f] != -1:
		return dp[i - 1][f]
	if f == 1:
		ans = max(ans, (arr[i - 1] + x ^ arr[i]) + fun(i + 1, n, 0, arr, x, dp), (arr[i - 1] + x ^ arr[i] + x) + fun(i + 1, n, 1, arr, x, dp))
	else:
		ans = max(ans, (arr[i - 1] ^ arr[i]) + fun(i + 1, n, 0, arr, x, dp), (arr[i - 1] ^ arr[i] + x) + fun(i + 1, n, 1, arr, x, dp))
	dp[i - 1][f] = ans
	return ans
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[-1] * 2 for i in range(n + 1)]
	if n == 1:
		print(0)
	else:
		dp = [[-1, -1] for i in range(n + 1)]
		ans1 = fun(1, n, 0, arr, x, dp)
		ans2 = fun(1, n, 1, arr, x, dp)
		print(max(ans1, ans2))


for _ in range(int(input())):
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	incl = max(a[n - 2] + x ^ a[n - 1], a[n - 2] + x ^ a[n - 1] + x)
	excl = max(a[n - 2] ^ a[n - 1], a[n - 2] ^ a[n - 1] + x)
	for i in range(n - 3, -1, -1):
		(incl, excl) = (max(incl + (a[i] + x ^ a[i + 1] + x), excl + (a[i] + x ^ a[i + 1])), max(incl + (a[i] ^ a[i + 1] + x), excl + (a[i] ^ a[i + 1])))
	print(max(incl, excl))


def sol(a, n, x):
	r = 0
	for i in range(n - 1):
		r += a[i] ^ a[i + 1]
	if x == 0:
		return r
	dp1 = [0] * (n + 1)
	dp0 = [0] * (n + 1)
	for i in range(2, n + 1):
		dp0[i] = max(dp1[i - 1] + (a[i - 1] ^ a[i - 2] + x), dp0[i - 1] + (a[i - 1] ^ a[i - 2]))
		dp1[i] = max(dp1[i - 1] + (a[i - 1] + x ^ a[i - 2] + x), dp0[i - 1] + (a[i - 1] + x ^ a[i - 2]))
	return max(dp1[n], dp0[n])
for _ in range(int(input())):
	(n, x) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	print(sol(a, n, x))


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

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

def fact(n):
	if n == 1:
		return 1
	return n * fact(n - 1)

def cbits(n):
	count = 0
	while n:
		n = n & n - 1
		count += 1
	return count

def solve(n, x, arr):
	dp = [[0] * 2 for i in range(n + 1)]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i]), dp[i - 1][1] + (arr[i - 1] + x ^ arr[i]))
		dp[i][1] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i] + x), dp[i - 1][1] + (arr[i - 1] + x ^ arr[i] + x))
	return max(dp[n - 1][0], dp[n - 1][1])
t = int(input())
while t > 0:
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(solve(n, x, arr))
	t -= 1


import math
ts = int(input())
for _ in range(ts):
	(n, x) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	if n == 1:
		print(0)
		continue
	dp = [[], []]
	for i in range(n):
		dp[0].append(0)
		dp[1].append(0)
	for i in range(1, n, 1):
		dp[0][i] = max(dp[0][i - 1] + (arr[i - 1] + x ^ arr[i] + x), dp[1][i - 1] + (arr[i - 1] ^ arr[i] + x))
		dp[1][i] = max(dp[0][i - 1] + (arr[i - 1] + x ^ arr[i]), dp[1][i - 1] + (arr[i - 1] ^ arr[i]))
	print(max(dp[0][n - 1], dp[1][n - 1]))


from sys import setrecursionlimit
from functools import lru_cache
setrecursionlimit(10 ** 6)

def solve(n, x, arr):

	@lru_cache(None)
	def dp(i, prev_added):
		if i == n:
			return 0
		prev = 0
		if i > 0:
			prev = arr[i - 1]
			if prev_added:
				prev += x
		res = dp(i + 1, False) + (arr[i] ^ prev)
		res = max(res, dp(i + 1, True) + (arr[i] + x ^ prev))
		return res
	return max(dp(1, False), dp(1, True))
for _ in range(int(input())):
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	print(solve(n, x, arr))


import random
import math
from collections import defaultdict, Counter
from functools import lru_cache
import bisect
import heapq

class SegmentTree:

	def __init__(self, nums):
		self._size = len(nums)
		self._tree = [0] * (4 * self._size)
		self.build(nums)

	def build(self, a, v=1, lo=0, hi=None):
		if hi is None:
			hi = self._size - 1
		if lo == hi:
			self._tree[v] = a[lo]
		else:
			mi = (lo + hi) // 2
			self.build(a, 2 * v, lo, mi)
			self.build(a, 2 * v + 1, mi + 1, hi)
			self._tree[v] = self._tree[2 * v] + self._tree[2 * v + 1]

	def update(self, pos, val, v=1, lo=0, hi=None):
		if hi is None:
			hi = self._size - 1
		if lo == hi:
			self._tree[v] = val
		else:
			mi = (lo + hi) // 2
			if pos <= mi:
				self.update(pos, val, 2 * v, lo, mi)
			else:
				self.update(pos, val, 2 * v + 1, mi + 1, hi)
			self._tree[v] = self._tree[2 * v] + self._tree[2 * v + 1]

	def query(self, l, h, v=1, lo=0, hi=None):
		if hi is None:
			hi = self._size - 1
		if l > h:
			return 0
		elif l == lo and h == hi:
			return self._tree[v]
		else:
			mi = (lo + hi) // 2
			return self.query(l, min(mi, h), 2 * v, lo, mi) + self.query(max(mi + 1, l), h, 2 * v + 1, mi + 1, hi)

class UnionFind:

	def __init__(self, n):
		self.n = n
		self.parents = list(range(n))

	def find(self, x):
		if self.parents[x] == x:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x != y:
			self.parents[x] = y
dire = [0, 1, 0, -1, 0]

def is_prime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def google(t):
	print('Case #{}:'.format(t), end=' ')
RANDOM = random.randrange(2 ** 62)

def Wrapper(x):
	return x ^ RANDOM

def solve():
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0 for _ in range(2)] for _ in range(n)]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (a[i] ^ a[i - 1]), dp[i - 1][1] + (a[i] ^ a[i - 1] + x))
		dp[i][1] = max(dp[i - 1][0] + (a[i] + x ^ a[i - 1]), dp[i - 1][1] + (a[i] + x ^ a[i - 1] + x))
	print(max(dp[-1]))
for t in range(int(input())):
	solve()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	aa = [[0] * 2 for i in range(a)]
	dp = list(map(int, input().split()))
	for i in range(1, a):
		aa[i][0] = max(aa[i - 1][0] + (dp[i - 1] ^ dp[i]), aa[i - 1][1] + (dp[i - 1] + b ^ dp[i]))
		aa[i][1] = max(aa[i - 1][0] + (dp[i - 1] ^ dp[i] + b), aa[i - 1][1] + (dp[i - 1] + b ^ dp[i] + b))
	print(max(aa[-1][0], aa[-1][1]))


T = int(input())
for tr in range(T):
	(N, X) = map(int, input().split(' '))
	A = list(map(int, input().split(' ')))
	dp = [[-1, -1] for i in range(N)]
	for i in range(N):
		if i == 0:
			dp[0][0] = 0
			dp[0][1] = 0
		else:
			dp[i][1] = max(int(A[i] + X ^ A[i - 1]) + dp[i - 1][0], int(A[i] + X ^ A[i - 1] + X) + dp[i - 1][1])
			dp[i][0] = max(int(A[i] ^ A[i - 1]) + dp[i - 1][0], int(A[i] ^ A[i - 1] + X) + dp[i - 1][1])
	print(max(dp[N - 1][0], dp[N - 1][1]))


from math import inf
from collections import *
import math, os, sys, heapq, bisect, random, threading
from functools import lru_cache
from itertools import *
import sys

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

def out(var):
	sys.stdout.write(str(var))

def inpu():
	return int(inp())

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 fsep():
	return map(float, inp().split())
(M, M1) = (1000000007, 998244353)

def main():
	how_much_noob_I_am = 1
	how_much_noob_I_am = int(input())
	for _ in range(how_much_noob_I_am):
		(n, x) = sep()
		arr = lis()
		dp = [[0] * 2 for i in range(n)]
		for i in range(1, n):
			dp[i][0] = max(dp[i - 1][1] + (arr[i - 1] + x ^ arr[i]), dp[i - 1][0] + (arr[i - 1] ^ arr[i]))
			dp[i][1] = max(dp[i - 1][1] + (arr[i - 1] + x ^ arr[i] + x), dp[i - 1][0] + (arr[i - 1] ^ arr[i] + x))
		print(max(dp[n - 1][0], dp[n - 1][1]))
main()


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if n > 1:
		prevdp0 = prevdp1 = 0
		for i in range(n - 1):
			dp0 = max(prevdp0 + (a[i] ^ a[i + 1]), prevdp1 + (a[i] + x ^ a[i + 1]))
			dp1 = max(prevdp0 + (a[i] ^ a[i + 1] + x), prevdp1 + (a[i] + x ^ a[i + 1] + x))
			(prevdp0, prevdp1) = (dp0, dp1)
		print(max(dp0, dp1))
	else:
		print(0)


from collections import Counter, deque, defaultdict, OrderedDict
import math
from bisect import bisect, bisect_left
import sys
import re
MOD = 10 ** 9 + 7
MAX = -sys.maxsize
MIN = sys.maxsize

class get:

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

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

	def float():
		return list(map(float, input().split()))

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

def sqrt(x):
	return int(math.sqrt(x)) + 1

def gcd(*args):
	result = args[0]
	for item in args:
		result = math.gcd(result, item)
	return result

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

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

def solve():
	(n, x) = get.map()
	a = get.int()
	dp = [[0 for j in range(n)] for i in range(2)]
	for i in range(1, n):
		dp[0][i] = max(dp[0][i - 1] + (a[i] ^ a[i - 1]), dp[1][i - 1] + (a[i] ^ a[i - 1] + x))
		dp[1][i] = max(dp[0][i - 1] + (a[i] + x ^ a[i - 1]), dp[1][i - 1] + (a[i] + x ^ a[i - 1] + x))
	print(max(dp[0][-1], dp[1][-1]))
t = int(input())
for tt in range(t):
	solve()


def num():
	c = int(input())
	return c

def stri():
	c = str(input())
	return c

def vector():
	c = list(map(int, input().split()))
	return c

def strvector():
	c = list(map(str, input().split()))
	return c
import math

def isprime(num):
	a = 2
	while a <= math.sqrt(num):
		if num % a < 1:
			return False
		a = a + 1
	return num > 1
t = num()
while t > 0:
	t -= 1
	(n, x) = vector()
	arr = vector()
	ans = 0
	matrix = [[arr[i], arr[i] + x] for i in range(n)]
	val = [[0, 0] for i in range(n)]
	for i in range(0, len(matrix)):
		if i == 0:
			continue
		else:
			for j in range(0, 2):
				if j == 0:
					d = matrix[i][j] ^ matrix[i - 1][j]
					c = matrix[i][j] ^ matrix[i - 1][j + 1]
					val[i][j] = max(val[i - 1][j] + d, val[i - 1][j + 1] + c)
				else:
					d = matrix[i][j] ^ matrix[i - 1][j]
					c = matrix[i][j] ^ matrix[i - 1][j - 1]
					val[i][j] = max(val[i - 1][j] + d, val[i - 1][j - 1] + c)
	print(max(val[-1]))


for _ in range(int(input())):
	(n, x) = [int(i) for i in input().split()]
	A = [int(i) for i in input().split()]
	sum_orig = 0
	sum_new = 0
	current_sum = 0
	for i in range(n - 1):
		orig = A[i]
		new = A[i] + x
		next_orig = A[i + 1]
		next_new = A[i + 1] + x
		a = max((orig ^ next_orig) + sum_orig, (new ^ next_orig) + sum_new)
		b = max((orig ^ next_new) + sum_orig, (new ^ next_new) + sum_new)
		sum_orig = a
		sum_new = b
	print(max(sum_orig, sum_new))


for _ in range(int(input())):
	(n, p) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = []
	for i in range(n + 1):
		k = []
		for j in range(2):
			k.append(0)
		dp.append(k)
	for i in range(2, n + 1):
		dp[i][0] = max(dp[i - 1][0] + (a[i - 2] ^ a[i - 1]), dp[i - 1][1] + (a[i - 2] + p ^ a[i - 1]))
		dp[i][1] = max(dp[i - 1][0] + (a[i - 2] ^ a[i - 1] + p), dp[i - 1][1] + (a[i - 2] + p ^ a[i - 1] + p))
	print(max(dp[n][0], dp[n][1]))


T = int(input())
for _ in range(T):
	(N, X) = map(int, input().split())
	flag = 200001
	check = 1
	bool = True
	arr = list(map(int, input().split()))
	Bool = False
	dp = [[0, 0] for i in range(N)]
	for i in range(1, N):
		flag += check
		check ^= flag
		dp[i][0] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i]), dp[i - 1][1] + (arr[i - 1] + X ^ arr[i]))
		check -= flag
		dp[i][1] = max(dp[i - 1][0] + (arr[i - 1] ^ arr[i] + X), dp[i - 1][1] + (arr[i - 1] + X ^ arr[i] + X))
		bool = False
	Bool = True
	print(str(max(dp[N - 1][0], dp[N - 1][1])))


import math

def solve():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[0, 0] for x in range(n + 1)]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0] + (arr[i] ^ arr[i - 1]), dp[i - 1][1] + (arr[i] ^ arr[i - 1] + m))
		dp[i][1] = max(dp[i - 1][0] + (arr[i] + m ^ arr[i - 1]), dp[i - 1][1] + (arr[i] + m ^ arr[i - 1] + m))
	return max(dp[n - 1])
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	(N, X) = map(int, input().split())
	A = list(map(int, input().split()))
	dp = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(N)]
	for i in range(N - 1):
		for j in range(2):
			for k in range(2):
				for l in range(2):
					dp[i + 1][l][k] = max(dp[i + 1][l][k], dp[i][k][j] + (A[i] + k * X ^ A[i + 1] + l * X))
	res = 0
	for i in range(2):
		for j in range(2):
			res = max(res, dp[N - 1][i][j])
	print(res)


t = int(input())
for t_1 in range(t):
	line = input().split(' ')
	n = int(line[0])
	x = int(line[1])
	numbers = [int(item) for item in input().split(' ')]
	max_by_added_i = [0]
	max_not_added_i = [0]
	for i in range(1, n):
		max_by_added_i.append(max(max_by_added_i[i - 1] + (numbers[i - 1] + x ^ numbers[i] + x), max_not_added_i[i - 1] + (numbers[i - 1] ^ numbers[i] + x)))
		max_not_added_i.append(max(max_by_added_i[i - 1] + (numbers[i - 1] + x ^ numbers[i]), max_not_added_i[i - 1] + (numbers[i - 1] ^ numbers[i])))
	print(max(max_by_added_i[n - 1], max_not_added_i[n - 1]))


def solve(x, a):
	res = [0, 0]
	for i in range(len(a) - 1):
		oldres = res.copy()
		res[0] = max((a[i] + x ^ a[i + 1] + x) + oldres[0], (a[i] ^ a[i + 1] + x) + oldres[1])
		res[1] = max((a[i] + x ^ a[i + 1]) + oldres[0], (a[i] ^ a[i + 1]) + oldres[1])
	return max(res)
t = int(input())
for i in range(t):
	(n, x) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	print(solve(x, a))