Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation. For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. 

She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.

A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 10^5$).

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different.


-----Output-----

Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.


-----Examples-----
Input
4
4 0 11 6

Output
11 6 4 0
Input
1
13

Output
13 


-----Note-----

In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$.

$[11, 4, 0, 6]$ is also a valid answer.

Source: codeforces | Difficulty: MEDIUM


Solutions:
n = int(input())
l = list(map(int, input().split()))
p = [0] * n
temp = ~l[0]
for i in range(1, n):
	p[i] = temp
	temp &= ~l[i]
temp = ~l[-1]
ans = [-1, -float('inf')]
for i in range(n - 2, -1, -1):
	if i != 0:
		p[i] &= temp
		temp &= ~l[i]
		p[i] &= l[i]
		if ans[1] < p[i]:
			ans[0] = i
			ans[1] = p[i]
	else:
		p[i] = l[i] & temp
		if ans[1] < p[i]:
			ans[0] = i
			ans[1] = p[i]
p[-1] &= l[-1]
if ans[1] < p[-1]:
	ans[0] = n - 1
	ans[1] = p[-1]
print(l[ans[0]], end=' ')
for i in range(n):
	if i == ans[0]:
		continue
	print(l[i], end=' ')
print()


from collections import defaultdict

def solve(A, N):
	assert len(A) == N
	bitCover = defaultdict(int)
	for x in A:
		for i in range(32):
			if 1 << i & x:
				bitCover[i] += 1
	best = (0,)
	for (j, x) in enumerate(A):
		result = 0
		for i in range(32):
			if 1 << i & x:
				if bitCover[i] == 1:
					result += 1 << i
		best = max(best, (result, j))
	j = best[1]
	A = [A[j]] + A[:j] + A[j + 1:]
	return ' '.join(map(str, A))

def __starting_point():
	(N,) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	ans = solve(A, N)
	print(ans)
__starting_point()


n = int(input())
al = list(map(int, input().split()))
bits = [0 for i in range(40)]
for a in al:
	idx = 0
	while a > 0:
		bits[idx] += a % 2
		a //= 2
		idx += 1
ans = 0
for (idx, b) in enumerate(bits):
	if b == 1:
		ans += 2 ** idx
tmp = 0
ans2 = al[0]
for a in al:
	k = ans & a
	if k > tmp:
		tmp = k
		ans2 = a
al.remove(ans2)
print(ans2, *al)


from sys import *
from bisect import bisect_left as bl, bisect_right as br, bisect
from math import *
from time import clock as cl
from itertools import zip_longest as zl
from itertools import groupby as gb
from itertools import combinations as comb
from itertools import combinations_with_replacement as cwr
from collections import defaultdict as dd
from collections import deque as dq
from collections import Counter as c
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return input()

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]

def decimalToBinary(n):
	return bin(n).replace('0b', '')
n = int(data())
arr = l()
arr = sorted(arr)
arr = arr[::-1]
deci = list()
k = len(decimalToBinary(arr[0]))
dp = dd(int)
for i in arr:
	deci.append(decimalToBinary(i))
for i in deci:
	j = k - 1
	for m in i[::-1]:
		if m == '1':
			dp[j] += 1
		j -= 1
res = -1
for i in range(n):
	ans = ''
	p = k - 1
	for j in range(len(deci[i]) - 1, -1, -1):
		if dp[p] == 1 and deci[i][j] == '1':
			ans = '1' + ans
		else:
			ans = '0' + ans
		p -= 1
	if int(ans, 2) > res:
		res = int(ans, 2)
		ele = arr[i]
result = [ele]
t = True
for i in range(n):
	if arr[i] == ele and t:
		t = False
		continue
	result.append(arr[i])
print(*result)


import sys
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
l = [0] * 50
for i in range(n):
	s = bin(arr[i])
	(a, j) = (49, len(s) - 1)
	while j > 1:
		if s[j] == '1':
			l[a] += 1
		a -= 1
		j -= 1
ind = -1
ans = -1
for i in range(n):
	s = bin(arr[i])
	(a, j) = (49, len(s) - 1)
	while j > 1:
		if s[j] == '1':
			l[a] -= 1
		a -= 1
		j -= 1
	(a, j) = (49, len(s) - 1)
	(num, count) = (0, 0)
	while j > 1:
		if s[j] == '1' and l[a] == 0:
			num += 2 ** count
			count += 1
			j -= 1
			a -= 1
		else:
			j -= 1
			a -= 1
			count += 1
	if num > ans:
		ans = num
		ind = i
	(a, j) = (49, len(s) - 1)
	while j > 1:
		if s[j] == '1':
			l[a] += 1
		a -= 1
		j -= 1
b = [arr[ind]] + arr[:ind] + arr[ind + 1:]
print(*b)


n = int(input())
a = [int(s) for s in input().split(' ')]
bitmap = [[] for _ in range(33)]
for (i, b) in enumerate(a):
	b_bin = bin(b)[2:]
	for (j, b_str) in enumerate(b_bin[::-1]):
		if b_str == '1':
			bitmap[j].append(i)
index = -1
for i in range(32, -1, -1):
	if bitmap[i].__len__() == 1:
		index = bitmap[i][0]
		break
if index != -1:
	print(a[index], end=' ')
	for i in range(n):
		if i != index:
			print(a[i], end=' ')
else:
	for i in range(n):
		print(a[i], end=' ')


from bisect import bisect_left as bl
import heapq as hp
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
for j in range(40, -1, -1):
	c = 0
	for i in range(n):
		if 1 << j & a[i] > 0:
			ind = i
			c += 1
	if c == 1:
		print(a[ind], end=' ')
		for p in range(n):
			if p != ind:
				print(a[p], end=' ')
		exit()
print(*a)


import sys
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
b = a[:]
C = [0] * n
x = [0] * n
for i in range(0, 40):
	for j in range(0, n):
		x[j] = b[j] % 2
		b[j] = b[j] // 2
	if sum(x) == 1:
		for j in range(0, n):
			if x[j] == 1:
				C[j] = C[j] + 2 ** i
l = C.index(max(C))
print(' '.join(list(map(str, [a[l]] + a[0:l] + a[l + 1:]))))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(31, -1, -1):
	cnt = 0
	cur = -1
	for j in range(n):
		if l[j] >> i & 1 == 0:
			cnt += 1
		else:
			cur = j
	if cnt == n - 1:
		ans = cur
		break
print(l.pop(ans), *l)


n = int(input())
A = list(map(int, input().split()))
B = [[0] * 33 for i in range(n)]
C = [0] * 33
for i in range(n):
	t = A[i]
	j = 0
	while t > 0:
		B[i][j] += t % 2
		C[j] += B[i][j]
		t //= 2
		j += 1
M2 = [1]
for i in range(40):
	M2.append(M2[-1] * 2)
S = [0] * n
for i in range(n):
	for j in range(33):
		if B[i][j] == 1:
			if C[j] == 1:
				S[i] += M2[j]
ind = S.index(max(S))
ANS = [A[ind]]
for i in range(n):
	if i != ind:
		ANS.append(A[i])
print(' '.join([str(i) for i in ANS]))


n = int(input())
arr = [int(i) for i in input().split()]
dic = [0] * 34
for i in arr:
	temp = i
	bit = 0
	while temp:
		if temp & 1:
			dic[bit] += 1
		temp = temp >> 1
		bit += 1
ans = -1
first = 0
for i in range(len(arr)):
	tempdic = [0] * 34
	temp = arr[i]
	bit = 0
	rangeor = 0
	while temp:
		if temp & 1:
			tempdic[bit] += 1
		temp = temp >> 1
		bit += 1
	for j in range(len(tempdic)):
		if dic[j] - tempdic[j] > 0:
			rangeor += 2 ** j
	if (arr[i] | rangeor) - rangeor > ans:
		first = i
		ans = (arr[i] | rangeor) - rangeor
ansarr = [arr[first]]
for i in range(len(arr)):
	if i != first:
		ansarr.append(arr[i])
print(' '.join([str(i) for i in ansarr]))


n = int(input())
(*arr,) = map(int, input().split())

def CountBits(n):
	n = (n & 6148914691236517205) + ((n & 12297829382473034410) >> 1)
	n = (n & 3689348814741910323) + ((n & 14757395258967641292) >> 2)
	n = (n & 1085102592571150095) + ((n & 17361641481138401520) >> 4)
	n = (n & 71777214294589695) + ((n & 18374966859414961920) >> 8)
	n = (n & 281470681808895) + ((n & 18446462603027742720) >> 16)
	n = (n & 4294967295) + ((n & 18446744069414584320) >> 32)
	return n

def f(arr):
	res = arr[0]
	for v in arr[1:]:
		res = (res | v) - v
	print(bin(res))
	return res
cnt = {}
bits = 0
for v in arr:
	bits |= v
stat = [0] * 32
p2idx = {2 ** i: i for i in range(32)}
for i in range(n):
	p = 1
	p_idx = 0
	while p < 10 ** 9:
		if arr[i] & p:
			stat[p_idx] += 1
		p_idx += 1
		p *= 2
