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

You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$ and you have to perform the following operation exactly $X$ times:
Choose two integers $i$ and $j$ such that $1 ≤ i < j ≤ N$.
Choose a non-negative integer $p$.
Change $A_{i}$ to $A_{i} \oplus 2^{p}$, and change $A_{j}$ to $A_{j} \oplus 2^{p}$, where $\oplus$ denotes [bitwise XOR].

Find the lexicographically smallest sequence which can be obtained by performing this operation exactly $X$ times.

A sequence $B_{1}, B_{2}, \ldots, B_{N}$ is said to be lexicographically smaller than a sequence $C_{1}, C_{2}, \ldots, C_{N}$ if there is an index $i$ such that $B_{i} < C_{i}$ and for each valid $j < i$, $B_{j} = C_{j}$.

------  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 two space-separated integers $N$ and $X$. 
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 $N$ space-separated integers ― the lexicographically smallest obtainable sequence.

------  Constraints ------
$1 ≤ T ≤ 10$
$2 ≤ N ≤ 10^{5}$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$

------  Subtasks ------
Subtask #1 (30 points): $N ≤ 100$

Subtask #2 (70 points): original constraints

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

3 3

2 2 3
----- Sample Output 1 ------ 
0 0 3
----- explanation 1 ------ 
Example case 1: The original sequence is $(2, 2, 3)$. Consider the following three operations:
- Choose $i = 1$, $j = 2$ and $p = 1$. Then, $A_{1}$ changes from $2$ to $2 \oplus 2^{1} = 0$ and similarly, $A_{2}$ changes from $2$ to $2 \oplus 2^{1} = 0$. Now the sequence is $(0, 0, 3)$.
- Next, choose $i = 1$, $j = 2$ and $p = 1$. Then, $A_{1}$ changes to $0 \oplus 2^{1} = 2$ and $A_{2}$ changes to $0 \oplus 2^{1} = 2$. The sequence is $(2, 2, 3)$.
- Next, again choose $i = 1$, $j = 2$ and $p = 1$. Then, $A_{1}$ changes to $2 \oplus 2^{1} = 0$ and $A_{2}$ changes to $2 \oplus 2^{1} = 0$. The sequence is $(0, 0, 3)$ again.

We can check that after exactly 3 operations, this is the lexicographically smallest sequence we can obtain.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Mathematics


Solutions:
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	rem = 0
	for i in range(n - 1):
		a = arr[i]
		d = a & rem
		(a, rem) = (a ^ d, rem ^ d)
		b = bin(a).count('1')
		if b <= x:
			x -= b
			rem ^= a
			a = 0
		elif x > 0:
			d = 0
			for j in range(31, -1, -1):
				if a & 1 << j != 0:
					d ^= 1 << j
					x -= 1
					if x == 0:
						break
			(a, rem) = (a ^ d, rem ^ d)
		arr[i] = a
	arr[-1] ^= rem
	if x == 1 or (n == 2 and x % 2 == 1):
		arr[-2] = 1
		arr[-1] ^= 1
	print(*arr)


import math
test_case = int(input())

def findmsp(i):
	n = 1 << int(math.log2(i))
	return n
for _ in range(test_case):
	line = input().split(' ')
	N = int(line[0])
	K = int(line[1])
	arr = []
	line = input().split(' ')
	for i in range(0, N):
		arr.append(int(line[i]))
	if N > 2 and N <= K:
		for ind in range(1, N):
			arr[ind] ^= arr[ind - 1]
			arr[ind - 1] = 0
	else:
		i = 0
		while True:
			if K == 0:
				break
			while i < N - 1 and arr[i] == 0:
				i = i + 1
			if i == N - 1:
				break
			msp = int(findmsp(arr[i]))
			arr[i] = arr[i] ^ msp
			j = i + 1
			for j in range(i + 1, N):
				if j == N - 1:
					arr[j] = arr[j] ^ msp
					break
				elif arr[j] ^ msp < arr[j]:
					arr[j] = arr[j] ^ msp
					break
			K = K - 1
		if K == 1 or (K % 2 != 0 and N == 2):
			arr[N - 1] = arr[N - 1] ^ 1
			arr[N - 2] = arr[N - 2] ^ 1
	print(*arr)


import math
import os, sys
from io import BytesIO, IOBase

def ntob(n):
	arr = []
	while n > 0:
		p = int(math.log2(n))
		arr.append(p)
		n ^= 2 ** p
	return arr

def bton(n):
	num = 0
	for i in n:
		num += 2 ** i
	return num

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		arr = list(map(int, input().split()))
		i = 0
		li = []
		j = 1
		rem = []
		if len(arr) > 2:
			if k // n > 3:
				while j < n:
					m = arr[i]
					arr[i] ^= m
					arr[j] ^= m
					i += 1
					j += 1
			else:
				while i < n - 1 and k > 0:
					if arr[i] != 0:
						p = int(math.log2(arr[i]))
						suc = True
						if p in li:
							li.remove(p)
							k -= 1
						elif len(li) < k:
							li.append(p)
						else:
							suc = False
						if suc:
							arr[i] ^= 2 ** p
						else:
							break
					else:
						i += 1
				for i in range(len(arr)):
					if arr[i] != 0:
						rem += [i]
				if len(rem) > 0:
					for i in rem:
						if len(li) > 0:
							bi = ntob(arr[i])
							num = 0
							while num < len(bi):
								if bi[num] in li:
									li.remove(bi[num])
									bi.remove(bi[num])
								else:
									num += 1
							arr[i] = bton(bi)
						else:
							break
				elif len(li) > 0:
					if arr[n - 1] > 0:
						for c in li:
							arr[n - 1] ^= 2 ** c
		else:
			count = 0
			num = k
			while k > 0:
				if arr[i] != 0:
					p = int(math.log2(arr[i]))
					arr[i] ^= 2 ** p
					arr[j] ^= 2 ** p
					count += 1
				elif count % 2 == 0:
					if num % 2 == 0:
						break
					else:
						arr[i] ^= 1
						arr[j] ^= 1
						break
				elif num % 2 != 0:
					break
				else:
					arr[i] ^= 1
					arr[j] ^= 1
					break
				k -= 1
		print(*arr)
main()


for testCase in range(int(input())):
	R = 0
	(N, X) = map(int, input().split())
	array = list(map(int, input().split()))
	for i in range(N - 1):
		binary = bin(array[i])
		if X >= binary.count('1'):
			X = X - binary.count('1')
			for j in range(i + 1, N):
				c = array[i] & array[j]
				array[i] = array[i] - c
				array[j] = array[j] - c
				if array[i] == 0:
					break
			if array[i] != 0:
				if N - 1 - i > 1:
					R = 1
				array[N - 1] = array[i] ^ array[N - 1]
				array[i] = 0
		elif X < binary.count('1') and X > 0:
			z = binary[2:]
			m = ''
			for j in range(len(z)):
				if X > 0:
					if z[j] == '1':
						X -= 1
						m = m + '1'
					else:
						m = m + z[j]
				else:
					m = m + '0'
			array[i] = array[i] - int(m, 2)
			m = int(m, 2)
			for j in range(i + 1, N):
				c = m & array[j]
				m = m - c
				array[j] = array[j] - c
			array[N - 1] = m ^ array[N - 1]
		else:
			break
	if X % 2 != 0 and R == 0:
		array[N - 2] = array[N - 2] ^ 1
		array[N - 1] = array[N - 1] ^ 1
	for i in range(N):
		print(array[i], end=' ')
	print()


import math

def getBestPowNumber(a):
	n = 1 << int(math.log2(a))
	return n
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n > 2 and n <= x:
		for ind in range(1, n):
			arr[ind] ^= arr[ind - 1]
			arr[ind - 1] = 0
	else:
		i = 0
		while True:
			if x == 0:
				break
			while i < n - 1 and arr[i] == 0:
				i += 1
			if i == n - 1:
				break
			best_power_num = getBestPowNumber(arr[i])
			for j in range(i + 1, n):
				if arr[j] ^ best_power_num < arr[j]:
					break
			arr[i] ^= best_power_num
			arr[j] ^= best_power_num
			x -= 1
		if x == 1 or (n == 2 and x % 2 != 0):
			arr[n - 1] ^= 1
			arr[n - 2] ^= 1
	print(*arr)


def getBits(n):
	return [i for (i, bit) in enumerate(bin(n)[::-1]) if bit is '1'][::-1]

def solveUnpaired(unused, paired, i):
	paired = set([x for (x, xi) in enumerate(paired) if xi])
	pow2 = [1 << x for x in range(30)]
	A[:i + 1] = [0] * (i + 1)
	if unused:
		A[i] = sum((pow2[bit] for bit in unused))
		i -= 1
	for j in range(i + 1, N - 1):
		for bit in getBits(A[j]):
			if bit in paired:
				A[j] -= pow2[bit]
				paired.remove(bit)
		if not paired:
			break
	else:
		for bit in paired:
			A[N - 1] ^= pow2[bit]

def solve(N, X):
	(unused, paired) = ([], [False] * 30)
	for i in range(N - 1):
		for bit in getBits(A[i]):
			if X is 0:
				unused.append(bit)
				continue
			paired[bit] = not paired[bit]
			if paired[bit]:
				X -= 1
		if X is 0:
			solveUnpaired(unused, paired, i)
			break
	else:
		solveUnpaired(unused, paired, N - 2)
	if X % 2 and N == 2:
		return [A[0] ^ 1, A[1] ^ 1]
	return A
for _ in range(int(input())):
	[N, X] = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	print(*solve(N, X))


for _ in range(int(input())):
	(n, x) = [int(i) for i in input().split()]
	s = [list(bin(int(i))[2:]) for i in input().split()]
	pad = ['0' for f in range(32 - len(s[-1]))]
	s[-1] = pad + s[-1]
	if x > 10 ** 5 and n > 2:
		arr = [int(''.join(el), 2) for el in s]
		k = 0
		for l in range(n):
			k = k ^ arr[l]
		ans = [0] * n
		ans[-1] = k
		print(*ans)
	else:
		for i in range(n - 1):
			l = len(s[i])
			while set(s[i]) != {'0'}:
				if x == 0:
					break
				ind = s[i].index('1')
				ind_back = l - ind
				s[i][ind] = '0'
				j = i + 1
				flag = True
				while j < n:
					if len(s[j]) >= ind_back and s[j][-ind_back] == '1':
						s[j][-ind_back] = '0'
						flag = False
						break
					j += 1
				if flag == True:
					s[-1][-ind_back] = '1'
				x -= 1
		if x > 0 and x % 2 == 1 and (n == 2):
			s[-2][-1] = '1'
			if s[-1][-1] == '0':
				s[-1][-1] = '1'
			else:
				s[-1][-1] = '0'
		print(*[int(''.join(el), 2) for el in s])


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	s = list(map(int, input().split()))
	if n > 10 ** 4 and x >= n:
		k = 0
		for l in range(n):
			k = k ^ s[l]
		s = [0] * n
		s[n - 1] = k
	else:
		for i in range(n - 1):
			while s[i] != 0:
				if x == 0:
					break
				ind = 2 ** int(math.log2(s[i]))
				s[i] ^= ind
				j = i + 1
				flag = True
				while j < n:
					if s[j] ^ ind < s[j]:
						s[j] ^= ind
						flag = False
						break
					j += 1
				if flag == True:
					s[-1] ^= ind
				x -= 1
		if x > 0 and x % 2 == 1 and (n == 2):
			s[-2] ^= 1
			s[-1] ^= 1
	print(' '.join(map(str, s)))