mask = 0
for (i, cnt) in enumerate(stat):
	if cnt == 1:
		mask |= 1 << i
arr.sort(key=lambda obj: -(obj & mask))
print(*arr)


n = int(input())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
if n == 1:
	print(A[0])
else:
	x = A[0]
	steps = len(bin(x)) - 3
	mask = x >> steps
	mask = mask << steps
	f1 = True
	f2 = True
	while f1:
		ind = 0
		num = 0
		while f2:
			x = A[ind]
			r = x & mask
			if r != 0:
				num += 1
			if num > 1:
				f2 = False
			ind += 1
			if ind >= n:
				f2 = False
		if num == 1:
			f1 = False
		else:
			mask = mask >> 1
			f2 = True
			if mask == 0:
				f1 = False
	if mask != 0:
		f = True
		ix = 0
		sx = A[ix]
		while f:
			sx = A[ix]
			if mask & sx != 0:
				f = False
			ix += 1
		A.pop(ix - 1)
		A.insert(0, sx)
	B = [str(a) for a in A]
	print(' '.join(B))


import sys
n = int(sys.stdin.readline().split()[0])
a = list(map(int, sys.stdin.readline().split()))
b = [x for x in a]
idx = -1
while max(b) != 0:
	c = [x & 1 for x in b]
	if c.count(1) == 1:
		idx = c.index(1)
	b = [x >> 1 for x in b]
if idx >= 0:
	tmp = a[idx]
	a.remove(tmp)
	print(tmp, *a)
else:
	print(*a)


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append(str(int(bin(a[i]).replace('0b', '')) + 10 ** 30))
(flag, store) = (0, 0)
for i in range(1, 31):
	add = 0
	for j in range(n):
		if b[j][i] == str(1):
			add += 1
			store = j
	if add == 1:
		flag = -1
		break
if flag == 0:
	store = -2
else:
	print(a[store], end=' ')
for i in range(n):
	if i != store:
		print(a[i], end=' ')


n = int(input())
v = list(map(int, input().split()))
max = -1
maxi = 0
for i in range(n):
	x = v[i]
	for j in range(n):
		if j == i:
			continue
		if x == 0:
			break
		x = (x | v[j]) - v[j]
	if x > max:
		max = x
		maxi = i
print(v[maxi], end=' ')
for i in range(n):
	if i != maxi:
		print(v[i], end=' ')
print()


from math import ceil

def mlt():
	return map(int, input().split())
x = int(input())
s = list(mlt())
cur = 1 << 32
fst = -1
while cur:
	ct = 0
	tm = -1
	for n in s:
		if n & cur:
			ct += 1
			tm = n
	if ct == 1:
		fst = tm
		break
	cur >>= 1
if fst != -1:
	print(fst, end=' ')
for n in s:
	if n != fst:
		print(n, end=' ')


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
res = 0
for x in a:
	bx = bin(x)[2:][::-1]
	for i in range(len(bx)):
		if bx[i] == '1':
			d[i] += 1
midx = 0
mel = 0
for (j, x) in enumerate(a):
	bx = bin(x)[2:][::-1]
	cur = 0
	for i in range(len(bx)):
		if bx[i] == '1' and d[i] == 1:
			cur += 2 ** i
	if cur > mel:
		mel = cur
		midx = j
res = [a[midx]]
for i in range(len(a)):
	if i != midx:
		res.append(a[i])
print(' '.join((str(x) for x in res)))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = 1
for _ in range(t):
	n = ip()
	arr = list(inp())
	bit = dd(list)
	for i in arr:
		ch = bin(i)[2:]
		ch = ch[::-1]
		for j in range(len(ch)):
			if ch[j] == '1':
				bit[j].append(i)
	ch = sorted(list(bit.keys()), reverse=True)
	flag = False
	for i in ch:
		if len(bit[i]) == 1:
			flag = True
			ans = bit[i][0]
			break
	if flag:
		ans = [ans]
		f = True
		arr.remove(ans[0])
		ans = ans + arr
	else:
		ans = sorted(list(arr), reverse=True)
	print(*ans)


n = int(input())
a = list(map(int, input().split()))
ind = -1
for i in range(32, -1, -1):
	ct = 0
	x = 1 << i
	for j in range(n):
		if a[j] & x == x:
			ct += 1
			if ct == 1:
				ind = j
	if ct == 1:
		break
	else:
		ind = -1
if ind == -1:
	for i in a:
		print(i, end=' ')
else:
	print(a[ind], end=' ')
	for i in a:
		if i == a[ind]:
			continue
		print(i, end=' ')


n = int(input())
a = list(map(int, input().split()))
lis = [0] * 31
for i in a:
	for j in range(31):
		if i & 2 ** j > 0:
			lis[j] += 1
nmax = 0
maxind = 0
for (ind, i) in enumerate(a):
	for j in range(31):
		if lis[j] >= 2:
			i |= 2 ** j
			i -= 2 ** j
	if nmax < i:
		nmax = i
		maxind = ind
ans = [a[maxind]]
for i in range(n):
	if i != maxind:
		ans.append(a[i])
print(' '.join(map(str, ans)))


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def do(l):
	curr = l[0]
	for i in l[1:]:
		curr = (curr | i) - i
	return curr
n = val()
l = li()
bits = [0] * 50
for i in l:
	for j in range(50):
		bits[j] += i >> j & 1
for i in range(49, -1, -1):
	if bits[i] == 1:
		for j in l:
			if j >> i & 1:
				l.remove(j)
				print(*[j] + sorted(l))
				exit()
print(*l)


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
if N == 1:
	ans = A
else:
	dp1 = [0] * N
	bit = 0
	for (i, a) in enumerate(A):
		bit |= a
		dp1[i] = bit
	dp2 = [0] * N
	bit = 0
	for i in reversed(range(N)):
		bit |= A[i]
		dp2[i] = bit
	score = -1
	ind = -1
	for i in range(N):
		if i == 0:
			tmp = dp2[1]
		elif i == N - 1:
			tmp = dp1[N - 2]
		else:
			tmp = dp1[i - 1] | dp2[i + 1]
		if (A[i] | tmp) - tmp > score:
			score = (A[i] | tmp) - tmp
			ind = i
	ans = [A[ind]]
	for (i, a) in enumerate(A):
		if i != ind:
			ans.append(a)
print(*ans, sep=' ')


def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	before = [0] * n
	after = [0] * n
	accleft = 0
	for i in range(n):
		before[i] = accleft
		accleft |= a[i]
	accright = 0
	i = n - 1
	while i >= 0:
		after[i] = accright
		accright |= a[i]
		i -= 1
	answer = a[0] & ~before[0] & ~after[0]
	bindex = 0
	for i in range(1, n):
		v = a[i] & ~before[i] & ~after[i]
		if v > answer:
			bindex = i
			answer = v
	print(a[bindex], end=' ')
	for i in range(n):
		if i != bindex:
			print(a[i], end=' ')
main()


import sys
n = int(sys.stdin.readline().split()[0])
a = list(map(int, sys.stdin.readline().split()))
b = [x for x in a]
idx = -1
while max(b) != 0:
	c = [x & 1 for x in b]
	if c.count(1) == 1:
		idx = c.index(1)
	b = [x >> 1 for x in b]
if idx >= 0:
	sys.stdout.write(str(a[idx]) + ' ')
	sys.stdout.write(' '.join(map(str, a[:idx])) + ' ')
	sys.stdout.write(' '.join(map(str, a[idx + 1:])))
else:
	sys.stdout.write(' '.join(map(str, a)))


onbits = [[] for i in range(33)]
n = int(input())
nums = [int(i) for i in input().split()]
for i in range(n):
	temp = bin(nums[i])[2:][::-1]
	for j in range(len(temp)):
		if temp[j] == '1':
			onbits[j].append(i)
onbits = onbits[::-1]
for i in onbits:
	if len(i) == 1:
		(nums[0], nums[i[0]]) = (nums[i[0]], nums[0])
		break
print(' '.join([str(i) for i in nums]))


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
C = Counter()
for a in A:
	i = 0
	while a >> i > 0:
		if a & 1 << i:
			C[i] += 1
		i += 1
mx = 0
idx = -1
for (i, a) in enumerate(A):
	sm = 0
	j = 0
	while a >> j > 0:
		if a & 1 << j and C[j] == 1:
			sm += 1 << j
		j += 1
	if sm > mx:
		mx = sm
		idx = i
a = A.pop(idx)
ans = [a] + A
print(*ans)


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
l.reverse()
ll = []
sol = []
leng = len(bin(l[0])[2:])
i = 0
for x in l:
	ll.append('0' * (leng - len(bin(x)[2:])) + bin(x)[2:])
idx = -1
while i < leng:
	idx = -1
	for x in range(n):
		if ll[x][i] == '1':
			if idx == -1:
				idx = x
			else:
				idx = -2
		if idx == -2:
			break
	if idx >= 0:
		sol.append(idx)
	i += 1
for x in sol:
	if ll[x] != -1:
		print(int(ll[x], 2), end=' ')
		ll[x] = -1
for x in ll:
	if x != -1:
		print(int(x, 2), end=' ')


n = int(input())
a = list(map(int, input().split()))
mask = 0
for i in range(31):
	if [j >> i & 1 for j in a].count(1) == 1:
		mask += 1 << i
ans = [-1, -1]
for i in range(n):
	if ans[0] < a[i] & mask:
		ans[0] = a[i] & mask
		ans[1] = i
realans = []
realans.append(a[ans[1]])
for i in range(n):
	if i != ans[1]:
		realans.append(a[i])
print(' '.join(map(str, realans)))


import sys
input = sys.stdin.readline
n = int(input())
arr = [int(k) for k in input().split()]
num = 0
for i in range(30, -1, -1):
	count = 0
	for j in range(n):
		if arr[j] >> i & 1:
			count += 1
			num = arr[j]
	if count == 1:
		break
arr.remove(num)
print(num, ' '.join(map(str, arr)))


n = int(input())
s = list(map(int, input().split()))
dig = [0] * 50
record = [None] * 50
for x in s:
	i = 0
	tmp = x
	while tmp != 0:
		if tmp % 2 == 1:
			record[i] = x
			dig[i] += 1
		tmp //= 2
		i += 1
ans = []
head = None
for i in range(40, -1, -1):
	if dig[i] == 1:
		head = record[i]
		ans.append(head)
		break
for x in s:
	if x != head:
		ans.append(x)
for (i, x) in enumerate(ans):
	ans[i] = str(x)
print(' '.join(ans))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

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

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
n = iinp()
arr = lmp()
ml = l2d(31, 0)
for (i, tmp) in enumerate(arr):
	c = 0
	while tmp:
		if tmp % 2:
			ml[c].append(arr[i])
		c += 1
		tmp >>= 1
ansl = []
vis = dd(int)
for i in range(30, -1, -1):
	if len(ml[i]) == 1:
		x = ml[i][0]
		if not vis:
			ansl.append(x)
			vis[x] = 1
for i in arr:
	if not vis[i]:
		ansl.append(i)
print(*ansl)


n = int(input())
a = list(map(int, input().split()))
pre = [0 for i in range(n + 1)]
suf = [0 for i in range(n + 1)]
for i in range(n):
	if i == 0:
		pre[i] = a[i]
	else:
		pre[i] = pre[i - 1] | a[i]
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suf[i] = a[i]
	else:
		suf[i] = suf[i + 1] | a[i]
ans = -1
p = 0
for i in range(n):
	if pre[n - 1] - (pre[i - 1] | suf[i + 1]) > ans:
		p = i
		ans = pre[n - 1] - (pre[i - 1] | suf[i + 1])
print(a[p], end=' ')
for i in range(n):
	if i != p:
		print(a[i], end=' ')


import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
digits = [[] for _ in range(32)]
for (i, val) in enumerate(a):
	shift = 0
	while val > 0:
		if val & 1:
			digits[shift].append(i)
		val >>= 1
		shift += 1
for i in reversed(range(32)):
	if len(digits[i]) == 1:
		idx = digits[i][0]
		(a[0], a[idx]) = (a[idx], a[0])
		break
print(*a)


n = int(input())
li = [int(i) for i in input().split(' ')]
valid = {}
for i in li:
	for p in range(31):
		c = i & 2 ** p
		if c:
			valid[c] = valid.get(c, 0) + 1
s = 0
for (k, v) in valid.items():
	if v == 1:
		s += k
m = 0
a = li[0]
for i in li:
	if i & s > m:
		m = i & s
		a = i
li.remove(a)
print(a, end=' ')
for i in li:
	print(i, end=' ')


__author__ = 'tanunia'
n = int(input())
a = list(map(int, input().split()))
(left, right) = ([0 for _ in range(len(a))], [0 for _ in range(len(a))])
for i in range(1, len(a)):
	left[i] = left[i - 1] | a[i - 1]
	right[len(a) - i - 1] = right[len(a) - i] | a[len(a) - i]
(best, best_ind) = (-1, -1)
for i in range(len(a)):
	y = left[i] | right[i]
	cur = (a[i] | y) - y
	if cur > best:
		(best, best_ind) = (cur, i)
(a[0], a[best_ind]) = (a[best_ind], a[0])
print(' '.join([str(x) for x in a]))


import math
I = lambda : int(input())
ID = lambda : map(int, input().split())
IL = lambda : list(ID())
n = I()
a = IL()
b = [0] * (n + 1)
c = [0] * (n + 1)
sth = 0
idx = 0
for i in range(n):
	b[i + 1] = b[i] | a[i]
for i in range(n):
	c[i + 1] = c[i] | a[n - i - 1]
for i in range(n):
	m = b[i] | c[n - i - 1]
	if sth <= (m | a[i]) - m:
		sth = (m | a[i]) - m
		idx = i
ans = [a[idx]] + a[0:idx] + a[idx + 1:]
print(*ans)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [int(i) for i in input().split()]
m = 0
mi = 0
b = [-1] * 35
for i in range(len(a)):
	bi = bin(a[i])[2:][::-1]
	for j in range(len(bi)):
		if bi[j] == '1':
			if b[j] == -1:
				b[j] = i
			elif b[j] >= 0:
				b[j] = -2
for i in range(34, -1, -1):
	if b[i] >= 0:
		(a[b[i]], a[0]) = (a[0], a[b[i]])
		break
print(*a)


n = int(input())
a = list(map(int, input().split()))
for d in range(29, -1, -1):
	bit = 1 << d
	(count, j) = (0, 0)
	for (i, x) in enumerate(a):
		if bit & x:
			count += 1
			if count == 2:
				break
			else:
				j = i
	if count == 1:
		print(a[j], *a[:j] + a[j + 1:])
		break
else:
	print(*a)


n = int(input())
arr = list(map(int, input().split()))
pref = [0] * n
suf = [0] * n
pref[0] = arr[0]
for i in range(1, n):
	pref[i] = pref[i - 1] | arr[i]
suf[-1] = arr[-1]
for i in range(n - 2, -1, -1):
	suf[i] = suf[i + 1] | arr[i]
maxind = -1
maxans = -1
for i in range(n):
	prefval = pref[i - 1] if i else 0
	sufval = suf[i + 1] if i < n - 1 else 0
	val = arr[i] & ~(prefval | sufval)
	if val > maxans:
		maxind = i
		maxans = val
print(arr[maxind], *arr[:maxind], *arr[maxind + 1:])


from sys import stdin, stdout
n = int(input())
a = list(map(int, stdin.readline().split()))
used = [False] * n
ans = []
for k in range(30, -1, -1):
	(cnt1, pos1) = (0, -1)
	for i in range(n):
		if used[i]:
			continue
		mask = 1 << k
		if mask & a[i]:
			cnt1 += 1
			pos1 = i
	if cnt1 == 1:
		ans.append(a[pos1])
		used[pos1] = True
for i in range(n):
	if not used[i]:
		ans.append(a[i])
for i in ans:
	print(i, end=' ')


n = int(input())
a = [int(i) for i in input().split()]
once = 0
twice = 2 ** 64
for i in a:
	twice |= once & i
	once |= i
a = sorted([(i & ~twice, i) for i in a], reverse=True)
a = [j for (i, j) in a]
print(*a)


from collections import defaultdict, OrderedDict, Counter
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from queue import Queue, PriorityQueue
from heapq import *
from statistics import *
from math import sqrt, log10, log2, log, gcd, ceil, floor
import fractions
import copy
from copy import deepcopy
import sys
import io
sys.setrecursionlimit(10000)
import math
import os
import bisect
import collections
mod = int(pow(10, 9)) + 7
import random
from random import *
from time import time
import itertools
from itertools import combinations, permutations

def ncr(n, r, p=mod):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def normalncr(n, r):
	r = min(r, n - r)
	count = 1
	for i in range(n - r, n + 1):
		count *= i
	for i in range(1, r + 1):
		count //= i
	return count
inf = float('inf')
adj = defaultdict(set)
visited = defaultdict(int)

def addedge(a, b):
	adj[a].add(b)
	adj[b].add(a)

def bfs(v):
	q = Queue()
	q.put(v)
	visited[v] = 1
	while q.qsize() > 0:
		s = q.get_nowait()
		print(s)
		for i in adj[s]:
			if visited[i] == 0:
				q.put(i)
				visited[i] = 1

def dfs(v, visited):
	if visited[v] == 1:
		return
	visited[v] = 1
	print(v)
	for i in adj[v]:
		dfs(i, visited)