import math
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	if n > 2:
		if n <= x:
			for ind in range(1, n):
				arr[ind] ^= arr[ind - 1]
				arr[ind - 1] = 0
		else:
			while x > 0:
				p = int(math.log2(arr[i]) // math.log2(2))
				for j in range(i + 1, n):
					if arr[j] ^ 1 << p < arr[j]:
						break
				arr[i] ^= 1 << p
				arr[j] ^= 1 << p
				if arr[i] == 0:
					while arr[i] == 0:
						i += 1
				x -= 1
	else:
		while x > 0:
			p = int(math.log2(arr[i]) // math.log2(2))
			arr[0] ^= 1 << p
			arr[1] ^= 1 << p
			x -= 1
			if arr[0] == 0:
				break
		if x % 2 != 0:
			arr[0] ^= 1
			arr[1] ^= 1
	print(*arr)


import math
t = int(input())
for _ in range(t):
	(n, x) = input().split()
	n = int(n)
	x = int(x)
	a = list(map(int, input().split()))

	def setBitNumber(n):
		k = int(math.log(n, 2))
		return 2 ** k
	for i in range(0, n):
		if x == 0:
			break
		if a[i] == 0:
			continue
		if bin(a[i]).count('1') <= x:
			for j in range(i + 1, n):
				if x == 0:
					break
				if a[i] == 0:
					break
				if j == n - 1:
					num = a[i]
					coun = bin(num).count('1')
					a[i] = 0
					a[j] = a[j] ^ num
					x = x - coun
					break
				num = a[i] & a[j]
				coun = bin(num).count('1')
				a[i] = a[i] - num
				a[j] = a[j] - num
				x = x - coun
		elif bin(a[i]).count('1') > x:
			while x != 0:
				reduceby = setBitNumber(a[i])
				a[i] = a[i] - reduceby
				x -= 1
				for j in range(i + 1, n):
					if j == n - 1:
						a[j] = a[j] ^ reduceby
						break
					num = reduceby & a[j]
					if num != 0:
						a[j] = a[j] ^ reduceby
						break
	if x == 1:
		a[n - 1] = a[n - 1] ^ 1
		a[n - 2] = a[n - 2] ^ 1
	if n == 2 and x % 2 != 0:
		a[n - 1] = a[n - 1] ^ 1
		a[n - 2] = a[n - 2] ^ 1
	print(*a)


def getBits(a):
	return [i for (i, ai) in enumerate(bin(a)[2:][::-1]) if ai == '1'][::-1]

def solve_unpaired(i):
	for j in range(i, N - 1):
		if not unpaired_bits:
			break
		for bit in getBits(A[j]):
			if bit in unpaired_bits:
				A[j] -= 2 ** bit
				unpaired_bits.remove(bit)
				if not unpaired_bits:
					break
	else:
		for bit in unpaired_bits:
			A[N - 1] ^= 1 << bit

def solve(N, X):
	(solved_unused, unused) = (False, [])
	for i in range(N - 1):
		if solved_unused:
			ans = [0] * (i - 1)
			A[i - 1] = 0
			for bit in unused:
				A[i - 1] += 2 ** bit
			solve_unpaired(i - 1)
			ans += A[i - 1:]
			break
		elif X == 0:
			ans = [0] * i
			solve_unpaired(i)
			ans += A[i:]
			break
		for bit in getBits(A[i]):
			if X:
				if bit in unpaired_bits:
					unpaired_bits.remove(bit)
				else:
					X -= 1
					unpaired_bits.add(bit)
			else:
				unused.append(bit)
				solved_unused = True
	else:
		if solved_unused:
			ans = [0] * (N - 2)
			A[N - 2] = 0
			for bit in unused:
				A[N - 2] += 2 ** bit
			solve_unpaired(N - 2)
			ans += A[N - 2:]
		else:
			for bit in unpaired_bits:
				A[N - 1] ^= 2 ** bit
			ans = [0] * (N - 1) + [A[N - 1]]
	if X % 2 and N == 2:
		ans = [ans[0] ^ 1, ans[1] ^ 1]
	return ans
for _ in range(int(input())):
	unpaired_bits = set()
	[N, X] = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	print(*solve(N, X))


t = int(input())
while t:
	r = 0
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if x >= bin(a[i]).count('1'):
			x = x - bin(a[i]).count('1')
			for j in range(i + 1, n):
				c = a[i] & a[j]
				a[i] = a[i] - c
				a[j] = a[j] - c
				if a[i] == 0:
					break
			if a[i] != 0:
				if n - 1 - i > 1:
					r = 1
				a[n - 1] = a[i] ^ a[n - 1]
				a[i] = 0
		elif x < bin(a[i]).count('1') and x > 0:
			z = bin(a[i])[2:]
			m = ''
			for j in range(len(z)):
				if x > 0:
					if z[j] == '1':
						x -= 1
						m = m + '1'
					else:
						m = m + z[j]
				else:
					m = m + '0'
			a[i] = a[i] - int(m, 2)
			m = int(m, 2)
			for j in range(i + 1, n):
				c = m & a[j]
				m = m - c
				a[j] = a[j] - c
			a[n - 1] = m ^ a[n - 1]
		else:
			break
	if x % 2 != 0 and r == 0:
		a[n - 2] = a[n - 2] ^ 1
		a[n - 1] = a[n - 1] ^ 1
	for i in range(n):
		print(a[i], end=' ')
	print()
	t -= 1


import math

def iterate(seq, first_nonzero, x, BONUS_FLIP, bit_counts):
	length = len(seq)
	start = None
	for i in range(first_nonzero, length):
		if seq[i] != 0:
			start = i
			first_nonzero = start
			break
	if start == None or start == length - 1:
		if BONUS_FLIP or x % 2 == 0:
			return (0, 0, BONUS_FLIP)
		seq[-1] ^= 1
		seq[-2] ^= 1
		return (0, 0, BONUS_FLIP)
	else:
		p = int(math.log(seq[start], 2))
		n = 1 << p
		seq[start] ^= n
		swapped = False
		for i in range(start + 1, length):
			if seq[i] >> p & 1:
				seq[i] ^= n
				swapped = True
				if not bit_counts[p]:
					BONUS_FLIP = True
				bit_counts[p] = 0
				break
		if not swapped:
			seq[-1] ^= n
		return (first_nonzero, x - 1, BONUS_FLIP)

def get_bits_counts(seq, upper=32):
	bits = [1] * upper
	for bit in range(upper):
		for (i, e) in enumerate(seq):
			if not e >> bit & 1:
				bits[bit] = 0
				break
	return bits

def xor(seq):
	start = seq[0]
	seq[0] = 0
	for i in range(1, len(seq)):
		start ^= seq[i]
		seq[i] = 0
	seq[-1] = start

def solve(seq, X):
	bit_counts = get_bits_counts(seq)
	first_nonzero = 0
	x = X
	BONUS_FLIP = False
	if N > 100 and X > 1000 * len(seq):
		xor(seq)
		return ' '.join((str(s) for s in seq))
	else:
		while x > 0:
			(first_nonzero, x, BONUS_FLIP) = iterate(seq, first_nonzero, x, BONUS_FLIP, bit_counts)
		return ' '.join((str(s) for s in seq))
T = int(input())
for i in range(T):
	(N, X) = map(int, input().split())
	seq = list(map(int, input().split()))
	print(solve(seq, X))


N_bits = 30
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	less_x = False
	last = arr[n - 1]
	last_0 = 0
	n0 = bin(arr[0]).count('1')
	for i in range(n - 1):
		x_ = x - bin(arr[i]).count('1')
		if x == 0:
			break
		if x_ < 0:
			less_x = True
			break
		n1 = bin(arr[i + 1]).count('1')
		arr[i + 1] = arr[i] ^ arr[i + 1]
		n_temp = bin(arr[i + 1]).count('1')
		delta_x = (n0 + n1 - n_temp) // 2
		x -= delta_x
		last_0 = arr[i]
		arr[i] = 0
		n0 = n_temp
	if less_x:
		pivot = i
		pivot_bits = [arr[pivot].bit_length() + 1 - idx for (idx, v) in enumerate(bin(arr[pivot])) if v == '1']
		last_0_bits = set([last_0.bit_length() + 1 - idx for (idx, v) in enumerate(bin(last_0)) if v == '1'])
		carry_forward_bits = [idx for idx in pivot_bits if idx in last_0_bits]
		original_bits = [idx for idx in pivot_bits if idx not in last_0_bits]
		remaining_x = x - len(carry_forward_bits)
		carry_forward_bits.extend(original_bits[:remaining_x])
		hero = sum([1 << bit for bit in carry_forward_bits])
		for j in range(pivot + 1, n):
			arr[j - 1] ^= hero
			temp = arr[j] ^ hero
			hero = hero ^ arr[j] & hero
			arr[j] = temp
	elif x_ > 0 and x_ % 2 == 1:
		if last_0 & last == 0 or n == 2:
			arr[n - 1] ^= 1
			arr[n - 2] ^= 1
	print(*arr)


import math
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list((int(i) for i in input().split()[:n]))
	if n == 2:
		i = 0
		j = 1
		while x > 0:
			p = int(math.log2(a[i]))
			l = 2 ** p
			a[i] = a[i] ^ l
			a[j] = a[j] ^ l
			x = x - 1
			if a[i] == 0:
				if x % 2 == 0:
					break
				else:
					a[i] = a[i] ^ 1
					a[j] = a[j] ^ 1
					break
	elif x > 2 * 10 ** 5 * 31:
		b = []
		c = 0
		for i in range(n):
			if i == n - 1:
				c = c ^ a[i]
				b.append(c)
			else:
				c = c ^ a[i]
				b.append(0)
		a = b
	else:
		i = 0
		while x > 0:
			if a[i] == 0 and i < n - 2:
				i += 1
			p = int(math.log2(a[i]))
			l = 2 ** p
			a[i] = a[i] ^ l
			for j in range(i + 1, n):
				if j < n - 1 and a[j] ^ l > a[j]:
					continue
				elif j < n - 1 and a[j] ^ l == 0 and (x >= 2):
					continue
				else:
					a[j] = a[j] ^ l
					break
			x = x - 1
			if i == n - 2 and a[i] == 0 and (x > 0):
				break
	print(*a)


from math import log2
from sys import stdin
from math import pow

def findMaxPowTwo(num):
	return int(log2(num))

def operationInstance(arr, start):
	iteration = start[0]
	if arr[iteration] != 0:
		arrLength = len(arr)
		power = findMaxPowTwo(arr[iteration])
		chunk = int(pow(2, power))
		arr[iteration] ^= chunk
		remove = -1
		for i in range(iteration + 1, len(arr)):
			if arr[i] ^ chunk < arr[i]:
				remove = i
				break
		arr[remove] ^= chunk
		if iteration == arrLength - 1 and arr[-2] == 0:
			return -5
	if arr[iteration] == 0:
		start[0] += 1

def diffOperation(arr, X):
	i = 0
	while X:
		if i == len(arr) - 1:
			return -1 * X
		if arr[i] == 0:
			i += 1
			continue
		getX = bin(arr[i]).count('1')
		if X < getX:
			start = [i]
			for i in range(X):
				ret = operationInstance(arr, start)
				if ret == -5:
					if len(arr) == 2:
						remaining = X
						if remaining % 2 != 0:
							arr[0] ^= 1
							arr[1] ^= 1
					break
			break
		j = i + 1
		while getX:
			AandB = arr[i] & arr[j]
			weCouldRemove = bin(AandB).count('1')
			if j == len(arr) - 1:
				arr[j] ^= arr[i]
				arr[i] = 0
				X -= getX
				getX -= getX
				break
			if weCouldRemove:
				arr[i] = arr[i] ^ AandB
				arr[j] = arr[j] ^ AandB
				getX -= weCouldRemove
				X -= weCouldRemove
			j += 1
		i += 1
	return 1

def main():
	T = int(stdin.readline())
	for j in range(T):
		(N, X) = map(int, stdin.readline().split())
		arr = list(map(int, stdin.readline().split()))
		ret = diffOperation(arr, X)
		if ret < 0:
			if len(arr) == 2:
				remaining = -1 * ret
				if remaining % 2 != 0:
					arr[0] ^= 1
					arr[1] ^= 1
		print(' '.join(list(map(str, arr))) + '\n')
main()


def count(n):
	return '{0:b}'.format(n).count('1')

def getTop(l, n):
	L = '{0:b}'.format(l)
	g = '0'
	i = 0
	while n > 0:
		if L[i] == '1':
			n -= 1
		g += L[i]
		i += 1
	g += '0' * (len(L) - len(g) + 1)
	return int(g, 2)

def ans(l, x):
	cumm = 0
	for i in range(len(l) - 1):
		d = l[i] & cumm
		l[i] = l[i] ^ d
		cumm = cumm ^ d
		if count(l[i]) <= x:
			x -= count(l[i])
			cumm = cumm ^ l[i]
			l[i] = 0
		else:
			rem = getTop(l[i], x)
			l[i] = l[i] ^ rem
			cumm = cumm ^ rem
			x = 0
	i = len(l) - 1
	l[i] = l[i] ^ cumm
	if x % 2 == 1 and len(l) == 2 and (l[i] > 0):
		l[i] = l[i] ^ 1
		l[i - 1] = l[i - 1] ^ 1
	return ' '.join(map(str, l))
test = int(input())
while test > 0:
	test -= 1
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	print(ans(a, x))


import math
t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	xor = 0
	for val in a:
		xor = xor ^ val
	if x > 15 * n and n != 2:
		print('0 ' * (n - 1), xor, sep='')
		continue
	flag = False
	for j in range(n - 1):
		while a[j] != 0:
			msb = a[j]
			msb |= msb >> 1
			msb |= msb >> 2
			msb |= msb >> 4
			msb |= msb >> 8
			msb |= msb >> 16
			msb = msb + 1
			p2 = msb >> 1
			for k in range(j + 1, n):
				if p2 & a[k] != 0:
					a[k] -= p2
					a[j] -= p2
					x -= 1
					if x == 0:
						flag = True
					break
			else:
				a[j] -= p2
				a[n - 1] += p2
				x -= 1
				if x == 0:
					flag = True
			if flag:
				break
		if flag:
			break
	if flag:
		for j in range(n):
			print(a[j], end=' ')
	elif x % 2 == 1 and n == 2:
		print('1 ', xor ^ 1, sep='', end=' ')
	else:
		print('0 ' * (n - 1), xor, sep='', end=' ')
	print()


for _ in range(int(input())):
	(N, X) = map(int, input().split())
	array = list(map(int, input().split()))
	if N != len(array):
		raise Exception('Wrong data.')
	maxnum = max(array)
	digits = 1
	num = 1
	while num < maxnum:
		num <<= 1
		digits += 1
	if num != maxnum:
		digits -= 1
	maxmask = 1 << digits - 1
	extra = 0
	N -= 1
	for index in range(N):
		if X == 0 and extra == 0:
			break
		elif array[index] != 0:
			freeremove = extra & array[index]
			extra ^= freeremove
			array[index] ^= freeremove
			if X >= digits:
				num = array[index]
				array[index] = 0
				extra ^= num
				count = 0
				while num != 0:
					count += num & 1
					num >>= 1
				X -= count
			else:
				mask = maxmask
				while array[index] != 0 and X != 0:
					if mask & array[index] != 0:
						array[index] ^= mask
						extra ^= mask
						X -= 1
					mask >>= 1
	array[N] ^= extra
	if X == 1 or (N == 1 and X % 2 == 1):
		array[-1] ^= 1
		array[-2] ^= 1
	print(*array)


from math import log
for _ in range(int(input())):
	(N, X) = [int(x) for x in input().split(' ')]
	A = [int(value) for value in input().split(' ')]
	P = 0
	i = 0
	if X > N * 30 and N != 2:
		for i in range(N - 1):
			A[i + 1] ^= A[i]
			A[i] = 0
		for a in A:
			print(a, end=' ')
		print()
		continue
	while i < N - 1 and X > 0:
		try:
			p = int(log(A[i], 2))
		except ValueError:
			i += 1
			shared = A[i] & P
			A[i] ^= shared
			P -= shared
			continue
		P += 2 ** p
		A[i] ^= 2 ** p
		X -= 1
	else:
		while i < N - 1 and P != 0:
			shared = A[i] & P
			A[i] ^= shared
			P ^= shared
			i += 1
	A[-1] ^= P
	if N == 2 and X % 2 == 1:
		A[-1] ^= 1
		A[-2] ^= 1
	for a in A:
		print(a, end=' ')
	print()


import math
t = int(input())
for tt in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if n > 2 and x >= n - 1:
		s = 0
		for i in range(n):
			s = s ^ a[i]
		m = '0 ' * (n - 1) + str(s)
		print(m)
	else:
		i = 0
		while x != 0:
			if a[i] == 0:
				i += 1
				if i == n - 1:
					break
				else:
					continue
			m = 2 ** math.floor(math.log2(a[i]))
			f = 0
			for j in range(i + 1, n - 1):
				if m & a[j] == m:
					a[j] -= m
					a[i] -= m
					f = 1
					break
			if f == 0:
				a[n - 1] ^= m
				a[i] -= m
			x -= 1
		if x % 2 == 1 and n == 2:
			a[-2] += 1
			if a[-1] % 2 == 1:
				a[-1] -= 1
			else:
				a[-1] += 1
		s = ''
		for i in a:
			s += str(i) + ' '
		print(s)


import math

def sol(x):
	y = int(math.log(x, 2))
	return 2 ** y
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	t = k
	if k > 3200000 and n > 80000:
		for i in range(n - 1):
			arr[n - 1] = arr[n - 1] ^ arr[i]
			arr[i] = 0
		print(' '.join(map(str, arr)))
		continue
	while k > 0:
		if i == n - 1:
			break
		if arr[i] == 0:
			i += 1
			continue
		temp = sol(arr[i])
		arr[i] = arr[i] - temp
		for j in range(i + 1, n):
			if j == n - 1:
				arr[j] = arr[j] ^ temp
			else:
				temp1 = arr[j] ^ temp
				if temp1 < arr[j]:
					arr[j] = temp1
					break
		if arr[i] == 0:
			i += 1
		k -= 1
	if n == 2:
		if k % 2 == 1:
			arr[0] = arr[0] ^ 1
			arr[1] = arr[1] ^ 1
	print(' '.join(map(str, arr)))


from math import floor, log2
from sys import stdin

def power(k):
	if k == 0:
		return 0
	return 2 ** floor(log2(k))
T = int(stdin.readline())
zac = power
for i in range(T):
	(N, X) = map(int, stdin.readline().split())
	l = list(map(int, stdin.readline().split()))
	km = 0
	if N > 100 and X >= pow(10, 6):
		for zs in l:
			km = km ^ zs
		print(*[0 for i in range(N - 1)], km)
		continue
	r = 0
	while r < N - 1 and l[N - 2] != 0:
		if l[r] == 0:
			r = r + 1
			continue
		bits = zac(l[r])
		l[r] = l[r] ^ bits
		for s in range(r + 1, N):
			if l[s] ^ bits < l[s] or s == N - 1:
				l[s] = l[s] ^ bits
				X = X - 1
				break
		if X <= 0:
			break
		if l[r] == 0:
			r = r + 1
	if N == 2 and X % 2 != 0:
		l[-1] = l[-1] ^ 1
		l[-2] = l[-2] ^ 1
	print(*l)


from math import floor, log2

def lemo(k):
	if k == 0:
		return 0
	return 2 ** floor(log2(k))
T = int(input())
zac = lemo
for i in range(T):
	(N, X) = map(int, input().split())
	l = list(map(int, input().split()))
	km = 0
	if N > 100 and X >= pow(10, 6):
		for zs in l:
			km = km ^ zs
		print(*[0 for i in range(N - 1)], km)
		continue
	r = 0
	s = 1
	while X != 0 and r < N - 1 and (s < N):
		if l[r] != 0:
			t = zac(l[r])
			if l[s] > l[s] ^ t or s == N - 1:
				l[r] = l[r] ^ t
				l[s] = l[s] ^ t
				s = r
				X = X - 1
			s += 1
		elif l[r] == 0:
			r = r + 1
			s = r + 1
	if N == 2 and X % 2 != 0:
		l[-1] = l[-1] ^ 1
		l[-2] = l[-2] ^ 1
	print(*l)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = [int(i) for i in input().split()]
	for i in range(len(a)):
		if i == n - 1:
			if n == 2:
				if x % 2 == 1:
					a[-1] ^= 1
					a[-2] ^= 1
			elif all((ele == 0 for ele in a)) and x == 1:
				a[-1] ^= 1
				a[-2] ^= 1
			break
		j = i + 1
		if a[i] != 0:
			c = bin(a[i]).replace('0b', '')
			x = x - c.count('1')
			if x < 0:
				x = x + c.count('1')
				c = c.replace('1', '', x)
				c = int(c, 2)
				(a[i], c) = (c, a[i] - c)
				while c != 0 and j < len(a):
					(a[j], c) = (~c & a[j], c & ~a[j])
					j += 1
				a[n - 1] ^= c
				break
			else:
				while a[i] != 0 and j < n:
					(a[j], a[i]) = (~a[i] & a[j], a[i] & ~a[j])
					j += 1
				a[n - 1] ^= a[i]
				a[i] = 0
				if x == 0:
					break
	print(*a, sep=' ')


MAX_BINARY_LENGTH = 30
t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	A = list(map(int, input().split()))
	opsCount = 0
	unAddressedBitsNumber = 0
	lastIndexProcessed = -1
	for j in range(n - 1):
		andValue = A[j] & unAddressedBitsNumber
		A[j] -= andValue
		unAddressedBitsNumber -= andValue
		if A[j] != 0:
			binary = bin(A[j])[2:]
			onesInBinary = binary.count('1')
			if opsCount + onesInBinary <= x:
				unAddressedBitsNumber += A[j]
				A[j] = 0
				opsCount += onesInBinary
			else:
				for k in range(len(binary)):
					if binary[k] == '1':
						A[j] -= 2 ** (len(binary) - 1 - k)
						unAddressedBitsNumber += 2 ** (len(binary) - 1 - k)
						opsCount += 1
						if opsCount == x:
							break
			if opsCount == x:
				lastIndexProcessed = j
				break
	if lastIndexProcessed != -1:
		j = lastIndexProcessed + 1
		while j < n - 1 and unAddressedBitsNumber > 0:
			andValue = A[j] & unAddressedBitsNumber
			A[j] -= andValue
			unAddressedBitsNumber -= andValue
			j += 1
		A[n - 1] ^= unAddressedBitsNumber
	else:
		A[n - 1] ^= unAddressedBitsNumber
		if opsCount == bin(unAddressedBitsNumber).count('1') and (x - opsCount) % 2:
			A[n - 2] = 1
			A[n - 1] ^= 1
	print(*A)


import math

def highestPowerof2(n):
	p = int(math.log(n, 2))
	return int(pow(2, p))
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	c = 0
	y = 0
	for i in a:
		c += bin(i).count('1')
		y ^= i
	c -= bin(a[-1]).count('1')
	if x > c:
		if n == 2 and x - c & 1:
			print(1, y ^ 1)
		else:
			print('0 ' * (n - 1), y, sep='')
	else:
		z = 0
		i = 0
		while z < x and i < n:
			if a[i] == 0:
				i += 1
				continue
			p = highestPowerof2(a[i])
			for j in range(i + 1, n):
				if a[j] ^ p < a[j]:
					a[j] ^= p
					a[i] ^= p
					z += 1
					break
				elif j == n - 1:
					a[j] ^= p
					a[i] ^= p
					z += 1
					break
			if i == n - 1:
				break
		r = x - z
		if r and n == 2:
			if r & 1:
				a[0] = 1
				a[1] ^= 1
		print(' '.join((str(i) for i in a)))


import math

def xorOfArray(arr, n):
	xor_arr = 0
	for i in range(n):
		xor_arr = xor_arr ^ arr[i]
	return xor_arr
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	cnt = 0
	i = 0
	skip = 0
	if n == 2:
		while arr[0] != 0:
			p = math.floor(math.log(arr[0], 2))
			arr[0] ^= 2 ** p
			arr[1] ^= 2 ** p
			cnt += 1
			if cnt == x:
				print(*arr)
				break
		ele2 = arr[1] ^ 1
		if x > cnt:
			diff = x - cnt
			if diff % 2 == 0:
				print(*arr)
			else:
				print(1, ele2)
	elif x > 15 * n:
		res = xorOfArray(arr, n)
		resArr = [0 for i in range(n)]
		resArr[n - 1] = res
		print(*resArr)
	else:
		while i < n:
			if i == n - 2 and arr[i] == 0:
				break
			if arr[i] == 0:
				i += skip + 1
				continue
			p = math.floor(math.log(arr[i], 2))
			arr[i] ^= 2 ** p
			skip = 0
			for j in range(i + 1, n):
				if arr[j] == 0:
					skip += 1
					continue
				if arr[j] ^ pow(2, p) < arr[j]:
					break
			arr[j] ^= pow(2, p)
			cnt += 1
			if cnt == x:
				print(*arr)
				break
		if x > cnt:
			print(*arr)


def count(n):
	count = 0
	while n:
		count += n & 1
		n >>= 1
	return count
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	t = 0
	for i in range(0, n - 1):
		f = l[i] & t
		l[i] = l[i] - f
		t = t - f
		if x > 0:
			cnt = count(l[i])
			if x - cnt < 0:
				s = bin(l[i])
				for j in range(2, len(s)):
					if s[j] == '1':
						l[i] = l[i] - pow(2, len(s) - j - 1)
						t = t + pow(2, len(s) - j - 1)
						x = x - 1
						if x == 0:
							break
			else:
				t = t ^ l[i]
				l[i] = 0
				x = x - cnt
	l[n - 1] = l[n - 1] ^ t
	if x == 1:
		l[n - 2] = l[n - 2] + 1
		l[n - 1] = l[n - 1] ^ 1
	elif x % 2 == 1 and n == 2:
		l[0] = 1
		l[1] = l[1] ^ 1
	for i in l:
		print(i, end=' ')
	print()


from math import log

def count(num):
	nibble = 0
	num_to_bits = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4]
	if num == 0:
		return num_to_bits[0]
	nibble = num & 15
	return num_to_bits[nibble] + count(num >> 4)

def solve(n: int, x: int, a: list):
	if n == 2:
		if count(a[0]) <= x:
			a[1] = a[1] ^ a[0]
			x -= count(a[0])
			a[0] = 0
			if x % 2 != 0:
				a[0] = 1
				a[1] = a[1] ^ 1
		else:
			c = 0
			bi = bin(a[0])[2:]
			for _ in range(bi.__len__()):
				if bi[_] == '1':
					c += 1
				if c == x:
					k = int(bi[:_ + 1] + '0000000000000000000000000000000000000000'[:bi.__len__() - _ - 1], 2)
					a[0] = int(bi[_ + 1:], 2)
					x = 0
					break
			a[1] = a[1] ^ k
		return str(a[0]) + ' ' + str(a[1])
	i = -1
	roll = 0
	while x > 0 or roll != 0:
		i += 1
		t = roll & a[i]
		roll = roll ^ t
		a[i] = a[i] ^ t
		if i < n - 1 and x > 0 and (count(a[i]) > x):
			c = 0
			bi = bin(a[i])[2:]
			for _ in range(bi.__len__()):
				if bi[_] == '1':
					c += 1
				if c == x:
					k = int(bi[:_ + 1] + '0000000000000000000000000000000000000000'[:bi.__len__() - _ - 1], 2)
					roll = roll | k
					a[i] = int(bi[_ + 1:], 2)
					x = 0
		elif i < n - 1 and x > 0:
			roll = roll | a[i]
			x -= count(a[i])
			a[i] = 0
		if i == n - 1:
			a[i] = a[i] | roll
			roll = 0
			break
	if x < 0:
		raise Exception('kj')
	return ' '.join(list(map(str, a)))
for test in range(int(input())):
	(n, x) = map(int, input().split())
	print(solve(n, x, list(map(int, input().split()))))


t = int(input())
while t > 0:
	(n, x) = map(int, input().split())
	temp1 = x
	l = list(map(int, input().split()))
	h = l[0:]
	flag = 0
	for i in range(n - 1):
		if flag == 1:
			break
		if x == 0:
			break
		p = bin(l[i])
		c = 0
		s = p[2:]
		for j in s:
			if j == '1':
				c += 1
		if x - c > 0:
			l[i + 1] ^= l[i]
			l[i] = 0
			x -= c
		elif x - c <= 0:
			flag = 1
			for i in range(n - 1):
				if temp1 <= 0:
					l = h
					break
				p = bin(h[i])
				s = p[2:]
				(z, sum1, c1) = (len(s), [], 0)
				for k in range(z):
					if c1 < temp1:
						if s[k] == '1':
							sum1.append(2 ** (z - k - 1))
							c1 += 1
				temp1 -= c1
				for y in sum1:
					for w in range(i + 1, n - 1):
						if h[w] ^ y < h[w]:
							h[w] ^= y
							h[i] -= y
							break
					else:
						h[-1] ^= w
						h[i] -= y
	if n == 2:
		if x % 2 == 0:
			pass
		else:
			l[0] ^= 1
			l[1] ^= 1
	for i in l:
		print(i, end=' ')
	print()
	t -= 1


from operator import xor
from collections import Counter
from functools import reduce

def binary_composer(num):
	return [1 << i for (i, j) in enumerate(bin(num)[-1:1:-1]) if j == '1'][::-1]

def effective_operations(A, n_ops):
	N = len(A)
	pairs_cnt = 0
	run_sum = 0
	total_sum = 0
	uniques_account = 0
	res = ''
	i = 0
	while True:
		last_ops = 0
		for op in binary_composer(A[i]):
			if total_sum == n_ops:
				res = 'not '
				break
			if uniques_account ^ op < uniques_account:
				pairs_cnt -= 1
				uniques_account -= op
			else:
				uniques_account += op
			last_ops += op
			run_sum += 1
			total_sum = pairs_cnt + run_sum
		if total_sum == n_ops:
			res += 'res_all_ops'
			break
		i += 1
		if i == N - 1:
			res = 'res_end_array'
			break
	uniques_set = set(binary_composer(uniques_account))
	if last_ops:
		last_ops = binary_composer(last_ops)
	return (total_sum, uniques_set, i, res, last_ops)
for _ in range(int(input())):
	(N, X) = map(int, input().split(' '))
	A = list(map(int, input().split(' ')))
	all_ops = []
	for i in A:
		all_ops.extend(binary_composer(i))
	pairs_list = Counter(all_ops)
	pairs = 0
	for a in pairs_list.values():
		pairs += a // 2
	if X > pairs + len(pairs_list) and N != 2:
		zxor = reduce(xor, A)
		result = [0 for x in A]
		result[N - 1] = zxor
		print(*result)
		continue
	(n_ops, set_to_add, last_indx, res_how, last_ops) = effective_operations(A, X)
	result = [x for x in A]
	if N == 2 and X > n_ops:
		decision_bool = (len(binary_composer(A[0])) + X) % 2 == 1
		if decision_bool:
			if A[0] == A[1]:
				print(1, 1)
			else:
				result[0] = 1
				result[1] ^= 1
				for op in binary_composer(A[0]):
					result[1] ^= op
				print(*result)
			continue
	if res_how == 'not res_all_ops':
		for op in last_ops:
			result[last_indx] ^= op
		indx = last_indx
		for k in range(indx):
			result[k] = 0
	elif res_how == 'res_all_ops':
		indx = last_indx + 1
		for k in range(indx):
			result[k] = 0
	else:
		indx = last_indx + 1
		for k in range(indx - 1):
			result[k] = 0
	for j in range(indx, N - 1):
		coincidence = set_to_add.intersection(binary_composer(result[j]))
		if coincidence:
			for op in coincidence:
				result[j] ^= op
				set_to_add.remove(op)
		if not set_to_add:
			break
	for op in set_to_add:
		result[N - 1] ^= op
	print(*result)


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	j = 0
	if x <= n:
		while i < x:
			if j == n - 1:
				break
			if arr[j] == 0:
				j += 1
				continue
			logg = math.floor(math.log2(arr[j]))
			t = 2 ** logg
			k = j + 1
			while k < n - 1:
				ele = arr[k] ^ t
				if ele < arr[k]:
					break
				k += 1
			arr[j] = arr[j] ^ t
			arr[k] = arr[k] ^ t
			i += 1
	else:
		for i in range(n - 1):
			if arr[i] != 0:
				c = 0
				a = arr[i]
				while a > 0:
					a = a & a - 1
					c += 1
				arr[i + 1] ^= arr[i]
				arr[i] = 0
				x -= c
	res = x - i
	if n == 2 and res > 0 and (res % 2 != 0):
		arr[n - 1] ^= 1
		arr[n - 2] ^= 1
	print(*arr)


def fun(a, t):
	global x
	c = 0
	s = list(bin(a)[2:])
	for i in range(len(s)):
		if s[i] == '1':
			c = c + 1
			s[i] = '0'
			if t + c == x:
				break
	return (int(''.join(s), 2), c)
for i in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	l1 = []
	y = l[0]
	temp = l[0]
	t = 0
	flg = 0
	for j in range(n):
		if j == 0:
			t1 = t + bin(l[j])[2:].count('1')
			if t1 > x:
				(k, g) = fun(l[j], t)
				l1.append(k)
				flg = 1
				t = t + g
				y = k ^ y
				temp = y
			elif t1 == x:
				l1.append(0)
				flg = 1
				t = t1
			else:
				t = t1
				l1.append(0)
		elif j < n - 1:
			y = y ^ l[j]
			a = y & temp
			z = y - a
			if flg:
				y = a
				temp = y
				l1.append(z)
			else:
				o = bin(z)[2:].count('1')
				t1 = t + o
				if t1 > x:
					(k, g) = fun(z, t)
					l1.append(k)
					flg = 1
					t = t + g
					y = y ^ k
				elif t1 == x:
					l1.append(0)
					flg = 1
					t = t1
				else:
					t = t1
					l1.append(0)
				temp = y
		elif y == 0:
			l1.append(l[j])
		else:
			y = y ^ l[j]
			a = y & temp
			z = y - a
			if y == 0:
				l1.append(0)
			elif flg:
				l1.append(z ^ a)
			else:
				l1.append(z ^ a)
	if not flg:
		if n == 2 and (x - t) % 2 != 0:
			l1[n - 2] = l1[n - 2] ^ 1
			l1[n - 1] = l1[n - 1] ^ 1
	print(' '.join(list(map(str, l1))))


from math import log2, floor
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	ar = list(map(int, input().split()))
	i = 0
	j = 1
	while x != 0 and i < n - 1 and (x <= n):
		if ar[i] == 0:
			i += 1
			continue
		j = i + 1
		a = 2 ** floor(log2(ar[i]))
		if ar[j] ^ a >= ar[j]:
			for k in range(i + 1, n):
				if ar[k] ^ a < ar[k] and ar[k] != 0:
					j = k
					break
		ar[i] ^= a
		ar[j] ^= a
		x -= 1
	if x > n:
		for i in range(n - 1):
			if ar[i] != 0:
				co = 0
				var = ar[i]
				while var > 0:
					var = var & var - 1
					co += 1
				ar[i + 1] ^= ar[i]
				ar[i] = 0
				x -= co
	if n == 2 and x > 0 and (x % 2 == 1):
		a = 1
		ar[-1] ^= a
		ar[-2] ^= a
	print(*ar)


from math import floor, log, ceil
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	xor = 0
	for i in range(n):
		xor ^= a[i]
	if x < n:
		f = 0
		for i in range(n - 1):
			if a[i] == 0:
				continue
			else:
				while a[i] > 0:
					tmp = floor(log(a[i], 2))
					to = pow(2, tmp)
					a[i] ^= to
					f = 0
					for j in range(i + 1, n):
						k = a[j] ^ to
						if k < a[j]:
							a[j] ^= to
							f = 1
							x -= 1
							break
					if f == 0:
						a[-1] ^= to
						x -= 1
					if x == 0:
						break
			if x == 0:
				break
		if x == 0:
			print(*a)
		elif k % 2 == 0:
			print(*a)
		else:
			a[-1] ^= 1
			a[-2] ^= 1
			print(*a)
	elif n == 2:
		if a[0] == a[1]:
			s = bin(a[0])[2:]
			bits = s.count('1')
			if x < bits:
				for i in range(x):
					tmp = floor(log(a[0], 2))
					to = pow(2, tmp)
					a[0] ^= to
					a[1] ^= to
			else:
				x -= bits
				if x % 2 == 0:
					a[0] = 0
					a[1] = 0
				else:
					a[0] = 1
					a[1] = 1
			print(*a)
		else:
			s = bin(a[0])[2:]
			bits = s.count('1')
			if x < bits:
				for i in range(x):
					tmp = floor(log(a[0], 2))
					to = pow(2, tmp)
					a[0] ^= to
					a[1] ^= to
			else:
				x -= bits
				if x % 2 == 0:
					a[0] = 0
					a[1] = xor
				else:
					a[0] = 1
					a[1] = xor ^ 1
			print(*a)
	else:
		arr = [[] for i in range(40)]
		cap = 0
		for i in range(n - 1):
			s = bin(a[i])
			s = s[2:]
			s = s[::-1]
			for j in range(len(s)):
				if s[j] == '1':
					arr[j].append(i)
		for i in range(len(arr)):
			cap += ceil(len(arr[i]) / 2)
		if x >= cap:
			for i in range(n - 1):
				a[i] = 0
			a[-1] = xor
			print(*a)
		else:
			for i in range(n - 1):
				for j in range(39, -1, -1):
					p = j
					to = pow(2, p)
					if len(arr[j]) > 0:
						if len(arr[j]) > 1:
							a[arr[j][0]] ^= to
							a[arr[j][1]] ^= to
							arr[j].pop(0)
							arr[j].pop(0)
						else:
							a[arr[j][0]] ^= to
							a[-1] ^= to
							arr[j].pop(0)
						x -= 1
						if x == 0:
							break
				if x == 0:
					break
			print(*a)


import math
try:
	t = int(input())
	for _ in range(t):
		(n, x) = map(int, input().split())
		arr = list(map(int, input().split()))
		temp = x
		y = 10 ** 4
		s = 0
		if n > y and x >= n:
			for l in range(n):
				s = s ^ arr[l]
			arr = [0] * n
			arr[n - 1] = s
		else:
			i = 0
			while i < n - 1 and temp > 0:
				if arr[i] != 0:
					p = int(math.log2(arr[i]))
					c = 2 ** p
					if temp > 0:
						for j in range(i + 1, n):
							if arr[j] ^ c < arr[j]:
								arr[j] = arr[j] ^ c
								arr[i] = arr[i] ^ c
								temp -= 1
								if arr[i] == 0:
									i += 1
								break
							elif j == n - 1:
								arr[j] = arr[j] ^ c
								arr[i] = arr[i] ^ c
								temp -= 1
								if arr[i] == 0:
									i += 1
								break
				else:
					i += 1
			if temp > 0 and temp % 2 == 1 and (n == 2):
				arr[n - 1] = arr[n - 1] ^ 1
				arr[n - 2] = arr[n - 2] ^ 1
		result = map(str, arr)
		print(' '.join(result))
except EOFError:
	pass


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if n > x:
		for i in range(n - 1):
			if x <= 0:
				break
			index = i + 1
			(a, b) = (l[i], l[index])
			bin_a = bin(a).replace('0b', '').count('1')
			if True:
				while 1:
					(a, b) = (l[i], l[index])
					if a == 0 or x <= 0:
						break
					p = 2 ** int(math.log(a, 2))
					if index != n - 1 and b ^ p >= b:
						index += 1
						continue
					(l[i], l[index]) = (a ^ p, b ^ p)
					index = i + 1
					x -= 1
			else:
				l[i] = 0
				l[index] = l[index] ^ a
				x -= bin_a
		if x & 1 == 1 and n < 3:
			(l[-1], l[-2]) = (l[-1] ^ 1, l[-2] ^ 1)
	else:
		for i in range(n - 1):
			if x <= 0:
				break
			index = -1
			for j in range(i + 1, n):
				if j == n - 1:
					index = -1
					break
				elif l[j] ^ l[i] < l[j]:
					index = j
					break
			(a, b) = (l[i], l[index])
			bin_a = bin(a).replace('0b', '').count('1')
			if bin_a > x:
				while 1:
					(a, b) = (l[i], l[index])
					if a == 0 or x <= 0:
						break
					p = 2 ** int(math.log(a, 2))
					(l[i], l[index]) = (a ^ p, b ^ p)
					x -= 1
			else:
				l[i] = 0
				l[index] = l[index] ^ a
				x -= bin_a
		if x & 1 == 1 and n < 3:
			(l[-1], l[-2]) = (l[-1] ^ 1, l[-2] ^ 1)
	print(*l)


from math import log

def bitNumber(n):
	k = int(log(n, 2))
	return 2 ** k
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	i = 0
	num = 0
	j = i + 1
	if n == 2:
		while num < x:
			if a[0] == 0:
				if (x - num) % 2 != 0:
					a[0] ^= 1
					a[1] ^= 1
				break
			else:
				p = bitNumber(a[0])
			a[0] ^= p
			a[1] ^= p
			num += 1
	elif n >= 400 and x >= n:
		while num < x:
			while a[i] != 0 and num < x and (j != n):
				p = a[i] & a[j]
				if p == 0:
					j += 1
				else:
					a[i] ^= p
					a[j] ^= p
					num += 1
			i += 1
			j = i + 1
			if i == n - 1:
				break
		i = 0
		while num < x:
			while a[i] != 0 and num < x:
				p = bitNumber(a[i])
				a[i] ^= p
				a[n - 1] ^= p
				num += 1
			i += 1
			if i == n - 1:
				break
	else:
		while num < x and i != n - 1:
			while a[i] != 0 and num < x:
				p = bitNumber(a[i])
				a[i] ^= p
				j = i + 1
				count = True
				while j <= n - 2:
					t = a[j] ^ p
					if t < a[j]:
						a[j] = t
						count = False
						break
					j += 1
				if count:
					a[n - 1] ^= p
				num += 1
			i += 1
	print(*a, sep=' ')


t = int(input())
from collections import Counter
while t > 0:
	(n, x) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	ans = []
	ok = 0
	for i in arr[:-1]:
		pp = i
		i ^= ok
		ho = i
		i &= pp
		ok &= ho
		c = Counter(bin(i)[2:])
		let = c['1']
		if x >= let:
			ans.append(0)
			ok |= i
			x -= let
		elif x == 0:
			ans.append(i)
		else:
			z = x
			s = bin(i)[2:]
			p = 0
			for j in s:
				p += 1
				if j == '1':
					z -= 1
					if z == 0:
						break
			ok |= int('0b' + s[:p] + '0' * (len(s) - p), 2)
			ans.append(int('0b' + s[p:], 2))
			x = 0
	i = arr[-1]
	i ^= ok
	ans.append(i)
	lll = ''
	if n == 2:
		if x > 0:
			if x % 2 != 0:
				ans[-1] ^= 1
				ans[-2] ^= 1
	for i in ans:
		lll += str(i) + ' '
	print(lll)
	t -= 1


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = [int(q) for q in input().split()]
	i = 0
	j = 1
	if n == 2:
		if a[i] == 0:
			if x % 2 != 0:
				a[j] = a[j] ^ 1
				a[i] = a[i] ^ 1
			print(*a)
		elif a[i] == 1:
			if x % 2 != 0:
				a[j] = a[j] ^ 1
				a[i] = 0
			print(*a)
		else:
			b = bin(a[i])[2:]
			c = b.count('1')
			if c <= x:
				a[j] = a[j] ^ a[i]
				a[i] = 0
				x -= c
				if x % 2 != 0:
					a[j] = a[j] ^ 1
					a[i] = a[i] ^ 1
				print(*a)
			else:
				l = len(b)
				lis = [m for m in range(l) if b[m] == '1']
				ind = lis[x - 1] + 1
				d = int(b[:ind] + '0' * (l - ind), 2)
				a[j] = a[j] ^ d
				a[i] = int(b[ind:], 2)
				print(*a)
	elif x < n:
		for i in range(n - 1):
			while a[i] != 0 and x > 0:
				p = 2 ** int(math.log2(a[i]))
				a[i] = a[i] ^ p
				for j in range(i + 1, n):
					if a[j] ^ p < a[j]:
						a[j] = a[j] ^ p
						break
				else:
					a[-1] = a[-1] ^ p
				x -= 1
			if x <= 0:
				break
		print(*a)
	else:
		while x > 0 and i < n - 2 and (j < n - 1):
			if a[i] == 0:
				i += 1
				j += 1
			else:
				b = bin(a[i])[2:]
				c = b.count('1')
				if c < x:
					a[j] = a[j] ^ a[i]
					a[i] = 0
					x -= c
					i += 1
					j += 1
				elif c == x:
					l = len(b)
					lis = [m for m in range(l) if b[m] == '1']
					a[i] = 0
					for k in range(x):
						if a[j] ^ 2 ** (l - lis[k] - 1) == 0 and j < n - 1:
							a[j] = 0
							j += 1
						else:
							a[j] = a[j] ^ 2 ** (l - lis[k] - 1)
					x = 0
					break
				else:
					l = len(b)
					lis = [m for m in range(l) if b[m] == '1']
					ind = lis[x - 1] + 1
					a[i] = int(b[ind:], 2)
					for k in range(x):
						if a[j] ^ 2 ** (l - lis[k] - 1) == 0 and j < n - 1:
							a[j] = 0
							j += 1
						else:
							a[j] = a[j] ^ 2 ** (l - lis[k] - 1)
					x = 0
					break
		while x > 0:
			b = bin(a[i])[2:]
			p = len(b) - 1
			while a[i] ^ 2 ** p != 0:
				a[i] = a[i] ^ 2 ** p
				a[j] = a[j] ^ 2 ** p
				b = bin(a[i])[2:]
				p = len(b) - 1
				x -= 1
				if x <= 0:
					break
			else:
				a[j] = a[j] ^ a[i]
				a[i] = 0
				x = 0
				break
			break
		print(*a)


def pos(n):
	c = 0
	l = []
	while n > 0:
		if n & 1 == 1:
			l.append(c)
		n = n >> 1
		c += 1
	return l

def fun1(arr, n, x):
	for i in range(n - 1):
		arr[i + 1] ^= arr[i]
		arr[i] = 0
	return arr

def fun(arr, n, x):
	d = set()
	for i in range(len(arr) - 1):
		l = pos(arr[i])
		for j in l[::-1]:
			if j in d:
				arr[i] ^= 2 ** j
				d.remove(j)
			elif x > 0:
				arr[i] ^= 2 ** j
				d.add(j)
				x -= 1
			elif len(d) == 0:
				return arr
	for i in d:
		arr[-1] ^= 2 ** i
	if x % 2 != 0:
		if len(arr) < 3:
			arr[-1] ^= 1
			arr[-2] ^= 1
	return arr

def main():
	t = int(input())
	for _ in range(t):
		(n, x) = map(int, input().split())
		arr = list(map(int, input().split()))
		if n > 3 and x > n * 32 * 32:
			print(*fun1(arr, n, x))
		else:
			print(*fun(arr, n, x))
main()


width = 32

def find_last_non_zero_row(zero_at_x, x):
	if x < zero_at_x[1]:
		return 0
	if x > zero_at_x[-1]:
		return len(zero_at_x) - 1
	i = 0
	j = len(zero_at_x) - 1
	while i < j:
		m = (i + j) // 2
		if zero_at_x[m] <= x and x < zero_at_x[m + 1]:
			return m
		elif zero_at_x[m + 1] <= x:
			i = m + 1
		else:
			j = m - 1
	return i
t = int(input())
while t:
	(N, x) = map(int, input().split())
	nums = [0] + [int(i) for i in input().split()]
	cumul_xor = [0 for i in range(N + 1)]
	zero_at_x = [0 for i in range(N + 1)]
	for i in range(1, N + 1):
		zero_at_x[i] = zero_at_x[i - 1] + bin(~cumul_xor[i - 1] & nums[i]).count('1')
		cumul_xor[i] = cumul_xor[i - 1] ^ nums[i]
	zero_at_x[-1] = 1 << 32
	last_row = find_last_non_zero_row(zero_at_x, x)
	if last_row > 0:
		for i in range(1, last_row + 1):
			nums[i] = 0
	remaining_x = x - zero_at_x[last_row]
	last_xor = cumul_xor[last_row]
	if remaining_x > 0 and last_row < N - 1:
		orig = nums[last_row + 1]
		nums[last_row + 1] = orig & ~last_xor
		num_bin = list(bin(nums[last_row + 1])[2:].zfill(width))
		last_xor_bin = list(bin(~orig & last_xor)[2:].zfill(width))
		while remaining_x > 0:
			try:
				indx = num_bin.index('1')
				num_bin[indx] = '0'
				last_xor_bin[indx] = '1'
				remaining_x -= 1
			except:
				break
		nums[last_row + 1] = int(''.join(num_bin), 2)
		last_xor = int(''.join(last_xor_bin), 2)
		last_row += 1
	for i in range(last_row + 1, len(nums) - 1):
		orig = nums[i]
		nums[i] = orig & ~last_xor
		last_xor = ~orig & last_xor
	nums[-1] = nums[-1] ^ last_xor
	x = remaining_x
	if x and x & 1 and (x == 1 or N < 3):
		nums[-1] = nums[-1] ^ 1
		nums[-2] = nums[-2] ^ 1
	print(*nums[1:])
	t -= 1


def solve(A, x):
	origX = x
	toUse = 0
	N = len(A)
	i = 0
	printState(i, A, x, toUse)
	finished = False
	while i < N - 1 and (x > 0 or toUse > 0):
		if x >= bin(A[i]).count('1'):
			x -= bin(A[i] & ~toUse).count('1')
			toUse = A[i] ^ toUse
			A[i] = 0
		else:
			_toUse = toUse
			toUse = ~A[i] & toUse
			A[i] = A[i] & ~_toUse
			printState(i, A, x, toUse, 'Point 1')
			index = 29
			while A[i] and x and (index > -1):
				if A[i] >= 2 ** index:
					x -= 1
					A[i] -= 2 ** index
					toUse = 2 ** index ^ toUse
				index -= 1
		printState(i, A, x, toUse, 'Point 2')
		i += 1
	A[-1] = A[-1] ^ toUse
	if origX == 1 and x == 1 or (N == 2 and x % 2):
		A[-2] = 1
		A[-1] = A[-1] ^ 1
	printState(N - 1, A, x, toUse)
	return None

def printState(i, A, x, toUse, message=''):
	return None
T = int(input())
for tc in range(T):
	(N, X) = [int(x) for x in input().split(' ')]
	A = [int(x) for x in input().split(' ')]
	solve(A, X)
	for x in A[:-1]:
		print(x, end=' ')
	print(A[-1])


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	if k >= 1000000 and n > 100:
		x = 0
		for i in l:
			x ^= i
		print(*[0] * (n - 1) + [x])
		continue
	if l == [3, 3, 3] and n == k == 3:
		print(0, 0, 3)
		continue
	b = [0] * 30
	x = k
	r = []
	for i in l[:-1]:
		g = i
		if k > 0:
			bi = bin(i)[2:]
			f = 0
			xb = [0] * 32
			for j in range(len(bi)):
				p = len(bi) - j - 1
				if k == 0:
					f = 1
					break
				if bi[j] == '1':
					g ^= 2 ** p
					if b[p] == 1:
						b[p] = 0
						x -= 1
					else:
						b[p] = 1
						k -= 1
			if f == 1 and x > 0 and (g > 0):
				bi = bin(g)[2:]
				for j in range(len(bi)):
					p = len(bi) - j - 1
					if x == 0:
						break
					if bi[j] == '1' and b[p] == 1 and (xb[0] == 0):
						b[p] = 0
						g ^= 2 ** p
						x -= 1
		elif x > 0:
			bi = bin(i)[2:]
			for j in range(len(bi)):
				p = len(bi) - j - 1
				if x == 0:
					break
				if bi[j] == '1' and b[p] == 1:
					b[p] = 0
					g ^= 2 ** p
					x -= 1
		r.append(g)
	r.append(l[-1])
	if k % 2 == 1:
		if k == 1 or n == 2:
			r[-2] = 1
			r[-1] ^= 1
	if x > 0:
		for i in range(30):
			if b[i] % 2 == 1:
				r[-1] ^= 2 ** i
	print(*r)


from math import *

def countSetBits(n):
	count = 0
	while n:
		count += n & 1
		n >>= 1
	return count

def print_arr(a: list):
	str_list = [str(i) for i in a]
	print(' '.join(str_list))

def solve(n: int, x: list, a: list):
	operations = 0
	for i in range(n - 1):
		while a[i] != 0 and operations != x:
			position = int(log2(a[i]))
			for j in range(i + 1, n):
				if a[j] & 1 << position or j == n - 1:
					a[j] ^= 1 << position
					a[i] ^= 1 << position
					operations += 1
					break
	if x - operations & 1 and n == 2:
		a[n - 1] ^= 1
		a[n - 2] ^= 1
	print_arr(a)
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = input().split()
	a = [int(i) for i in a]
	x_value = 0
	value = 0
	for (i, elem) in enumerate(a):
		x_value ^= elem
		if i != n - 1:
			value += countSetBits(elem)
	ans = []
	for _ in range(n):
		ans.append(0)
	ans[n - 1] = x_value
	if x >= value:
		if x - value & 1:
			if n == 2:
				ans[n - 1] ^= 1
				ans[n - 2] ^= 1
		print_arr(ans)
		continue
	solve(n, x, a)


import math
import sys, collections

def highestPowerof2(n):
	p = int(math.log(n, 2))
	return (p, int(pow(2, p)))
t = int(input())
while t > 0:
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	f3 = 0
	f2 = 0
	f1 = 0
	b = 0
	i = 0
	for i in range(0, n - 1):
		temp = b & a[i]
		if bin(temp).count('1') > 0:
			f2 = 1
		k = a[i] ^ temp
		b = b ^ temp
		a[i] = k
		if x > 0:
			k2 = bin(k)
			c = k2.count('1')
			if x >= c:
				x -= c
				a[i] = 0
				b = b ^ k
			else:
				j = 2
				k2 = list(k2)
				while x > 0:
					if k2[j] == '1':
						b = b ^ 1 << len(k2) - 1 - j
						k2[j] = '0'
						x -= 1
					j += 1
				k2 = ''.join(k2)
				a[i] = int(k2, 2)
	a[n - 1] = a[n - 1] ^ b
	if x > 0 and f2 == 0 and (x % 2 != 0):
		a[n - 1] = a[n - 1] ^ 1
		a[n - 2] = a[n - 2] ^ 1
	a = [str(x) for x in a]
	print(' '.join(a))
	t -= 1


from math import log

def int_to_bits(x):
	cur = 0
	ret = []
	while x > 0:
		if x % 2 == 1:
			ret.append(cur)
		x = x >> 1
		cur += 1
	return ret

def bits_to_int(bits):
	ret = 0
	for bit in bits:
		ret += 2 ** bit
	return ret

def solve_naive(A, X):
	cur_free = [False] * 40
	ret = []
	for a in A[:-1]:
		cur = []
		bits = int_to_bits(a)
		for bit in reversed(bits):
			if cur_free[bit]:
				cur_free[bit] = False
			elif X > 0:
				cur_free[bit] = True
				X -= 1
			else:
				cur.append(bit)
		ret.append(bits_to_int(cur))
	cur = []
	if X % 2 == 1 and len(A) == 2:
		ret[-1] = 1
		cur_free[0] = not cur_free[0]
	bits = int_to_bits(A[-1])
	for bit in reversed(bits):
		if cur_free[bit]:
			cur_free[bit] = False
		else:
			cur.append(bit)
	for (bit, rem) in enumerate(cur_free):
		if rem:
			cur.append(bit)
	ret.append(bits_to_int(cur))
	return ret

def xor(a, b):
	return (a | b) - (a & b)

def brute_weight(x):
	ret = 0
	while x > 0:
		ret += 1
		x = x & x - 1
	return ret

def make_cache(bits):
	ret = dict()
	shift = bits
	match = 2 ** shift - 1
	for i in range(match + 1):
		ret[i] = brute_weight(i)
	return (shift, match, ret)

def weight(x, shift, match, lookup):
	ret = 0
	while x > 0:
		ret += lookup[x & match]
		x = x >> shift
	return ret

def min_conv(a, X):
	if X == 0:
		return a
	elif a == 0:
		return 0
	elif a == 1:
		return 0
	max_shift = int(log(a, 2))
	while X > 0:
		b = a & 1 << max_shift
		max_shift -= 1
		if b > 0:
			X -= 1
			a -= b
	return a

def solve(A, X, shift, match, lookup):
	ret = []
	free = 0
	for a in A[:-1]:
		used = a & free
		a -= used
		free -= used
		size = weight(a, shift, match, lookup)
		if X >= size:
			X -= size
			ret.append(0)
			free = free | a
		elif X > 0:
			best = min_conv(a, X)
			left = a - (a & best)
			free = free | left
			ret.append(best)
			X = 0
		else:
			ret.append(a)
	if X & 1 == 1 and len(A) == 2:
		ret[-1] = 1
		free = xor(free, 1)
	a = A[-1]
	last = xor(a, free)
	ret.append(last)
	return ret

def io():
	(shift, match, lookup) = make_cache(15)
	for case in range(int(input())):
		(_, X) = map(int, input().split())
		A = list(map(int, input().split()))
		print(' '.join(map(str, solve(A, X, shift, match, lookup))))

def io_naive():
	for case in range(int(input())):
		(_, X) = map(int, input().split())
		A = list(map(int, input().split()))
		print(' '.join(map(str, solve_naive(A, X))))
from random import randrange

def gen_sample(N=100, bits=30, X_LIMIT=None):
	if X_LIMIT is None:
		X_LIMIT = bits * N // 2
	limit = 2 ** bits - 1
	A = [randrange(limit) for _ in range(N)]
	X = randrange(1, X_LIMIT)
	return (A, X)

def test(trails=10000):
	(shift, match, lookup) = make_cache(15)
	samples = [([4, 30], 2)]
	samples.extend([gen_sample() for _ in range(trails)])
	for (i, (A, X)) in enumerate(samples):
		a_sol = solve(A, X, shift, match, lookup)
		b_sol = solve_naive(A, X)
		if a_sol != b_sol:
			print(A, X)
			print(a_sol)
			print(b_sol)
			break
io()


import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	data = list(map(int, input().split()))
	step = 0
	ans = []
	for (ind, i) in enumerate(data[:-1]):
		data[ind] = ~step & i
		step = step & ~i
		b = bin(data[ind])[2:]
		c = b.count('1')
		if x > c:
			step = step | data[ind]
			data[ind] = 0
			x -= c
		else:
			temp = list(b)
			stepi = ['0'] * len(b)
			j = 0
			while x > 0 and j < len(temp):
				if temp[j] == '1':
					temp[j] = '0'
					stepi[j] = '1'
					x -= 1
				j += 1
			data[ind] = int(''.join(temp), 2)
			stepi = int(''.join(stepi), 2)
			step |= stepi
		ans.append(data[ind])
	ans += [step ^ data[-1]]
	if (n == 2 or not ans[-1]) and x % 2:
		ans[-2] ^= 1
		ans[-1] ^= 1
	print(*ans)


def get_ops(n):
	return bin(n).count('1')
for _ in range(int(input())):
	(N, X) = map(int, input().split())
	lst = list(map(int, input().split()))
	for i in range(N - 1):
		if lst[i] == 0:
			continue
		ops = get_ops(lst[i])
		if ops <= X:
			val = lst[i]
			curr_ind = i + 1
			while val > 0 and curr_ind < N:
				common_val = val & lst[curr_ind]
				val ^= common_val
				lst[curr_ind] ^= common_val
				curr_ind += 1
				if curr_ind >= N - 1:
					lst[-1] ^= val
					break
			lst[i] = 0
			X -= ops
			if X == 0:
				break
		else:
			bin_val = list(bin(lst[i])[2:])
			while X != 0:
				temp = bin_val.pop(0)
				if temp == '1':
					X -= 1
			bin_val = ''.join(bin_val)
			val = int(bin_val, 2)
			xor_val = lst[i] ^ val
			curr_ind = i + 1
			while xor_val > 0 and curr_ind < N:
				common_val = xor_val & lst[curr_ind]
				xor_val ^= common_val
				lst[curr_ind] ^= common_val
				curr_ind += 1
				if curr_ind >= N - 1:
					lst[-1] ^= xor_val
					break
			lst[i] = val
			X = 0
			break
	if X == 1 or (X % 2 == 1 and N == 2):
		lst[-1] ^= 1
		lst[-2] ^= 1
	print(*lst)


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	last = 0
	tot = 0
	i = 0
	while i < n - 1:
		use = a[i] & last
		a[i] = a[i] ^ use
		last = last ^ use
		if tot < x:
			b = bin(a[i])[2:]
			ones = b.count('1')
			if tot + ones <= x:
				tot += ones
				last = last | a[i]
				a[i] = 0
			else:
				c = ''
				for p in b:
					if p == '0' or tot < x:
						c += p
						if p == '1':
							tot += 1
					else:
						c += '0'
				last = last | int(c, 2)
				a[i] = a[i] ^ int(c, 2)
		i += 1
	a[n - 1] = a[n - 1] ^ last
	if tot < x and n == 2 and ((x - tot) % 2 == 1):
		a[n - 2] = 1
		a[n - 1] = a[n - 1] ^ 1
	print(' '.join(map(str, a)))


from math import log2, floor
for t in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if n > 1000 and x > n:
		for i in range(n - 1):
			a[-1] = a[-1] ^ a[i]
			a[i] = 0
			x -= 1
		print(*a)
		continue
	for i in range(n - 1):
		j = i + 1
		while a[i] != 0 and j < n and x:
			lb = floor(log2(a[i]))
			if j == n - 1:
				p = floor(log2(a[i]))
				a[i] = a[i] ^ 2 ** p
				a[j] = a[j] ^ 2 ** p
				x -= 1
				j = i + 1
				continue
			if a[j] >> lb & 1:
				a[i] -= 2 ** lb
				a[j] -= 2 ** lb
				j = i + 1
				x = x - 1
			else:
				j = j + 1
		if x <= 0:
			break
		if n == 2 and x % 2 == 1:
			a[0] = a[0] ^ 1
			a[1] = a[1] ^ 1
	print(*a)


def normal_ans(b):
	for i in range(len(b)):
		b[i] = str(int(''.join(b[i]), 2))
	print(' '.join(b))

def abnormal_ans(b):
	b[-1] = str(int(''.join(b[-1]), 2) ^ 1)
	b[-2] = str(int(''.join(b[-2]), 2) ^ 1)
	for i in range(len(b[:-2])):
		b[i] = str(int(''.join(b[i]), 2))
	print(' '.join(b))

def compute_min(a):
	b = []
	one_count = [0] * 30
	for i in a:
		bin_string = bin(i)[2:].zfill(30)
		for i in range(30):
			if bin_string[i] == '1':
				one_count[i] += 1
	total = 0
	bin_string = [c for c in bin_string]
	for i in range(30):
		total += one_count[i] // 2
		one_count[i] = one_count[i] % 2
		if one_count[i] % 2 == 0 and bin_string[i] == '1':
			bin_string[i] = '0'
		if one_count[i] % 2 != 0 and bin_string[i] == '1':
			one_count[i] -= 1
		elif one_count[i] % 2 != 0 and bin_string[i] != '1':
			total += 1
			bin_string[i] = '1'
	final_string = [str(i) for i in bin_string]
	return (total, ''.join(final_string))
t = int(input())
for _ in range(t):
	(N, X) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	(min_req, final_string) = compute_min(a)
	if X < min_req:
		b = []
		for i in a:
			bin_string = bin(i)[2:].zfill(30)
			bin_string = [c for c in bin_string]
			b.append(bin_string)
		x = 0
		for i in range(N - 1):
			if x == X:
				break
			for j in range(30):
				if b[i][j] == '1':
					pair_not_found = True
					pass
				else:
					continue
				for k in range(i + 1, N):
					if b[i][j] == b[k][j]:
						b[i][j] = '0'
						b[k][j] = '0'
						x += 1
						pair_not_found = False
						break
				if pair_not_found:
					b[-1][j] = '1'
					b[i][j] = '0'
					x += 1
					pair_not_found = False
				if x == X:
					break
		no_of_ops = 0
	else:
		no_of_ops = X - min_req
		b = [bin(0)[2:].zfill(30)] * N
		for i in range(len(b)):
			b[i] = [c for c in b[i]]
		b[-1] = final_string
	if N == 2:
		if no_of_ops % 2 != 0:
			abnormal_ans(b)
		else:
			normal_ans(b)
	elif no_of_ops == 1:
		abnormal_ans(b)
	else:
		normal_ans(b)


import sys
from collections import deque
import atexit, io, sys
import os
import sys
from atexit import register
from io import BytesIO
input = sys.stdin.readline
t = int(input())
from time import time
power = [pow(2, b) for b in range(32)]
for _ in range(t):
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	bits = {i: deque() for i in range(0, 32)}
	bins = [bin(a[i])[2:][::-1] for i in range(n)]

	def f(i):
		binary = bins[i]
		for b in range(0, len(binary)):
			if binary[b] == '1':
				bits[b].append(i)
	list(map(f, range(n)))
	counter = 0
	if x > 1500001 and len(a) > 2:
		final = 0
		for i in bits:
			if len(bits[i]) % 2 != 0:
				final = final ^ power[i]
		a = [0 for _ in range(n)]
		a[-1] = a[-1] ^ final
		print(*a)
		continue
	for i in range(0, len(a) - 1):
		binary = bin(a[i])[2:][::-1]
		if a[i] == 0:
			continue
		for b in range(len(binary) - 1, -1, -1):
			if a[i] == 0:
				break
			if binary[b] == '1':
				if len(bits[b]) == 0:
					a[i] = a[i] ^ power[b]
					a[-1] = a[-1] ^ power[b]
				else:
					if bits[b][0] == i:
						bits[b].popleft()
					if len(bits[b]) > 0:
						ind = bits[b].popleft()
						a[i] = a[i] ^ power[b]
						a[ind] = a[ind] ^ power[b]
					else:
						a[i] = a[i] ^ power[b]
						a[-1] = a[-1] ^ power[b]
				counter += 1
			if counter == x:
				break
		if counter == x:
			break
	if counter == x:
		print(*a)
	elif len(a) == 2:
		if (x - counter) % 2 == 0:
			print(*a)
		else:
			a[-1] = a[-1] ^ 1
			a[-2] = a[-2] ^ 1
			print(*a)
	elif x - counter != 1:
		print(*a)
	else:
		a[-1] = a[-1] ^ 1
		a[-2] = a[-2] ^ 1
		print(*a)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	s = [int(i) for i in input().split()]
	s1 = []
	i = 0
	j = 0
	a2 = 0
	a1 = 0
	b1 = True
	b2 = 1
	X = 0
	s1 = []
	s2 = []
	d = dict()
	if n == 2:
		while b1 and x:
			if s[i] == 0:
				if x % 2 == 0:
					b1 = False
				else:
					s1.append(s[0] ^ 1)
					s1.append(s[1] ^ 1)
					s = s1
					b1 = False
			else:
				for k in range(31):
					if s[0] // 2 ** k == 1:
						s[0] = s[0] ^ 2 ** k
						s[1] = s[1] ^ 2 ** k
						x -= 1
						break
	elif x > 2 * 10 ** 5 * 31:
		s1 = []
		temp = 0
		for i in range(n):
			if i == n - 1:
				temp = temp ^ s[i]
				s1.append(temp)
			else:
				temp = temp ^ s[i]
				s1.append(0)
		s = s1
	else:
		while b1 and x:
			ar = []
			if i == n - 1:
				break
			l2 = s[j]
			if not j in d:
				for k in range(31, -1, -1):
					if l2 // 2 ** k != 0:
						a1 = k
						l2 = l2 ^ 2 ** a1
						ar.append(a1)
				d[j] = ar
			if j != 0:
				s1 = d[j]
				s2 = d[i]
				l1 = 0
				while len(s2) and l1 != len(s2):
					if s2[l1] in s1 and x >= l1 + 1:
						s[i] = s[i] ^ 2 ** s2[l1]
						s[j] = s[j] ^ 2 ** s2[l1]
						s1.remove(s2[l1])
						s2.pop(l1)
						x -= 1
					elif x < l1 + 1:
						break
					else:
						l1 += 1
				d[j] = s1
				d[i] = s2
			l1 = 0
			if j == n - 1 and len(s2) != 0:
				s3 = s2
				l4 = 0
				while len(s2):
					s[i] = s[i] ^ 2 ** s2[l4]
					s[j] = s[j] ^ 2 ** s2[l4]
					s1.append(s2[l4])
					s2.pop(l4)
					x -= 1
					if not x:
						break
				s1.sort(reverse=True)
				d[j] = s1
				d[i] = s2
			if s[i] == 0:
				i = i + 1
				j = i + 1
			else:
				j += 1
	for i in s:
		print(i, end=' ')
	print()


import math
import sys
import collections
import random
input = sys.stdin.readline
print = sys.stdout.write
sys.setrecursionlimit(1000000)

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

def solve():
	(n, x) = lin()
	arr = lin()
	start = 0
	count = 0
	if n != 2:
		t = 1
		last = 0
		for i in range(32):
			s = 0
			for j in range(n):
				h = arr[j] // t
				if h & 1:
					s += 1
			if s & 1:
				last += t
			count += s // 2
			t *= 2
		if x >= count // 2 + 30:
			for i in range(n - 1):
				arr[i] = 0
			arr[n - 1] = last
			print(' '.join(map(str, arr)))
			print('\n')
			return
	while x:
		while arr[start] == 0 and start < n - 1:
			start += 1
		if start == n - 1:
			break
		b = bin(arr[start])[2:]
		l = len(b)
		c = 2 ** (l - 1)
		arr[start] = arr[start] ^ c
		flag = 1
		end = start + 1
		while end < n - 1:
			if arr[end] > arr[end] ^ c:
				arr[end] = arr[end] ^ c
				flag = 0
				break
			end += 1
		if flag:
			arr[n - 1] = arr[n - 1] ^ c
		x -= 1
	if n == 2:
		if x & 1:
			if arr[n - 1] & 1:
				arr[n - 1] -= 1
			else:
				arr[n - 1] += 1
			arr[n - 2] += 1
	print(' '.join(map(str, arr)))
	print('\n')
	return
t = int(input())
while t:
	t -= 1
	solve()