def reverse_bisect_right(a, x, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x > a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo

def reverse_bisect_left(a, x, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x >= a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo

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

def make_list(m):
	m += list(map(int, input().split()))

def get_str_list_in_int():
	return [int(i) for i in list(input())]

def get_str_list():
	return list(input())

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

def input_int():
	return int(input())

def matrix(a, b):
	return [[0 for i in range(b)] for j in range(a)]

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

def find_gcd(l):
	a = l[0]
	for i in range(len(l)):
		a = gcd(a, l[i])
	return a

def is_prime(n):
	sqrta = int(sqrt(n))
	for i in range(2, sqrta + 1):
		if n % i == 0:
			return 0
	return 1

def prime_factors(n):
	while n % 2 == 0:
		return [2] + prime_factors(n // 2)
	sqrta = int(sqrt(n))
	for i in range(3, sqrta + 1, 2):
		if n % i == 0:
			return [i] + prime_factors(n // i)
	return [n]

def p(a):
	if type(a) == str:
		print(a + '\n')
	else:
		print(str(a) + '\n')

def ps(a):
	if type(a) == str:
		print(a)
	else:
		print(str(a))

def kth_no_not_div_by_n(n, k):
	return k + (k - 1) // (n - 1)

def forward_array(l):
	n = len(l)
	stack = []
	forward = [0] * n
	for i in range(len(l) - 1, -1, -1):
		while len(stack) and l[stack[-1]] < l[i]:
			stack.pop()
		if len(stack) == 0:
			forward[i] = len(l)
		else:
			forward[i] = stack[-1]
		stack.append(i)
	return forward

def backward_array(l):
	n = len(l)
	stack = []
	backward = [0] * n
	for i in range(len(l)):
		while len(stack) and l[stack[-1]] < l[i]:
			stack.pop()
		if len(stack) == 0:
			backward[i] = -1
		else:
			backward[i] = stack[-1]
		stack.append(i)
	return backward

def char(a):
	return chr(a + 97)

def get_length(a):
	return 1 + int(log10(a))

def issq(n):
	sqrta = int(n ** 0.5)
	return sqrta ** 2 == n

def ceil(a, b):
	return int((a + b - 1) / b)

def equal_sum_partition(arr, n):
	sum = 0
	for i in range(n):
		sum += arr[i]
	if sum % 2 != 0:
		return False
	part = [[True for i in range(n + 1)] for j in range(sum // 2 + 1)]
	for i in range(0, n + 1):
		part[0][i] = True
	for i in range(1, sum // 2 + 1):
		part[i][0] = False
	for i in range(1, sum // 2 + 1):
		for j in range(1, n + 1):
			part[i][j] = part[i][j - 1]
			if i >= arr[j - 1]:
				part[i][j] = part[i][j] or part[i - arr[j - 1]][j - 1]
	return part[sum // 2][n]

def bin_sum_array(arr, n):
	for i in range(n):
		if arr[i] % 2 == 1:
			return i + 1
	binarray = [list(reversed(bin(i)[2:])) for i in arr]
	new_array = [0 for i in range(32)]
	for i in binarray:
		for j in range(len(i)):
			if i[j] == '1':
				new_array[j] += 1
	return new_array
nc = 'NO'
yc = 'YES'
ns = 'No'
ys = 'Yes'

def yesno(a):
	print(yc if a else nc)

def reduce(dict, a):
	dict[a] -= 1
	if dict[a] == 0:
		dict.pop(a)
t = 1
for i in range(t):
	n = int(input())
	l = get_list()
	if n == 1:
		print(*l)
		continue
	prefix = [0] * n
	suffix = [0] * n
	for i in range(n):
		if i == 0:
			prefix[i] = ~l[i]
		else:
			prefix[i] = prefix[i - 1] & ~l[i]
	for i in reversed(range(n)):
		if i == n - 1:
			suffix[i] = ~l[i]
		else:
			suffix[i] = suffix[i + 1] & ~l[i]
	answer = [0] * n
	for i in range(n):
		if i == 0:
			answer[i] = l[i] & suffix[i + 1]
		elif i == n - 1:
			answer[i] = prefix[i - 1] & l[i]
		else:
			answer[i] = prefix[i - 1] & l[i] & suffix[i + 1]
	a = l[answer.index(max(answer))]
	l.remove(a)
	print(a, *l)


import sys
inp = sys.stdin.readline
input = lambda : inp().strip()
flush = sys.stdout.flush

def iin():
	return int(input())

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

def main():
	n = iin()
	a = lin()
	ch = [0] * 35
	for i in a:
		ch1 = 0
		x = i
		while x:
			if x % 2:
				ch[ch1] += 1
			x //= 2
			ch1 += 1
	sl = [0] * n
	for i in range(n):
		ch1 = 0
		x = a[i]
		while x:
			if x % 2:
				if ch[ch1] == 1:
					sl[i] += 2 ** ch1
			x //= 2
			ch1 += 1
	sl = [[sl[i], i] for i in range(n)]
	sl.sort(reverse=True)
	ans = [a[j] for (i, j) in sl]
	print(*ans)
main()


n = int(input())
a = list(map(int, input().split()))
scale = 30
btc = [10 ** 6 for _ in range(scale)]
for i in range(n):
	x = a[i]
	for d in range(scale):
		if x % 2 == 1:
			if btc[d] == 10 ** 6:
				btc[d] = i
			else:
				btc[d] = 10 ** 6 + 1
		x //= 2
btc.reverse()
for y in btc:
	if y < 10 ** 6:
		tmp = y
		ans = [a[y]]
		for i in range(n):
			if i != y:
				ans.append(a[i])
		break
	else:
		ans = a
print(' '.join(list(map(str, ans))))


t = 1
while t:
	n = int(input())
	l = list(map(int, input().split()))
	s = [0] * (n + 1)
	p = [0] * (n + 1)
	p[0] = ~l[0]
	for i in range(1, n):
		p[i] = p[i - 1] & ~l[i]
	s[n - 1] = ~l[-1]
	for i in range(n - 2, -1, -1):
		s[i] = s[i + 1] & ~l[i]
	max_ind = 0
	maxi = 0
	if n == 1:
		print(l[0])
	else:
		for i in range(n):
			if i == 0:
				if maxi < l[i] & s[i + 1]:
					maxi = l[i] & s[i + 1]
					max_ind = i
			elif i == n - 1:
				if maxi < p[i - 1] & l[i]:
					maxi = p[i - 1] & l[i]
					max_ind = i
			elif maxi < p[i - 1] & l[i] & s[i + 1]:
				maxi = p[i - 1] & l[i] & s[i + 1]
				max_ind = i
		print(l[max_ind], end=' ')
		for i in range(n):
			if i != max_ind:
				print(l[i], end=' ')
	t -= 1


n = int(input())
a = list(map(int, input().split()))
exist = 0
final = max(a)
ok = 2 ** 30 - 1
for i in range(n):
	tmp = a[i] ^ exist
	tmp2 = a[i] & exist
	ok = ok & ~tmp2
	final = (final | tmp) & ok
	exist = exist | a[i]
final = format(final, 'b').zfill(30)
idx = 0
for i in range(30):
	if final[i] == '1':
		idx = i
		break
for i in range(n):
	if format(a[i], 'b').zfill(30)[idx] == '1':
		idx = i
		break
print(a[idx], end=' ')
for i in range(n):
	if i != idx:
		print(a[i], end=' ')


from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

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

def intsput():
	return map(int, sinput())

class RangedList:

	def __init__(self, start, stop, val=0):
		self.shift = 0 - start
		self.start = start
		self.stop = stop
		self.list = [val] * (stop - start)

	def __setitem__(self, key, value):
		self.list[key + self.shift] = value

	def __getitem__(self, key):
		return self.list[key + self.shift]

	def __repr__(self):
		return str(self.list)

	def __iter__(self):
		return iter(self.list)
n = intput()
originals = list(intsput())
flips = [~x for x in originals]
forward = [2 ** 100 - 1]
backward = [2 ** 100 - 1]
for i in range(len(flips)):
	forward.append(forward[-1] & flips[i])
for i in range(len(flips) - 1, -1, -1):
	backward.append(backward[-1] & flips[i])
best = -1
first = None
for i in range(len(originals)):
	trial = forward[i] & originals[i] & backward[len(originals) - 1 - i]
	if trial > best:
		best = trial
		first = originals[i]
originals.remove(first)
originals = [first] + originals
print(*originals)


import os.path
import sys
from math import *
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def inp():
	return sys.stdin.readline().strip()
n = int(inp())
arr = list(map(int, inp().split()))
first = 0
for i in range(30, -1, -1):
	cnt = 0
	for j in range(n):
		if arr[j] & 1 << i:
			cnt += 1
			first = j
	if cnt == 1:
		break
print(arr[first], *arr[:first], *arr[first + 1:])


from collections import defaultdict
n = int(input())
xs = list(map(int, input().split()))
count = defaultdict(int)
for x in xs:
	for (i, b) in enumerate(reversed(bin(x)[2:])):
		if b != '0':
			count[i] += 1
rm = 0
for (k, v) in count.items():
	if v > 1:
		rm += 2 ** k
m = (-1, 0)
for (i, x) in enumerate(xs):
	if (x | rm) - rm > m[0]:
		m = ((x | rm) - rm, i)
i = m[1]
xs = [xs[i]] + xs[:i] + xs[i + 1:]
print(' '.join(map(str, xs)))


from sys import stdin, stdout
for _ in range(1):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	ans = []
	bits = [[0 for _ in range(32)] for _ in range(n)]
	for i in range(n):
		num = a[i]
		for j in range(32):
			bits[i][j] = num >> j & 1
	for i in range(31, -1, -1):
		cnt = 0
		pos = -1
		for j in range(n):
			if bits[j][i] == 1:
				cnt += 1
				pos = j
		if cnt == 1:
			break
	if pos != -1:
		ans += [a[pos]]
	for i in range(n):
		if i == pos:
			continue
		ans += [a[i]]
	print(*ans)


n = int(input())
s = list(map(int, input().split()))
for i in range(30, -1, -1):
	if sum((1 for x in s if x & 1 << i)) == 1:
		s.sort(key=lambda x: -(x & 1 << i))
		break
print(*s)


def f(x, y):
	return (x | y) - y
n = int(input())
l = list(map(int, input().split()))
mp = [0 for _ in range(31)]
for num in l:
	b = bin(num)[2:].zfill(31)
	for i in range(31):
		if b[i] == '1':
			mp[i] += 1
largest = 0
largestNum = l[0]
for num in l:
	b = bin(num)[2:].zfill(31)
	tmp = int(''.join(['1' if b[i] == '1' and mp[i] == 1 else '0' for i in range(31)]), 2)
	if tmp > largest:
		largest = tmp
		largestNum = num
print(largestNum, end=' ')
l.remove(largestNum)
for num in l:
	print(num, end=' ')


n = int(input())
a = list(map(int, input().split()))
a.sort()
a = a[::-1]
k = 0
m = 0
l1 = [0] * n
l1[0] = a[0]
l2 = [0] * n
l2[n - 1] = a[n - 1]
for i in range(1, n):
	l1[i] = l1[i - 1] | a[i]
	l2[n - 1 - i] = l2[n - i] | a[n - i - 1]
for i in range(n):
	s = 0
	if i > 0:
		s = s | l1[i - 1]
	if i < n - 1:
		s = s | l2[i + 1]
	s = (a[i] | s) - s
	if m < s:
		m = s
		k = i
(a[0], a[k]) = (a[k], a[0])
print(*a)


n = int(input())
a = list(map(int, input().split()))
w = []
for i in range(33):
	w.append([])
for i in range(n):
	h = a[i]
	for j in range(30, -1, -1):
		if 2 ** j <= h:
			h -= 2 ** j
			w[j].append(a[i])
		else:
			continue
t = 0
for i in range(33):
	if len(w[32 - i]) == 1:
		print(w[32 - i][0], end=' ')
		a.remove(w[32 - i][0])
		break
	else:
		t += 1
if t == 33:
	print(*a)
else:
	print(*a)


R = lambda : list(map(int, input().split()))
n = int(input())
a = R()
first = 0
for i in range(30, -1, -1):
	cnt = 0
	for j in range(n):
		if a[j] & 1 << i:
			cnt += 1
			first = j
	if cnt == 1:
		break
print(a[first], *a[:first], *a[first + 1:])


import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : list(map(int, input().split()))
go = lambda : 1 / 0

def write(*args, sep='\n'):
	for i in args:
		sys.stdout.write('{}{}'.format(i, sep))
INF = float('inf')
MOD = int(1000000000.0 + 7)
YES = 'YES'
NO = 'NO'

def f(x):
	return bin(x)[2:][::-1]
n = int(input())
cnt = [0] * 100
arr = read()
for i in arr:
	s = f(i)
	for j in range(len(s)):
		if s[j] == '1':
			cnt[j] += 1
ans = [-INF, -INF]
for i in arr:
	s = f(i)
	for j in range(len(s)):
		if s[j] == '1':
			cnt[j] -= 1
	x = []
	for j in range(len(s)):
		if cnt[j] == 0:
			x.append(s[j])
		else:
			x.append('0')
	ans = max(ans, [int(''.join(x)[::-1], 2), i])
	for j in range(len(s)):
		if s[j] == '1':
			cnt[j] += 1
arr.remove(ans[1])
print(ans[1], *arr)


n = int(input())
a = [int(c) for c in input().split()]
only_1 = (1 << 40) - 1
left = [only_1]
right = [only_1]
L = only_1
for i in range(n - 1):
	L = L & ~a[i]
	left.append(L)
R = only_1
for i in range(n - 1):
	R = R & ~a[n - 1 - i]
	right.append(R)
res = 0
res_i = 0
for i in range(n):
	r = a[i] & left[i] & right[n - 1 - i]
	if r > res:
		res_i = i
		res = r
b = [a[res_i]] + [a[j] for j in range(n) if j != res_i]
print(*b)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

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

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 graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

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

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def bs(a, l, h, x):
	while l < h:
		mid = (l + h) // 2
		if a[mid] == x:
			return mid
		if a[mid] < x:
			l = mid + 1
		else:
			h = mid
	return l

def sieve(a):
	for i in range(3, 10 ** 6 + 1, 2):
		a[i] = 1
	for i in range(3, 10 ** 6 + 1, 2):
		for j in range(i * i, 10 ** 6 + 1, i):
			a[j] = 0
	a[2] = 1
	return a

def solve():
	n = int(inp())
	a = lis()
	s = '1' * 32
	if n == 1:
		print(a[0])
		return
	l = []
	for i in range(0, n):
		if i == 0:
			l.append(~a[i])
			continue
		l.append(l[i - 1] & ~a[i])
	r = [0] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			r[i] = ~a[i]
			continue
		r[i] = r[i + 1] & ~a[i]
	ans = -1
	ind = -1
	for i in range(0, n):
		if i == 0:
			if ans < a[i] & r[i + 1]:
				ind = i
			ans = max(ans, a[i] & r[i + 1])
			continue
		if i == n - 1:
			if ans < a[i] & l[i - 1]:
				ind = i
			ans = max(ans, a[i] & l[i - 1])
			continue
		if ans < l[i - 1] & a[i] & r[i + 1]:
			ind = i
		ans = max(ans, l[i - 1] & a[i] & r[i + 1])
	print(*[a[ind]] + a[:ind] + a[ind + 1:])
testcase(1)


n = int(input())
a = list(map(int, input().split()))
bitFreq = [0] * 32
for i in a:
	for j in range(0, 32):
		if i & 1 << j > 0:
			bitFreq[j] += 1
currBest = 0
currIn = 0
for i in range(len(a)):
	currScore = 0
	for j in range(0, 32):
		if a[i] & 1 << j > 0 and bitFreq[j] == 1:
			currScore = currScore | 1 << j
	if currScore > currBest:
		currBest = currScore
		currIn = i
tup = (a[0], a[currIn])
(a[currIn], a[0]) = tup
print(*a)


n = int(input())
l = list(map(int, input().split()))
idx = 0
for i in range(30, -1, -1):
	cnt = 0
	for j in range(n):
		if l[j] & 1 << i:
			cnt += 1
			idx = j
	if cnt == 1:
		break
print(l[idx], *l[idx + 1:], *l[:idx])


from math import log2
n = int(input())
l = list(map(int, input().split(' ')))
l.sort(reverse=True)
d = [0] * 30
for i in l:
	b = bin(i)[2:]
	b = b[::-1]
	for j in range(len(b)):
		if b[j] == '1':
			d[30 - j - 1] += 1
if d.count(1) == 0:
	print(*sorted(l, reverse=True))
else:
	ind = 0
	for i in range(30):
		if d[i] == 1:
			ind = 29 - i
			break
	a = -1
	ans = []
	for i in range(n):
		if 2 ** ind & l[i] and a == -1:
			a = l[i]
		else:
			ans.append(l[i])
	print(a, *ans)


t = int(input())
a = list(map(int, input().split()))
if t == 1:
	print(a[0])
else:
	b = [~i for i in a]
	pos = [0] * t
	pre = [0] * t
	pre[0] = b[0]
	pos[t - 1] = b[-1]
	for i in range(1, t):
		pre[i] = pre[i - 1] & b[i]
		pos[t - i - 1] = pos[t - i] & b[t - i - 1]
	mmax = 0
	ans = 0
	for i in range(t):
		if i == 0:
			temp = pos[i + 1]
			if mmax < temp & a[i]:
				mmax = temp & a[i]
				ans = i
		elif i == t - 1:
			temp = pre[i - 1]
			if mmax < temp & a[i]:
				mmax = temp & a[i]
				ans = i
		else:
			temp = pre[i - 1] & pos[i + 1]
			if mmax < temp & a[i]:
				mmax = temp & a[i]
				ans = i
	(a[0], a[ans]) = (a[ans], a[0])
	print(*a, sep=' ')


import sys
import math
import heapq
import bisect
import re
from collections import deque
from decimal import *
from fractions import gcd

def YES_NO(flag):
	if flag:
		print('AWW')
	else:
		print('WAW')

def main():
	n = int(sys.stdin.readline())
	q = [int(i) for i in sys.stdin.readline().split()]
	r = int(math.log2(max(max(q), 1))) + 5
	for i in range(r, -1, -1):
		(w, e) = (0, 0)
		for o in range(n):
			if q[o] & 1 << i:
				w += 1
				e = o
		if w == 1:
			print(q[e], end=' ')
			for o in range(n):
				if e != o:
					print(q[o], end=' ')
			return 0
	print(*q)
for i in range(1):
	main()


def Input():
	tem = input().split()
	ans = []
	for it in tem:
		ans.append(int(it))
	return ans
n = Input()[0]
a = Input()
l_or = [0 for i in range(n)]
tem = 0
for i in range(n):
	l_or[i] = tem | a[i]
	tem = l_or[i]
inx = 0
MAX = 0
tem = 0
for i in range(n - 1, -1, -1):
	if i - 1 >= 0:
		mean = a[i] - (a[i] & (l_or[i - 1] | tem))
	else:
		mean = a[i] - (a[i] & tem)
	if mean > MAX:
		MAX = mean
		inx = i
	tem = tem | a[i]
print(a[inx], end='')
for i in range(n):
	if i == inx:
		continue
	print('', a[i], end='')
print()


from operator import itemgetter
from collections import Counter

def solve(a):
	m = max(a)
	l = m.bit_length()
	b = [-1] * l
	for (ii, aa) in enumerate(a):
		for ll in range(l):
			if aa & 1 << ll != 0:
				if b[ll] != -1:
					b[ll] = 10 ** 6
				else:
					b[ll] = ii
	xx = -1
	flag = True
	b.reverse()
	for bb in b:
		if bb != -1 and bb != 10 ** 6 and flag:
			xx = bb
			flag = False
	if xx == -1:
		print(' '.join(map(str, a)))
	else:
		x = a.pop(xx)
		a.insert(0, x)
		print(' '.join(map(str, a)))

def main():
	n = int(input())
	a = list(map(int, list(input().split())))
	solve(a)
main()


import sys
import math
from collections import defaultdict, Counter
from itertools import permutations
n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(a[0])
else:
	pref = [0] * n
	pref[0] = ~a[0]
	suff = [0] * n
	suff[n - 1] = ~a[n - 1]
	for i in range(1, n):
		pref[i] = pref[i - 1] & ~a[i]
		suff[n - i - 1] = suff[n - i] & ~a[n - i - 1]
	ma = 0
	ind = 0
	for i in range(n):
		if i == 0:
			d = a[i] & suff[i + 1]
		elif i == n - 1:
			d = a[i] & pref[i - 1]
		else:
			d = a[i] & suff[i + 1] & pref[i - 1]
		if d >= ma:
			ma = d
			ind = i
	print(*a[ind:], *a[:ind])


def h(n):
	if n == 1:
		return '1'
	if n == 0:
		return '0'
	return h(n % 2) + h(n // 2)
n = int(input())
a = list(map(int, input().split()))
b = list(map(h, a))
res = 0
for i in range(31, 0, -1):
	ind = -1
	col = 0
	for j in range(n):
		if len(b[j]) >= i and b[j][i - 1] == '1':
			col += 1
			ind = j
	if col == 1:
		res = ind
		break
print(a.pop(res), ' '.join(map(str, a)))


from sys import stdin

def inp():
	return (ss.rstrip() for ss in stdin).__next__()

def itg():
	return int(inp())

def mpint():
	return map(int, inp().split())
from itertools import accumulate
from functools import reduce

def and_(a, b):
	return a & b
n = itg()
if n == 1:
	print(itg())
	exit()
arr = [~i for i in mpint()]
prefix = tuple(accumulate(arr, and_))
suffix = tuple(accumulate(reversed(arr), and_))
Max = ~arr[0] & suffix[-2]
idx = 0
if ~arr[-1] & prefix[-2] > Max:
	Max = ~arr[-1] & prefix[-2]
	idx = n - 1
for i in range(1, n - 1):
	v = ~arr[i] & prefix[i - 1] & suffix[n - i - 2]
	if v > Max:
		Max = v
		idx = i
arr = [~i for i in arr]
print(arr.pop(idx), *arr)


n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(32, -1, -1):
	flag = -1
	badflag = 0
	for j in range(n):
		if a[j] & 1 << i:
			if flag == -1:
				flag = j
			else:
				badflag = 1
		if badflag == 1:
			break
	if flag != -1 and badflag == 0:
		ans.append(str(a[flag]))
		for j in range(n):
			if j != flag:
				ans.append(str(a[j]))
		break
if len(ans) == 0:
	for i in range(n):
		ans.append(str(a[i]))
print(' '.join(ans))


def main():
	n = int(input().strip())
	A = [int(s) for s in input().strip().split()]
	for i in range(31, -1, -1):
		if sum((a >> i & 1 for a in A)) == 1:
			break
	for (j, a) in enumerate(A):
		if a >> i & 1:
			break
	result = [A[j]] + A[:j] + A[j + 1:]
	print(' '.join((str(x) for x in result)))
main()


N = int(input())
A = list(map(int, input().split()))
k = 1 << 31
X = -1
while k:
	T = []
	for i in range(N):
		if A[i] & k:
			T.append(i)
	if len(T) == 1:
		X = T[0]
		break
	else:
		k >>= 1
if X < 0:
	print(*A)
else:
	ret = A[X]
	A.pop(X)
	print(ret, *A)


n = int(input())
s = [int(x) for x in input().split()]
pos = [0] * 32
for i in range(0, len(s)):
	t = bin(s[i])
	t = t[2:]
	t = '0' * (32 - len(t)) + t
	for j in range(0, len(t)):
		pos[j] += int(t[j])
ptr = -1
for i in range(0, len(pos)):
	if pos[i] == 1:
		ptr = i
		break
if ptr == -1:
	print(*s)
else:
	for i in range(0, len(s)):
		t = bin(s[i])
		t = t[2:]
		t = '0' * (32 - len(t)) + t
		if t[ptr] == '1':
			temp = s[0]
			s[0] = s[i]
			s[i] = temp
	print(*s)


n = int(input())
l = list(map(int, input().split()))
freq = [0] * 32
for i in l:
	for j in range(32):
		if i >> j & 1:
			freq[j] -= -1
ans = -1
ind = -1
for i in range(n):
	temp = 0
	for j in range(32):
		if freq[j] == 1 and l[i] >> j & 1:
			temp += 2 ** j
	if temp > ans:
		ans = temp
		ind = i
print(l[ind], end=' ')
for i in range(n):
	if i != ind:
		print(l[i], end=' ')


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

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

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

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

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)
n = int(input())
l = list(map(int, input().split()))
pre = [~l[0]]
for i in range(1, n):
	pre.append(pre[i - 1] & ~l[i])
suf = [~l[-1]]
for i in range(1, n):
	suf.append(suf[i - 1] & ~l[n - 1 - i])
if suf[n - 2] & l[0] > pre[n - 2] & l[-1]:
	a = suf[n - 2] & l[0]
	ind = 0
else:
	a = pre[n - 2] & l[-1]
	ind = n - 1
for i in range(1, n - 1):
	if pre[i - 1] & suf[n - 2 - i] & l[i] > a:
		a = pre[i - 1] & suf[n - 2 - i] & l[i]
		ind = i
print(l[ind], end=' ')
l.pop(ind)
print(*l)


from bisect import *
from math import *
n = int(input())
arr = list(map(int, input().split()))
arr_ind = [[] for i in range(32)]
for i in range(n):
	temp = arr[i]
	ind = 0
	while temp > 0:
		if temp % 2 == 1:
			arr_ind[ind].append(i)
		temp = temp // 2
		ind += 1
flag = False
for i in range(31, -1, -1):
	if len(arr_ind[i]) == 1:
		flag = True
		break
if flag == True:
	index = arr_ind[i][0]
	out_arr = [str(arr[index])]
	for j in range(0, index):
		out_arr.append(str(arr[j]))
	for j in range(index + 1, n):
		out_arr.append(str(arr[j]))
	out = ' '.join(out_arr)
else:
	arr1 = list(map(str, arr))
	out = ' '.join(arr1)
print(out)


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(a[0])
else:
	com_a = [~i for i in a]
	prefix = [0] * n
	postfix = [0] * n
	prefix[0] = com_a[0]
	postfix[-1] = com_a[-1]
	for i in range(1, n):
		prefix[i] = prefix[i - 1] & com_a[i]
		postfix[n - i - 1] = com_a[n - i - 1] & postfix[n - i]
	MAX = -10 ** 10
	ans = 0
	for i in range(n):
		if i == 0:
			tmp = a[i] & postfix[i + 1]
		elif i == n - 1:
			tmp = a[i] & prefix[-2]
		else:
			tmp = a[i] & prefix[i - 1] & postfix[i + 1]
		if tmp >= MAX:
			ans = i
			MAX = tmp
	print(a[ans], end=' ')
	try:
		print(*a[:ans], end=' ')
	except IndexError:
		pass
	try:
		print(*a[ans + 1:], end=' ')
	except IndexError:
		pass


y = input
z = print
r = range
n = int(y())
l = list(map(int, y().split()))
d = {}
for i in l:
	b = bin(i)[:1:-1]
	for j in r(len(b)):
		if int(b[j]):
			d[j] = d.get(j, [])
			d[j].append(i)
for i in r(31, -1, -1):
	if len(d.get(i, [])) == 1:
		v = d[i][0]
		print(v, end=' ')
		k = 0
		for j in l:
			if j == v and k < 1:
				k += 1
			else:
				z(j, end=' ')
		quit()
z(' '.join(map(str, l)))


n = int(input())
a = [int(v) for v in input().split()]
x = [[] for _ in range(64)]
for (i, v) in enumerate(a):
	mask = 1
	for k in range(64):
		if v & mask:
			x[k].append(i)
		mask <<= 1
z = 0
for k in range(63, -1, -1):
	if len(x[k]) == 1:
		z = x[k][0]
		break
print(a[z], *a[:z] + a[z + 1:])


import sys
from pprint import pprint
if not 'DEBUG' in globals():
	readline = sys.stdin.readline

def read(map_func):
	return map_func(readline().rstrip())

def reads(map_func):
	return list(map(map_func, readline().rstrip().split()))

def readint():
	return read(int)

def readints():
	return reads(int)

def readcol(n):
	return [readint() for _ in range(n)]

def readmat(n):
	return [readints() for _ in range(n)]

def makemat(n, m, v):
	return [[v for _ in range(n)] for _ in range(m)]

def zeromat(n, m=None):
	return makemat(n, m if m else n, 0)

def listmat(n, m=None):
	return [[[] for _ in range(n)] for _ in range(m if m else n)]

def crosslist(y, x, n=None, m=None):
	return [(p, q) for (p, q) in [(y, x + 1), (y - 1, x), (y, x - 1), (y + 1, x)] if (n == None or 0 <= p < n) and (m == None or 0 <= q < m)]

def roundlist(y, x, n=None, m=None):
	return [(p, q) for (p, q) in [(y, x + 1), (y - 1, x + 1), (y - 1, x), (y - 1, x - 1), (y, x - 1), (y + 1, x - 1), (y + 1, x), (y + 1, x + 1)] if (n == None or 0 <= p < n) and (m == None or 0 <= q < m)]

def log(obj):
	pprint(obj) if 'DEBUG' in globals() else None
N = readint()
A = readints()
L = max(A).bit_length()
log(L)
k = None
for j in range(L):
	t = None
	for (i, a) in enumerate(A):
		b = a >> L - 1 - j & 1
		log(b)
		if b == 1:
			if t is not None:
				t = None
				break
			else:
				t = i
	else:
		if t is not None:
			k = t
			break
if k:
	x = A.pop(k)
	A.insert(0, x)
print(' '.join(map(str, A)))


def func(start, b):
	temp = start
	for j in range(len(b)):
		temp = (temp | b[j]) - b[j]
	return temp
n = int(input())
a = list(map(int, input().split()))
count = []
for i in range(32):
	count.append([])
for i in a:
	b = bin(i)[2:].rjust(32, '0')
	for j in range(32):
		if b[j] == '1':
			count[j].append(i)
ans = a[:]
maxi = -1
for i in range(32):
	if len(count[i]) == 1:
		b = a[:]
		elem = count[i][0]
		b.remove(elem)
		f = func(elem, b)
		if f > maxi:
			maxi = f
			ans = [elem] + b
print(*ans)


n = int(input())
l = list(map(int, input().split()))
m = [0] * n
d = 0
for i in range(n):
	bin_str = format(l[i], 'b')
	d = max(d, len(bin_str))
	m[i] = bin_str
for i in range(n):
	m[i] = '0' * (d - len(m[i])) + m[i]
i_ = -1
for j in range(d):
	cnt = 0
	for i in range(n):
		if m[i][j] != '0':
			i_ = i
			cnt += 1
	if cnt == 1:
		break
if i_ != -1:
	temp = l.pop(i_)
	l_ = [temp] + l
	ans = [str(c) for c in l_]
	print(' '.join(ans))
else:
	l_ = sorted(l, reverse=True)
	ans = [str(c) for c in l_]
	print(' '.join(ans))


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time

def I():
	return int(input())

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

def S():
	return input()

def MS():
	return map(str, input().split())

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

def LI_():
	return [int(i) - 1 for i in input().split()]

def StoI():
	return [ord(i) - 97 for i in input()]

def ItoS(nn):
	return chr(nn + 97)

def input():
	return sys.stdin.readline().rstrip()

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = {False: 'No', True: 'Yes'}
MOD = 10 ** 9 + 7
inf = float('inf')
IINF = 10 ** 10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False

def f(x, y):
	return x & ~y

def main():
	n = I()
	a = LI()
	a = sorted(a, reverse=True)
	x = 0
	for i in range(30, -1, -1):
		c = 0
		for j in range(n):
			if a[j] >> i & 1 == 1:
				c += 1
				x = j
		if c == 1:
			break
	ans = [a.pop(x)] + a
	print(*ans)
main()


sp = lambda : map(int, input().split())
si = lambda : int(input())
n = si()
rawa = list(sp())
a = list(map(lambda x: list(map(int, format(x, '030b'))), rawa))
bitcount = [0] * 30
for x in a:
	for b in range(30):
		bitcount[b] += x[b]
bitcount2 = 0
for bit in bitcount:
	bitcount2 <<= 1
	bitcount2 += bit == 1
maxv = -1
maxi = -1
for (i, x) in enumerate(rawa):
	if x & bitcount2 > maxv:
		maxv = x & bitcount2
		maxi = i
print(' '.join(map(str, [rawa[maxi]] + rawa[:maxi] + rawa[maxi + 1:])))


n = int(input())
x = list(map(int, input().split()))
y = 0
for i in range(32, -1, -1):
	c = 0
	temp = -1
	for j in range(n):
		if x[j] >> i & 1 == 0:
			c = c + 1
		else:
			temp = j
	if c == n - 1:
		y = temp
		break
(x[0], x[y]) = (x[y], x[0])
print(*x)


from sys import stdin
input = stdin.buffer.readline
n = int(input())
arr = [int(x) for x in input().split()]
dp = [0] * 32
l = []
for ele in arr:
	s = bin(ele)[2:]
	s = '0' * (32 - len(s)) + s
	for i in range(32):
		dp[i] = dp[i] + int(s[i])
	l.append(s)
ans = 0
first = -1
for s in l:
	for i in range(32):
		dp[i] = dp[i] - int(s[i])
	s_new = ''
	for i in range(32):
		if s[i] == '1' and dp[i] >= 1:
			s_new = s_new + '0'
		else:
			s_new = s_new + s[i]
	if int(s_new, 2) > ans:
		first = int(s, 2)
		ans = int(s_new, 2)
	for i in range(32):
		dp[i] = dp[i] + int(s[i])
if first == -1:
	print(*arr)
else:
	i = arr.index(first)
	print(first, end=' ')
	for j in range(0, i):
		print(arr[j], end=' ')
	for j in range(i + 1, n):
		print(arr[j], end=' ')


n = int(input())
a = list(map(int, input().split()))
bitsum = [0] * 32
for i in a:
	for j in range(32):
		bitsum[j] += i >> j & 1
onebit = None
for i in range(31, -1, -1):
	if bitsum[i] == 1:
		onebit = i
		break
if onebit is None:
	print(*a)
	exit(0)
for i in range(n):
	if a[i] >> onebit & 1:
		first = i
		break
a = [a[first]] + a[:first] + a[first + 1:]
print(*a)


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(30, -1, -1):
	dem = 0
	for x in arr:
		if x & 1 << i:
			dem += 1
	if dem == 1:
		cur = 0
		for j in range(n):
			if arr[j] & 1 << i:
				cur = j
		print(arr[cur], end=' ')
		for j in range(n):
			if j != cur:
				print(arr[j], end=' ')
		exit()
for x in arr:
	print(x, end=' ')


class SegTree:

	def __init__(self, arr):
		self.arr = arr
		self.tree = [None for _ in range(2 * len(arr) * 2)]
		self.build()

	def build(self, node=1, start=0, end=None):
		if end == None:
			end = len(self.arr) - 1
		if start == end:
			self.tree[node] = self.arr[start]
		else:
			mid = (start + end) // 2
			self.build(node * 2, start, mid)
			self.build(node * 2 + 1, mid + 1, end)
			self.tree[node] = self.tree[node * 2] | self.tree[node * 2 + 1]

	def update(self, idx, val, node=1, start=0, end=None):
		if end == None:
			end = len(self.arr) - 1
		if start == end:
			self.arr[idx] = val
			self.tree[node] = val
		else:
			mid = (start + end) // 2
			if start <= idx <= mid:
				self.update(idx, val, 2 * node, start, mid)
			else:
				self.update(idx, val, 2 * node + 1, mid + 1, end)
			self.tree[node] = self.tree[2 * node] | self.tree[2 * node + 1]
input()
arr = list(map(int, input().strip().split(' ')))
tree = SegTree(arr.copy())
cumilative_or = [None for _ in range(len(arr))]
max_f = -1
first_idx = -1
for (idx, a) in enumerate(arr):
	tree.update(idx, 0)
	f = (a | tree.tree[1]) - tree.tree[1]
	if f > max_f:
		max_f = f
		first_idx = idx
	tree.update(idx, a)
print(arr[first_idx], end=' ')
for (idx, a) in enumerate(arr):
	if idx == first_idx:
		continue
	print(a, end=' ')
print()


n = int(input())
pw = [2 ** (30 - i) for i in range(31)]

def kek(x):
	global pw
	ans = set()
	mind = 0
	for i in range(31):
		pwi = pw[i]
		if pwi <= x:
			x -= pwi
			ans.add(pwi)
	return ans
ra = list(map(int, input().split()))
a = list(map(kek, ra))
t = dict([[pw[i], 0] for i in range(31)])
for ii in a:
	for i in ii:
		t[i] += 1
q = set()
meme = 1
for i in range(30, -1, -1):
	if t[pw[i]] > 1:
		q.add(pw[i])
(m, mv) = (-1, -1)
for i in a:
	idf = sum(i.difference(q))
	if idf > mv:
		(m, mv) = (i, idf)
m = sum(m)
print(m, end=' ')
nmust = False
ra.pop(ra.index(m))
print(' '.join(map(str, ra)))


import sys
input = sys.stdin.buffer.readline
n = int(input())
a = [int(i) for i in input().split()]
for i in range(34, -1, -1):
	b = 1 << i
	cnt = 0
	idx = -1
	for x in range(n):
		if b & a[x] != 0:
			cnt += 1
			idx = x
	if cnt == 1:
		print(a[idx], end=' ')
		for x in range(n):
			if x != idx:
				print(a[x], end=' ')
		exit(0)
else:
	print(*a)


import os, sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	x = Counter()
	(ind, ans) = (-1, -1)
	for i in arr:
		for (j, val) in enumerate(bin(i)[2:][::-1]):
			x[j] += int(val)
	for (indd, i) in enumerate(arr):
		(va, y) = (0, 1)
		for (j, val) in enumerate(bin(i)[2:][::-1]):
			if int(val) and x[j] == 1:
				va += y
			y <<= 1
		if va > ans:
			ind = indd
			ans = va
	(arr[0], arr[ind]) = (arr[ind], arr[0])
	print(*arr)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import log, ceil

def bitnot(n):
	if n == 0:
		return (1 << 31) - 1
	return (1 << 31) - 1 - n
n = int(input())
l = [int(i) for i in input().split()]
f = bitnot(l[0])
prefix = []
for i in l:
	f &= bitnot(i)
	prefix.append(f)
f = bitnot(l[-1])
sufix = []
for i in l[::-1]:
	f &= bitnot(i)
	sufix.append(f)
m = [-1, -1]
for i in range(n):
	first = l[i]
	p = s = 2 ** 31 - 1
	if i > 0:
		p = prefix[i - 1]
	if i < n - 1:
		s = sufix[n - i - 2]
	r = p & s
	ans = first & r
	if ans > m[0]:
		m[0] = ans
		m[1] = i
(l[m[1]], l[0]) = (l[0], l[m[1]])
print(*l)


n = int(input().strip())
A = list(map(int, input().strip().split()))
A_set = list(set(A))
if len(A) == 1 or len(A_set) == 1:
	print(' '.join(list(map(str, A))))
else:
	m = len(A_set)
	prefix = [~A_set[0]] * m
	postfix = [~A_set[-1]] * m
	for i in range(1, m):
		prefix[i] = prefix[i - 1] & ~A_set[i]
	for i in range(m - 2, -1, -1):
		postfix[i] = postfix[i + 1] & ~A_set[i]
	max_ans = -1
	index = -1
	for i in range(1, m - 1):
		ans = A_set[i] & prefix[i - 1] & postfix[i + 1]
		if ans > max_ans:
			max_ans = ans
			index = i
	ans = A_set[0] & postfix[1]
	if ans > max_ans:
		max_ans = ans
		index = 0
	ans = A_set[m - 1] & prefix[m - 2]
	if ans > max_ans:
		max_ans = ans
		index = m - 1
	ans = A_set[index]
	A.remove(ans)
	A.insert(0, ans)
	print(' '.join(list(map(str, A))))


def binarySearch(arr, x):
	l = 0
	r = len(arr) - 1
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] <= x:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def jg(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= x:
			ans = m
			r = m - 1
		else:
			l = m + 1
	return ans
from math import *

def ceil(a, b):
	if a % b == 0:
		return int(a / b)
	else:
		return a // b + 1
n = int(input())
ar = list(map(int, input().split()))
p = [~ar[0]]
for i in range(1, n):
	p.append(p[-1] & ~ar[i])
s = [~ar[-1]]
for i in range(2, n + 1):
	s.append(s[-1] & ~ar[-i])
s = s[::-1]
ind = 0
d = 0
if n > 1:
	for i in range(n):
		if i == 0:
			if d < ar[i] & s[i + 1]:
				d = ar[i] & s[i + 1]
				ind = i
		elif i == n - 1:
			if d < ar[i] & p[i - 1]:
				d = ar[i] & s[i - 1]
				ind = i
		elif d < p[i - 1] & s[i + 1] & ar[i]:
			d = p[i - 1] & s[i + 1] & ar[i]
			ind = i
print(ar[ind], end=' ')
for i in range(n):
	if i != ind:
		print(ar[i], end=' ')


import math
for _ in range(1):
	n = int(input())
	li = list(map(int, input().split()))
	d = [0 for i in range(40)]
	e = []
	for i in li:
		k = bin(i)[2:][::-1]
		e.append(k)
		for j in range(len(k)):
			if k[j] == '1':
				d[j] += 1
	ans = 0
	l = -1
	ii = -1
	for i in e:
		ii += 1
		p = 0
		for j in range(len(i)):
			if i[j] == '1':
				if d[j] == 1:
					p += 2 ** j
		if p > ans:
			ans = p
			l = ii
	if ans > 0:
		k = li[l]
		li.remove(k)
		print(k, *li)
	else:
		print(*li)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(31, -1, -1):
	cnt = 0
	cur = -1
	for j in range(n):
		if a[j] >> i & 1 == 0:
			cnt += 1
		else:
			cur = j
	if cnt == n - 1:
		ans = cur
		break
print(a.pop(ans), *a)


def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	for j in range(32, -1, -1):
		flg = 0
		for (i, a) in enumerate(A):
			if a >> j & 1:
				flg += 1
				idx = i
				if flg == 2:
					break
		if flg == 1:
			(A[0], A[idx]) = (A[idx], A[0])
			print(*A)
			return
	print(*A)

def __starting_point():
	main()
__starting_point()


n = int(input())
l = list(map(int, input().split()))
cnt = [0] * 30
for i in range(n):
	for j in range(30):
		if l[i] & 1 << j:
			cnt[j] += 1
options = [i for i in range(n)]
for i in range(29, -1, -1):
	newoptions = []
	if cnt[i] == 1:
		for idx in options:
			if l[idx] & 1 << i:
				newoptions.append(idx)
	if len(newoptions) > 0:
		options = newoptions
print(l[options[0]], end=' ')
for i in range(n):
	if i != options[0]:
		print(l[i], end=' ')


import math
n = int(input())
a = list(map(int, input().split()))

def f(x, y):
	return (x | y) - y
if n == 1:
	print(a[0])
else:
	gauche = [~a[0]] * n
	droite = [~a[-1]] * n
	for i in range(1, n):
		gauche[i] = gauche[i - 1] & ~a[i]
	for i in range(n - 2, -1, -1):
		droite[i] = droite[i + 1] & ~a[i]
	maxi = a[i] & droite[i + 1]
	index = 0
	for i in range(1, n):
		res = a[i] & gauche[i - 1] if i == n - 1 else a[i] & gauche[i - 1] & droite[i + 1]
		if res > maxi:
			maxi = res
			index = i
	print(' '.join(map(str, [a[index]] + a[0:index] + a[index + 1:n])))


import sys
n = int(sys.stdin.readline().rstrip())
num = list(map(int, sys.stdin.readline().rstrip().split()))
max_len = 0
for i in num:
	bi = bin(i)[2:]
	if max_len < len(bi):
		max_len = len(bi)
st = 0
max_len -= 1
while True:
	cnt = 0
	for (idx, i) in enumerate(num):
		if 2 ** max_len & i > 0:
			cnt += 1
			if cnt > 1:
				break
			if cnt == 1:
				st = idx
	if cnt == 1:
		break
	max_len -= 1
	if max_len < 0:
		break
print(num[st], end=' ')
del num[st]
for i in num:
	print(i, end=' ')


from collections import defaultdict
n = int(input())
arr = [int(i) for i in input().split(' ')]
d = defaultdict(list)
mask = [1 << i for i in range(31)]
for a in arr:
	for i in range(31):
		if mask[i] & a != 0:
			d[i].append(a)
result = -1
for i in range(30, -1, -1):
	l = d[i]
	if len(l) == 1:
		result = l[0]
		break
if result == -1:
	for i in arr:
		print(i, end=' ')
else:
	print(result, end=' ')
	new_arr = [i for i in arr if i != result]
	new_arr.sort()
	for i in new_arr:
		print(i, end=' ')


n = int(input())
arr = list(map(int, input().split()))
fl = False
max_ans = 0
for i in range(n):
	ans = arr[i]
	for j in range(n):
		if i != j:
			ans = (ans | arr[j]) - arr[j]
			if ans <= max_ans:
				break
	if ans > max_ans:
		max_ans = ans
		idx = i
		fl = True
if fl:
	(arr[0], arr[idx]) = (arr[idx], arr[0])
print(*arr)