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.

Difficulty: MEDIUM
Tags: Complete search

Mathematics

Greedy algorithms
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
from sys import stdin, stdout
get_string = lambda : stdin.readline().strip('\n')
get_intmap = lambda : map(int, get_string().split(' '))

def testcase():
	n = int(input())
	a = list(get_intmap())
	not_mask = (1 << 32) - 1
	a_not = tuple([not_mask ^ i for i in a])
	(dp_left, dp_right) = ([0] * n, [0] * n)
	(dp_left[0], dp_right[-1]) = (a_not[0], a_not[-1])
	for ind in range(1, n):
		dp_left[ind] = dp_left[ind - 1] & a_not[ind]
	for ind in range(n - 2, -1, -1):
		dp_right[ind] = dp_right[ind + 1] & a_not[ind]
	max_ind = -1
	max_val = -1
	for ind in range(n):
		left = not_mask if ind == 0 else dp_left[ind - 1]
		right = not_mask if ind == n - 1 else dp_right[ind + 1]
		tmp = left & a[ind] & right
		if tmp > max_val:
			max_val = tmp
			max_ind = ind
	a.insert(0, a.pop(max_ind))
	print(' '.join([str(i) for i in a]))
testcase()
quit()
for t in range(int(input())):
	testcase()


from math import factorial
from collections import Counter
from heapq import heapify, heappop, heappush
from sys import stdin

def RL():
	return map(int, stdin.readline().rstrip().split())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
n = int(input())
arr = list(RL())
tag = 31
res = []
while True:
	num = 0
	ts = -1
	for i in arr:
		if 1 << tag & i:
			num += 1
			ts = i
			if num > 1:
				break
	else:
		if num == 1:
			res.append(ts)
			arr.remove(ts)
			break
	tag -= 1
	if tag < 0:
		break
print(' '.join([str(i) for i in res + arr]))


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=' ')


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]
			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)


def digit(n, d):
	return n % 2 ** (d + 1) // 2 ** d

def get_ans(nums):
	for d in range(30, -1, -1):
		s = 0
		b = None
		for (i, n) in enumerate(nums):
			if digit(n, d):
				s += 1
				b = i
		if s == 1:
			return [nums[b]] + nums[:b] + nums[b + 1:]
	return nums
input()
nums = list(map(int, input().strip().split()))
nums = get_ans(nums)
print(' '.join(map(str, nums)))


n = int(input())
a = [int(x) for x in input().split()]
val = [0] * n
pos = [list() for i in range(31)]
cnt = [0] * 31
ans = int(0)
for i in range(n):
	for j in range(31):
		if a[i] >> j & 1:
			cnt[j] += 1
			pos[j].append(i)
for i in range(31):
	if cnt[i] != 1:
		continue
	for p in pos[i]:
		val[p] += 1 << i
for i in range(n):
	if val[i] > val[ans]:
		ans = i
print(a[ans], end=' ')
for i in range(n):
	if i != ans:
		print(a[i], end=' ')


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
count = [0] * 32
for a in A:
	for i in range(32):
		if a & 1 << i != 0:
			count[i] += 1
init = 0
flag = 0
for i in range(31, -1, -1):
	if count[i] == 1:
		for j in range(n):
			if A[j] & 1 << i != 0:
				init = j
				flag = 1
				break
	if flag == 1:
		break
ANS = [A[init]]
for i in range(n):
	if i == init:
		continue
	ANS.append(A[i])
print(*ANS)


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()


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)


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)


s = int(input())
arr = list(map(int, input().split()))
zero_bits = dict()
index_max = 0
for i in range(0, 32):
	zero_bits[i] = []
power_ = [2 ** i for i in range(0, 32)]
_enumerate = enumerate
_reversed = reversed
for (index, i) in _enumerate(arr):
	temp = i
	for (index_, j) in _enumerate(_reversed(power_)):
		if temp >= j:
			zero_bits[index_].append(index)
			temp -= j
for i in _reversed(range(32)):
	if len(zero_bits[i]) == 1:
		index_max = zero_bits[i][0]
max_ = arr[index_max]
del arr[index_max:index_max + 1]
arr = [max_] + arr
for i in arr:
	print(i, end=' ')


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]
	del a[idx]
	sys.stdout.write(str(tmp) + ' ')
	if len(a) >= 1:
		sys.stdout.write(' '.join(map(str, a)))
else:
	sys.stdout.write(' '.join(map(str, a)))


def f(x, y):
	return (x | y) - y

def solve(a):
	bit = 33
	while bit >= 0:
		count = 0
		for item in a:
			if 1 << bit & item:
				count += 1
		if count == 1:
			break
		bit -= 1
	if bit < 0:
		return a
	result = []
	skip = None
	for (i, item) in enumerate(a):
		if 1 << bit & item:
			result.append(item)
			skip = i
	for (i, item) in enumerate(a):
		if i != skip:
			result.append(item)
	return result
n = int(input())
a = [int(x) for x in input().split()]
print(' '.join(map(str, solve(a))))


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()


def cal(x):
	if x == 0:
		return 1
	else:
		ret = 0
		while x > 0:
			ret += 1
			x //= 2
		return ret

def flip(x, nbits):
	more = nbits - cal(x)
	d = []
	if x == 0:
		d.append(0)
	else:
		while x > 0:
			d.append(x % 2)
			x //= 2
	for i in range(more):
		d.append(0)
	ret = 0
	p = 1
	for i in d:
		ret += (1 - i) * p
		p *= 2
	return ret
n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(*a)
	exit(0)
nbits = cal(max(a, key=cal))
pref = [flip(a[0], nbits)] * n
suf = [flip(a[-1], nbits)] * n
for i in range(1, n):
	pref[i] = pref[i - 1] & flip(a[i], nbits)
for i in range(n - 2, -1, -1):
	suf[i] = suf[i + 1] & flip(a[i], nbits)
v = []
for i in range(n):
	if i == 0:
		v.append(a[i] & suf[i + 1])
	elif i == n - 1:
		v.append(a[i] & pref[i - 1])
	else:
		v.append(a[i] & pref[i - 1] & suf[i + 1])
pos = v.index(max(v))
print(a[pos], end=' ')
for i in range(n):
	if i != pos:
		print(a[i], end=' ')


def solve(a_s):
	bits = 31
	a_bits = [[a >> bit & 1 for bit in range(bits - 1, -1, -1)] for a in a_s]
	ans_i = 0
	for i in range(bits):
		lone_j = -1
		for (j, a_bit) in enumerate(a_bits):
			if a_bit[i] == 1:
				if lone_j != -1:
					lone_j = -1
					break
				lone_j = j
		if lone_j != -1:
			ans_i = lone_j
			break
	ans = [a_s[ans_i]]
	for (i, a) in enumerate(a_s):
		if i == ans_i:
			continue
		ans.append(a)
	return ans
t = int(input())
a_s = [int(ch) for ch in input().split(' ')]
print(' '.join([str(val) for val in solve(a_s)]))


from sys import stdin, stdout
from os import path
if path.exists('inp.txt'):
	stdin = open('inp.txt', 'r')
c = [0 for i in range(100005)]
ans = 0
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
for i in range(n):
	for j in range(30):
		if 1 << j & a[i]:
			c[j] += 1
for i in range(n):
	res = 0
	for j in range(30):
		if 1 << j & a[i] and c[j] == 1:
			res += 1 << j
	if res > ans:
		(a[0], a[i]) = (a[i], a[0])
		ans = res
for i in a:
	print(i, end=' ')


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=' ')


input()
l = [0] * 30
c = [0] * 30
a = [*map(int, input().split())]
i = 0
for x in a:
	for j in range(30):
		if x >> 29 - j & 1:
			c[j] += 1
			l[j] = i
	i += 1
if 1 in c:
	i = l[c.index(1)]
	a = [a.pop(i)] + a
print(*a)


n = int(input())
a = list(map(int, input().split()))
suf = [0] * (n + 1)
for q in range(n - 1, -1, -1):
	suf[q] = suf[q + 1] | a[q]
(naw, ans) = (0, [-1, -1])
for q in range(n):
	w = a[q] & ~(naw | suf[q + 1])
	if w > ans[0]:
		ans = [w, q]
	naw |= a[q]
print(a[ans[1]], *a[:ans[1]], *a[ans[1] + 1:])


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
n = I()
a = L()
l = [~a[0]]
r = [~a[-1]]
for i in range(1, n):
	l += (l[-1] & ~a[i],)
for i in range(n - 2, -1, -1):
	r += (r[-1] & ~a[i],)
r = r[::-1]
ans = 0
ind = a[0]
if n == 1:
	exit(print(ind))
for i in range(n):
	if not i:
		tmp = a[i] & r[i + 1]
	elif not i + 1 - n:
		tmp = a[i] & l[i - 1]
	else:
		tmp = a[i] & (l[i - 1] & r[i + 1])
	if tmp > ans:
		ans = tmp
		ind = a[i]
a.remove(ind)
print(ind, *a)


input()
a = input().split()
print(a.pop(next((x for x in zip(*(f'{int(x):30b}' for x in a)) if x.count('1') == 1), '1').index('1')), *a)


n = int(input())
ans = list(map(int, input().split()))
ll = [0] * n
rr = [0] * n
s = 0
for i in range(n):
	s |= ans[i]
	ll[i] = s
s = 0
for i in range(n - 1, -1, -1):
	s |= ans[i]
	rr[i] = s
mx = 0
mxi = 0
for i in range(n):
	s = 0
	if i - 1 >= 0:
		s |= ll[i - 1]
	if i + 1 < n:
		s |= rr[i + 1]
	cur = (ans[i] | s) - s
	if cur > mx:
		mx = cur
		mxi = i
(ans[0], ans[mxi]) = (ans[mxi], ans[0])
print(*ans)


get_bin = lambda x: format(int(x), 'b').zfill(32)
n = int(input())
seq = list(map(int, input().split()))
s = list(map(get_bin, seq))
arr = [[] for i1 in range(32)]
for i1 in range(32):
	for i2 in range(n):
		if s[i2][i1] == '1':
			arr[i1].append(i2)
result = None
for i1 in arr:
	if len(i1) == 1:
		result = i1[0]
		break
if result is None:
	print(*seq)
else:
	print(seq[result], *seq[0:result], *seq[result + 1:n])


n = int(input())
a = [int(x) for x in input().split()]
MAX = 2 ** 40 - 1
prefAndNot = [-1 for _ in range(n)]
for i in range(n):
	if i == 0:
		prefAndNot[i] = MAX
	else:
		prefAndNot[i] = prefAndNot[i - 1] & ~a[i - 1]
suffAndNot = [-1 for _ in range(n)]
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suffAndNot[i] = MAX
	else:
		suffAndNot[i] = suffAndNot[i + 1] & ~a[i + 1]
maxx = -float('inf')
first = 0
for firstElem in range(n):
	temp = prefAndNot[firstElem] & a[firstElem] & suffAndNot[firstElem]
	if temp > maxx:
		maxx = temp
		first = firstElem
ans = [a[first]]
for i in range(n):
	if i != first:
		ans.append(a[i])
print(' '.join((str(x) for x in ans)))


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=' ')


def main():
	n = int(input())
	ints = list(map(int, input().split(' ')))
	bins = []
	for i in range(31):
		bins.append([])
	for num in ints:
		s = bin(num)[2:]
		prefix = '0' * (31 - len(s))
		s = prefix + s
		i = 0
		while i <= 30:
			if s[i] == '1':
				bins[i].append(num)
			i += 1
	i = 0
	found = False
	while i <= 30:
		if len(bins[i]) == 1:
			to_print = bins[i][0]
			found = True
			break
		i += 1
	if found:
		answer = str(to_print) + ' '
		for num in ints:
			if num != to_print:
				answer += str(num) + ' '
	else:
		answer = ''
		for num in ints:
			answer += str(num) + ' '
	print(answer)
main()


n = int(input())
a = list(map(int, input().split()))
m = max(a)
b = 0
while 1 << b < m:
	b += 1
for i in reversed(range(b)):
	s = 0
	for j in range(n):
		if 1 << i & a[j] != 0:
			if s == 1:
				break
			s += 1
			ans = j
	else:
		if s == 1:
			(a[0], a[ans]) = (a[ans], a[0])
			break
print(*a)


__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)


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:])


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 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 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())
aa = [int(i) for i in input().split()]
pre = [~aa[0]]
suff = [~aa[-1]]
for i in range(1, n):
	pre.append(pre[-1] & ~aa[i])
for i in range(n - 2, -1, -1):
	suff.append(suff[-1] & ~aa[i])
suff = suff[::-1]
(maxi, ind) = (-1, -1)
for i in range(0, n):
	cur = aa[i]
	if i == 0:
		if n > 1:
			check = cur & suff[1]
			if maxi < check:
				maxi = check
				ind = i
		elif maxi < cur:
			maxi = cur
			ind = i
	elif i == n - 1:
		if n > 1:
			check = cur & pre[i - 1]
			if maxi < check:
				maxi = i
				ind = i
		elif maxi < cur:
			maxi = cur
			ind = i
	else:
		check = pre[i - 1] & cur & suff[i + 1]
		if maxi < check:
			maxi = check
			ind = i
ans = [aa[ind]]
ans += aa[ind + 1:]
ans += aa[:ind]
print(*ans)


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())
lis = list(map(int, input().split()))
k = 1 << 31
ans = -1
while k:
	c = 0
	ans = -1
	for i in range(n):
		if lis[i] & k:
			ans = i
			c += 1
	if c == 1:
		break
	else:
		k >>= 1
if ans < 0:
	print(*lis)
else:
	aa = lis[ans]
	lis.pop(ans)
	print(aa, *lis)


n = int(input())
x = list(map(int, input().split()))
(bi, ans, skip) = ([[] for i in range(31)], [], [0] * 31)
ma = 0
for i in range(n):
	b = str(bin(x[i]))
	for i2 in range(len(b) - 1, -1, -1):
		if b[i2] == 'b':
			break
		if b[i2] == '1':
			bi[len(b) - i2 - 1].append(i)
			ma = max(ma, len(b) - i2 - 1)
for i in range(ma, -1, -1):
	if skip[i] == 1:
		continue
	if len(bi[i]) == 1:
		ans.append(bi[i][0])
		for i2 in range(ma + 1):
			if bi[i][0] in bi[i]:
				skip[i2] = 1
for i in range(len(ans)):
	print(x[ans[i]], end=' ')
for i in range(n):
	if i not in ans:
		print(x[i], end=' ')
print('')


import sys, math
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

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

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def solve():
	N = getInt()
	A = getInts()
	B = list(A)
	if N == 1:
		print(*A)
		return
	two_pow = 2
	while two_pow < max(A):
		two_pow *= 2
	ans_ind = -1
	while two_pow > 0:
		count = 0
		for j in range(N):
			if A[j] >= two_pow:
				count += 1
				ans_ind = j
				A[j] -= two_pow
		if count == 1:
			break
		two_pow >>= 1
	if ans_ind < 0:
		print(*B)
		return
	B = [B[ans_ind]] + B[:ans_ind] + B[ans_ind + 1:]
	print(*B)
solve()


import sys
import itertools
import collections
n = int(input())
A = [int(x) for x in input().split()]
perms = []

def bits(lst):
	a = []
	for val in lst:
		s = []
		for shift in range(29, -1, -1):
			s += [str(val >> shift & 1)]
		a += [''.join(s[::-1])]
	return a

def calcCost(perm):
	f = lambda a, b: (a | b) - b
	a = f(perm[0], perm[1])
	for i in range(2, len(perm)):
		a = f(a, perm[i])
	return a

def bruteForce():
	global perms
	for perm in list(itertools.permutations(A)):
		perms += [(perm, calcCost(perm))]
	print(*perms, sep='\n')
calcBits = bits(A)
for i in range(29, -1, -1):
	one = 0
	swapIndex = None
	for ii in range(n):
		if calcBits[ii][i] == '1':
			one += 1
			swapIndex = ii
	if one == 1:
		(A[0], A[swapIndex]) = (A[swapIndex], A[0])
		break
print(*A)


from sys import stdin, stdout
import traceback
read = stdin.readline
write = stdout.write
writeln = lambda x: write(str(x) + '\n')

def reads(typ=int):
	return list(map(typ, read().split()))

class Sol:

	@classmethod
	def input(cls):
		s = cls()
		return s

	def solve(self):
		_ = read()
		counts = [0] * 32
		an = reads()
		for a in an:
			self.acc(a, counts)
		ret = 0
		first = 0
		for (i, a) in enumerate(an):
			tmp = self.v(a, counts)
			if tmp > ret:
				ret = tmp
				first = i
		return [an[first]] + an[:first] + an[first + 1:]

	def acc(self, a, counts):
		i = 0
		while a > 0:
			counts[i] += a % 2
			a = a // 2
			i += 1

	def v(self, a, counts):
		ret = 0
		i = 0
		prod = 1
		while a > 0:
			if a % 2 > 0 and counts[i] <= 1:
				assert counts[i] == 1
				ret += prod
			i += 1
			prod *= 2
			a = a // 2
		return ret
try:
	x = Sol.input().solve()
	writeln(' '.join([str(c) for c in x]))
except Exception as e:
	print('Got exception:', repr(e))
	print(traceback.format_exc())


import os
import sys
from io import BytesIO, IOBase
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')
import math
import bisect
from collections import Counter
import bisect
from collections import deque
for _ in range(1):
	n = int(input())
	arr = list(map(int, input().split()))
	f = 0
	if n == 1:
		print(*arr)
	else:
		for j in range(31, -1, -1):
			cnt = 0
			ans = -1
			for i in range(n):
				if arr[i] & 1 << j:
					cnt += 1
					ans = arr[i]
			if cnt == 1:
				f = 1
				arr.remove(ans)
				arr = [ans] + arr
				print(*arr)
				break
		if f == 0:
			print(*arr)


def pro(x, pos):
	global cou
	if x == 0:
		return
	y = 1
	z = 0
	while y <= x:
		y *= 2
		z += 1
	y //= 2
	z -= 1
	while z != -1:
		if x >= y:
			x -= y
			if cou[z] == -1:
				cou[z] = pos
			else:
				cou[z] = -2
		y //= 2
		z -= 1
n = int(input())
mas = list(map(int, input().split()))
cou = []
for i in range(30):
	cou.append(-1)
for i in range(n):
	pro(mas[i], i)
ans = -1
for i in range(29, -1, -1):
	if cou[i] >= 0:
		ans = cou[i]
		break
if ans != -1:
	print(mas[ans], end=' ')
for i in range(n):
	if i != ans:
		print(mas[i], end=' ')
print()


n = int(input(''))
arr = list(map(int, input().split()))
arr.sort(reverse=True)
suff = [0] * (n + 1)
pre = [0] * n
mx = -100000000000000
ind = -1
suff[n - 1] = ~arr[n - 1]
for i in range(n - 2, -1, -1):
	suff[i] = suff[i + 1] & ~arr[i]
for i in range(n):
	if i == 0:
		ase = ~arr[0]
		now = arr[i] & suff[1]
	else:
		now = arr[i]
		if i != n - 1:
			now = arr[i] & suff[i + 1]
		now = now & ase
		ase = ase & ~arr[i]
	if now > mx:
		mx = now
		ind = i
for i in range(n):
	if i == 0:
		ase = ~arr[0]
		now = arr[i] & suff[1]
	else:
		now = arr[i]
		if i != n - 1:
			now = arr[i] & suff[i + 1]
		now = now & ase
		ase = ase & ~arr[i]
	if now > mx:
		mx = now
		ind = i
temp = arr[ind]
arr[ind] = arr[0]
arr[0] = temp
print(*arr)


def f(n):
	pw = 30
	k = ''
	for j in range(pw):
		if n >> j & 1:
			k = '1' + k
		else:
			k = '0' + k
	return k
n = int(input())
a = input().split(' ')
bit = []
used = []
for i in range(30):
	used.append([])
for i in range(n):
	bit.append(f(int(a[i])))
	for j in range(len(bit[i])):
		if bit[i][j] == '1':
			used[j].append(i)
for l in range(30):
	if len(used[l]) != 1:
		continue
	(a[0], a[used[l][0]]) = (a[used[l][0]], a[0])
	break
ans = ' '.join(a)
print(ans)


n = int(input())
a = list(map(int, input().split()))
l = [bin(i)[2:].zfill(32) for i in a]
ind = -1
for i in range(32):
	c = 0
	flag = False
	for j in range(n):
		if l[j][i] == '1':
			if flag:
				ind = -1
				flag = False
				break
			flag = True
			ind = j
	if flag:
		break
if ind == -1:
	print(*a)
else:
	print(a[ind], *(a[i] for i in range(n) if i != ind))


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	idx = 0
	bit = 30
	while bit >= 0:
		count = 0
		for (i, num) in enumerate(a):
			if num >> bit & 1 == 1:
				count += 1
				idx = i
				if count > 1:
					break
		if count == 1:
			break
		bit -= 1
	print(f'{a[idx]} ', end='')
	del a[idx]
	print(' '.join(map(str, a)))

def main():
	solve()
main()


import sys
from collections import Counter
read = lambda : list(map(int, sys.stdin.readline().strip().split()))
n = int(input())
bins = [10 ** 7 for i in range(32)]
a = read()
abar = [~i for i in a]
all_mask = (1 << 32) - 1
temp = all_mask
left = []
right = []
for i in range(n):
	temp &= abar[i]
	left.append(temp)
temp = all_mask
for i in range(n - 1, -1, -1):
	temp &= abar[i]
	right.append(temp)
maxx = 0
max_index = 0
for i in range(n):
	j = n - 1 - (i + 1)
	temp = a[i]
	if i > 0:
		temp &= left[i - 1]
	if j >= 0:
		temp &= right[j]
	if temp > maxx:
		maxx = temp
		max_index = i
print(str(a.pop(max_index)) + ' ' + ' '.join([str(i) for i in 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))


import math
n = int(input())
a = list(map(int, input().split()))
A = max(a)
if A == 0:
	print(' '.join(map(str, a)))
	exit(0)
k = math.ceil(math.log(A, 2))
for i in range(k, -1, -1):
	count = 0
	id = -1
	for (j, x) in enumerate(a):
		if (x >> i) % 2 == 1:
			count += 1
			if count == 1:
				id = j
	if count == 1:
		print(a[id], ' '.join(map(str, a[:id])), ' '.join(map(str, a[id + 1:])))
		exit(0)
print(' '.join(map(str, a)))


N = int(input())
A = list(map(int, input().split()))
ans = -1
for i in range(32, -1, -1):
	count = 0
	ans = -1
	for n in A:
		if 1 << i & n != 0:
			ans = n
			count += 1
	if count == 1:
		break
if ans != -1:
	print(ans, end=' ')
for n in A:
	if n != ans:
		print(n, end=' ')
	else:
		ans = -1


n = int(input())
a = [int(x) for x in input().split()]
complementPrefixes = [-1 for _ in range(n)]
complementSuffixes = [-1 for _ in range(n)]
for i in range(n):
	if i == 0:
		complementPrefixes[i] = ~0
	else:
		complementPrefixes[i] = complementPrefixes[i - 1] & ~a[i - 1]
for i in range(n - 1, -1, -1):
	if i == n - 1:
		complementSuffixes[i] = ~0
	else:
		complementSuffixes[i] = complementSuffixes[i + 1] & ~a[i + 1]
currMax = 0
first = 0
for i in range(n):
	temp = complementPrefixes[i] & a[i] & complementSuffixes[i]
	if temp > currMax:
		first = i
		currMax = temp
ans = [a[first]]
for i in range(n):
	if i != first:
		ans.append(a[i])
print(' '.join([str(x) for x in ans]))


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
n = iinput()
a = linput()
k = 1 << 31
x = -1
while k:
	c = 0
	for i in range(n):
		if a[i] & k:
			c += 1
			x = i
	if c == 1:
		break
	k >>= 1
r = a[x]
a.remove(a[x])
print(r, *a)


n = int(input())
numbers = list(map(int, input().split()))
candidate = 0
prefix = [0] * n
suffix = [0] * n
prefix[0] = suffix[n - 1] = 2147483647
for i in range(1, n):
	prefix[i] = prefix[i - 1] & ~numbers[i - 1]
i = n - 2
while i >= 0:
	suffix[i] = suffix[i + 1] & ~numbers[i + 1]
	i -= 1
ans = -1
pos = -1
for i in range(n):
	tmp = prefix[i] & numbers[i] & suffix[i]
	if tmp > ans:
		ans = tmp
		pos = i
print(numbers[pos], end=' ')
for i in range(n):
	if i != pos:
		print(numbers[i], end=' ')


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)


import sys
n = int(input())
arr = list(map(int, input().split()))
pref = [0] * n
suff = [0] * n
(pref[0], suff[n - 1]) = (sys.maxsize, sys.maxsize)
for i in range(1, n):
	pref[i] = pref[i - 1] & ~arr[i - 1]
for i in range(n - 2, -1, -1):
	suff[i] = suff[i + 1] & ~arr[i + 1]
ans = -sys.maxsize - 1
temp = 0
for i in range(n):
	temp = pref[i] & arr[i] & suff[i]
	if temp > ans:
		ans = temp
		pos = i
print(arr[pos], *arr[:pos], *arr[pos + 1:])


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))


import math

def main():
	n = int(input())
	a = list(map(int, input().split()))
	lst = [[] for _ in range(30)]
	for i in a:
		nm = bin(i)[2:]
		o = 0
		for k in range(len(nm) - 1, -1, -1):
			if nm[k] == '1':
				lst[o].append(i)
			o += 1
	for i in range(len(lst) - 1, -1, -1):
		if len(lst[i]) == 1:
			for k in range(0, len(a)):
				if a[k] & 2 ** i == 2 ** i:
					(a[0], a[k]) = (a[k], a[0])
					for c in a:
						print(c, end=' ')
					return
	for c in a:
		print(c, end=' ')
t = 1
for i in range(t):
	main()


def solution():
	n = int(input())
	l = list(map(int, input().split()))
	nl = []
	pre = [~l[0]]
	suff = []
	for i in range(n):
		x = ~l[i]
		nl.append(x)
		pre.append(x)
		suff.append(x)
	suff.append(~l[-1])
	for i in range(1, n + 1):
		pre[i] &= pre[i - 1]
	for i in range(n - 1, -1, -1):
		suff[i] &= suff[i + 1]
	m = -1
	for i in range(n):
		if i == 0:
			x = l[i] & suff[i + 1]
		elif i == n - 1:
			x = l[i] & pre[i]
		else:
			x = l[i] & suff[i + 1] & pre[i]
		if x > m:
			m = x
			j = i
	(l[0], l[j]) = (l[j], l[0])
	print(*l)
solution()


n = int(input())
a = list(map(int, input().split()))
r = 0
for i in range(29, -1, -1):
	c = 0
	for j in a:
		if j >> i & 1:
			c += 1
			r = j
	if c == 1:
		break
a.remove(r)
print(r, ' '.join(map(str, a)))


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:])))


def answer(n, A):
	if n == 1:
		return A
	pre = [0] * n
	pre[0] = ~A[0]
	for i in range(1, n):
		pre[i] = ~A[i] & pre[i - 1]
	suff = [0] * n
	suff[-1] = ~A[-1]
	for i in range(n - 2, -1, -1):
		suff[i] = ~A[i] & suff[i + 1]
	ele = A[0]
	maxi = A[0] & suff[1]
	ans = 0
	for i in range(1, n):
		if i == n - 1:
			ans = A[i] & pre[i - 1]
		else:
			ans = A[i] & (pre[i - 1] & suff[i + 1])
		if ans > maxi:
			maxi = ans
			ele = A[i]
	A.remove(ele)
	return [ele] + A
n = int(input())
arr = list(map(int, input().split()))
print(*answer(n, arr))


n = int(input())
arr = [int(i) for i in input().split()]
var = 2 ** 30
while var and len([i for i in arr if i & var]) != 1:
	var >>= 1
for i in range(n):
	if var & arr[i]:
		break
(arr[i], arr[0]) = (arr[0], arr[i])
print(*arr)


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

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

def func1(a, b):
	return (a | b) - b

def func2(l):
	val = l[0]
	for i in range(1, n):
		val = func1(val, l[i])
	return val
n = inp()
l = inlt()
d = defaultdict(lambda : 0)
c = defaultdict(lambda : 0)
for each in l:
	x = bin(each)
	x = list(reversed(x[2:]))
	for i in range(len(x)):
		d[i] += int(x[i])
		if d[i] == 1 and int(x[i]):
			c[i] = each
mx = -1
for each in d.keys():
	if d[each] == 1:
		mx = max(mx, each)
if mx == -1:
	print(*l)
else:
	s = [0 for i in range(n)]
	s[0] = c[mx]
	j = 1
	for i in range(n):
		if l[i] != s[0]:
			s[j] = l[i]
			j += 1
	print(*s)


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=' ')


n = int(input())
a = [int(x) for x in input().split()]
l = sorted(a, reverse=True)
x = l[0]
bits = 0
while x > 0:
	x = x >> 1
	bits += 1
num = 0
for i in range(bits):
	count = 0
	ind = 0
	z = 1 << bits - i - 1
	for j in range(len(a)):
		if z & l[j] == z:
			count += 1
			ind = j
		if count > 1:
			break
	if count == 1:
		num = ind
		break
(l[num], l[0]) = (l[0], l[num])
print(*l)


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()


n = int(input())
arrr = [int(j) for j in input().split()]
arr = arrr[:]
for i in range(n):
	arr[i] = bin(arr[i])[2:]
	arr[i] = (30 - len(arr[i])) * '0' + arr[i]
fflag = 0
ans = -1
for i in range(30):
	flag = 0
	ttemp = 0
	for j in range(n):
		if arr[j][i] == '1':
			flag += 1
		if flag == 1 and ttemp == 0:
			ttemp = 1
			ans = j
		if flag == 2:
			break
	if flag == 1:
		fflag = 1
		break
if fflag == 0:
	print(*arrr)
else:
	kek = [arrr[ans]]
	for i in range(n):
		if i != ans:
			kek += [arrr[i]]
	print(*kek)


import math
import sys
from collections import OrderedDict

class Graph:

	def __init__(self, Nodes, is_directed=False):
		self.nodes = Nodes
		self.adj_list = {}
		self.is_directed = is_directed
		for node in self.nodes:
			self.adj_list[node] = []

	def add_edge(self, u, v):
		self.adj_list[u].append(v)
		if self.is_directed == False:
			self.adj_list[v].append(u)

	def print_graph(self):
		for node in self.nodes:
			print(node, '->', self.adj_list[node])

	def degree_node(self, node):
		return len(self.adj_list[node])

	def dfsUTIL(self, v, visited, parents=[]):
		visited[v] = True
		for i in self.adj_list[v]:
			if visited[i] == False:
				self.dfsUTIL(i, visited, parents)
				parents.append(i)

	def dfs(self, v):
		visited = [False] * (max(self.adj_list) + 1)
		parents = [v]
		self.dfsUTIL(v, visited, parents)
		return parents

def factorial(n):
	if n == 0 or n == 1:
		return 1
	ans = 1
	for i in range(2, n + 1):
		ans *= i
	return ans

def dict_sort(ans):
	ans = sorted(ans.items(), reverse=True, key=lambda kv: kv[1])

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

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

def seive_primes(n):
	flag = [0] * (n + 2)
	flag[1] = flag[0] = 1
	i = 2
	while i * i <= n + 1:
		if flag[i] == 0:
			j = i * i
			while j <= n + 1:
				flag[j] = 1
				j += i
		i += 1
	return flag

def factors(n):
	d = {}
	while n % 2 == 0:
		if 2 in d:
			d[2] += 1
		else:
			d[2] = 1
		n /= 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			n /= i
	n = int(n)
	if n > 1:
		d[n] = 1
	return d

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

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

def alphabates():
	return 'abcdefghijklmnopqrstuvwxyz'

def binary_search(ls, n, flag):
	low = 0
	hi = n - 1
	while low <= hi:
		mid = (low + hi) // 2
		if ls[mid] == flag:
			return mid
		elif ls[mid] > flag:
			hi = mid - 1
		else:
			low = mid + 1
	return -1

def qdrt(a, b, c):
	chk = b * b - 4 * a * c
	if chk >= 0:
		ans1 = (-b + chk ** 0.5) / (2 * a)
		ans2 = (-b - chk ** 0.5) / (2 * a)
		return [int(ans1), int(ans2)]
	return -1

def permutation(n, r):
	if n < r:
		return 0
	ans = 1
	for i in range(n - r + 1, n + 1):
		ans *= i
	return ans

def combination(n, r):
	if n < r:
		return 0
	ans = 1
	for i in range(r):
		ans *= n - i
	div = 1
	for i in range(2, r + 1):
		div *= i
	return ans // div

def ispow2(n):
	ans = int(math.log(n, 2))
	if 2 ** ans == n:
		return True
	return False

def _2dmat(n):
	flag = [[i + 1 for i in range(n)]]
	for i in range(2, n + 1):
		new = []
		for j in range((i - 1) * n + 1, i * n + 1):
			new.append(j)
		flag.append(new)
	return flag

def sumn(n):
	return n * (n + 1) // 2

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

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

def main():
	n = int(input())
	ls = arrinp()
	if n == 1:
		print(ls[0])
		exit(0)
	suf = [0] * n
	pre = [0] * n
	for i in range(n):
		if i == 0:
			pre[i] = ~ls[i]
			continue
		pre[i] = pre[i - 1] & ~ls[i]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			suf[i] = ~ls[i]
			continue
		suf[i] = suf[i + 1] & ~ls[i]
	mx = ls[0] & suf[1]
	pos = 0
	for i in range(1, n - 1):
		chk = ls[i] & pre[i - 1] & suf[i + 1]
		if chk > mx:
			mx = chk
			pos = i
	if mx < ls[n - 1] & pre[n - 2]:
		mx = ls[n - 1] & pre[n - 2]
		pos = n - 1
	temp = ls[0]
	ls[0] = ls[pos]
	ls[pos] = temp
	print(' '.join(map(str, ls)))
for _ in range(1):
	main()


N = int(input())
L = list(map(int, input().split()))
R = []
for i in range(N):
	R.append([0] * 32)
cnt = [0] * 32
for i in range(N):
	x = L[i]
	n = 0
	while x:
		if x % 2 == 1:
			R[i][n] = 1
			cnt[n] += 1
		else:
			R[i][n] = 0
		n += 1
		x = x // 2
index = 0
for i in range(31, 0, -1):
	if cnt[i] == 1:
		index = i
		break
ind = 0
for i in range(N):
	if R[i][index] == 1:
		ind = i
		break
print(L[ind], end=' ')
for i in range(N):
	if i != ind:
		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])))


n = int(input())
a = list(map(int, input().split()))
b = [bin(ele)[2:] for ele in a]
b = ['0' * (32 - len(ele)) + ele for ele in b]
found = -1
for i in range(32):
	ans = -1
	count = 0
	for (idx, ele) in enumerate(b):
		if ele[i] == '1':
			count += 1
			ans = idx
	if count == 1:
		found = ans
		break
if found != -1:
	(a[0], a[found]) = (a[found], a[0])
print(*a)


n = int(input())
a = list(map(int, input().split()))
b = set(a)
x = max(a)
d = 0
c = 0
r = 0
while x:
	x >>= 1
	d += 1
for i in range(d - 1, -1, -1):
	c = 0
	for j in b:
		if j >> i & 1:
			c += 1
			r = j
	if c == 1:
		break
a.remove(r)
print(r, ' '.join(map(str, a)))


n = int(input())
a = list(map(int, input().split()))
bitc = [0] * 32
for v in a:
	i = 0
	while v:
		if v & 1:
			bitc[i] += 1
		i += 1
		v >>= 1
bits = 0
for i in range(32):
	if bitc[i] == 1:
		bits = i
st = 0
for i in range(n):
	if 1 << bits & a[i]:
		st = i
		break
s = a[st]
a = a[:st] + a[st + 1:]
print(s, *a)


n = int(input())
arr = list(map(int, input().split()))
s = set()
for i in range(32, -1, -1):
	cnt = 0
	bs = []
	for c in arr:
		if c >> i & 1:
			cnt += 1
			bs.append(c)
	if cnt == 1 and bs[0] not in s:
		print(bs[0], end=' ')
		s.add(bs[0])
for c in arr:
	if c not in s:
		print(c, end=' ')


q = int(input())
numArray = [int(x) for x in input().split()]
binaryArray = ['{:030b}'.format(x) for x in numArray]
max = 0
first = -1
for x in range(30):
	count = 0
	for y in range(q):
		if int(binaryArray[y][x]) == 1:
			count += 1
	if count == 1:
		for y in range(q):
			if int(binaryArray[y][x]) == 1:
				first = y
				break
	if first != -1:
		break
(numArray[0], numArray[first]) = (numArray[first], numArray[0])
print(*numArray)


n = int(input())
a = list(map(int, input().split()))
fl = False
maxqqq = 0
maxi = 0
for i in range(n):
	qqq = a[i]
	for j in range(n):
		if j != i:
			qqq = (qqq | a[j]) - a[j]
			if qqq <= maxqqq:
				break
	if qqq > maxqqq:
		maxqqq = qqq
		maxi = i
		fl = True
if fl:
	(a[maxi], a[0]) = (a[0], a[maxi])
for i in range(n):
	print(a[i], end=' ')


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)
			exit()
	print(*A)
main()


input()
a = input().split()
s = {*range(len(a))}
for x in zip(*(f'{int(x):30b}' for x in a)):
	if x.count('1') == 1:
		t = {i for (i, y) in enumerate(x) if '0' < y}
		if s & t:
			s &= t
print(*[a.pop(s.pop())] + a)


def f(x, y):
	return (x | y) - y
n = int(input())
s = list(map(int, input().split()))
l = []
for i in range(32):
	l.append([])
for i in range(n):
	x = s[i]
	count = 0
	while x > 0:
		if x % 2 != 0:
			l[count].append(s[i])
		x = x // 2
		count += 1
flag = 0
first = 0
for i in range(len(l)):
	if len(l[i]) == 1:
		first = max(l[i])
		flag = 1
if flag == 0:
	for i in range(n):
		print(s[i], end=' ')
else:
	print(first, end=' ')
	s.remove(first)
	for i in range(len(s)):
		print(s[i], end=' ')


n = int(input())
a = list(map(int, input().split()))
b = [-1] * 32
ans = -1
for it in range(n):
	i = a[it]
	bi = bin(i)[2:][-1::-1]
	for (ii, j) in enumerate(bi):
		if j == '1':
			if b[ii] == -1:
				b[ii] = it
			else:
				b[ii] = -2
for i in range(31, -1, -1):
	if b[i] == -1 or b[i] == -2:
		continue
	else:
		ans = b[i]
		break
if ans == -1:
	print(*a)
else:
	print(a[ans], end=' ')
	for i in range(n):
		if i == ans:
			continue
		print(a[i], end=' ')
	print()


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)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d

def check(n, k):
	return n & 1 << k - 1
n = inputnum()
a = inputlist()
a.sort()
cnt = 0
ind = 0
for i in reversed(range(1, 31)):
	for j in reversed(range(n)):
		if check(a[j], i):
			ind = j
			cnt += 1
		if cnt > 1:
			cnt = 0
			break
	if cnt == 1:
		break
print(a[ind], end=' ')
for i in range(n):
	if i != ind:
		print(a[i], end=' ')


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=' ')


n = int(input())
a = list(map(int, input().split()))
before = [~a[0]]
after = [~a[-1]]
for i in range(1, n):
	before.append(before[-1] & ~a[i])
for i in range(n - 2, -1, -1):
	after.append(after[-1] & ~a[i])
after = after[::-1]
(maxi, ind) = (-1, -1)
for i in range(n):
	current = a[i]
	if i == 0:
		if n > 1:
			check = current & after[1]
			if maxi < check:
				maxi = check
				ind = i
		elif maxi < current:
			maxi = current
			ind = i
	elif i >= n - 1:
		if n > 1:
			check = current & before[i - 1]
			if maxi < check:
				maxi = check
				ind = i
		elif maxi < current:
			maxi = current
			ind = i
	else:
		check = before[i - 1] & current & after[i + 1]
		if maxi < check:
			maxi = check
			ind = i
result = [a[ind]] + a[:ind] + a[ind + 1:]
print(*result)


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())
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)))


n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
mask = 2 ** 30
while mask and len([ai for ai in a if ai & mask]) != 1:
	mask >>= 1
for i in range(n):
	if mask & a[i]:
		break
(a[i], a[0]) = (a[0], a[i])
print(*a)


def iinput():
	return [int(x) for x in input().split()]

def main():
	n = int(input())
	data = iinput()
	s = 0
	data1 = []
	for i in range(n):
		data1.append(s)
		s = s | data[i]
	data2 = []
	w = 0
	for i in range(n - 1, -1, -1):
		data2.append(w)
		w = w | data[i]
	data2 = data2[::-1]
	f = 0
	x = 0
	for i in range(n):
		z = abs((data2[i] | data1[i]) - s)
		if f < z:
			f = z
			x = i
	y = data[x]
	data.remove(y)
	data = [y] + data
	for i in range(n):
		data[i] = str(data[i])
	return ' '.join(data)
for t in range(1):
	print(main())


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	x_a = [[] for _ in range(31)]
	for i in range(n):
		for x in range(31):
			if arr[i] & 1 << x:
				x_a[x].append(i)
	m_i = 0
	for x in range(30, -1, -1):
		if len(x_a[x]) == 1:
			m_i = x_a[x][0]
			break
	new_arr = [str(arr[m_i])]
	for i in range(n):
		if i != m_i:
			new_arr.append(str(arr[i]))
	print(' '.join(new_arr))
solve()


import sys
u = sys.stdin.readline
n = int(u())
a = list(map(int, u().split()))
ind = -1
for i in range(40, -1, -1):
	for j in range(n):
		if a[j] & 1 << i:
			if ind == -1:
				ind = j
			else:
				ind = -1
				break
	if ind != -1:
		print(a[ind], end=' ')
		break
for i in range(n):
	if i != ind:
		print(a[i], end=' ')


n = int(input())
l = [(int(el), '{:032b}'.format(int(el))) for el in input().split(' ')]
res = []
ind = -1
for pos in range(32):
	count = 0
	for i in range(n):
		if l[i][1][pos] == '1':
			count += 1
			ind = i
	if count == 1:
		res.append(l[ind][0])
		break
	ind = -1
for i in range(n):
	if i != ind:
		res.append(l[i][0])
for el in res:
	print(el, end=' ')
print()


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)


import math
int_max = 2 ** 31 - 1

def invert(n):
	num = bin(n)[2:]
	num = (31 - len(num)) * '0' + num
	n_num = ''
	for i in num:
		if i == '1':
			n_num += '0'
		else:
			n_num += '1'
	return int(n_num, 2)
n = int(input())
a = list(map(int, input().split()))
pre = [int_max] * n
suf = [int_max] * n
for i in range(1, n):
	pre[i] = pre[i - 1] & invert(a[i - 1])
for i in range(n - 2, -1, -1):
	suf[i] = suf[i + 1] & invert(a[i + 1])
cur_max = 0
ans = 0
for i in range(n):
	val = pre[i] & a[i] & suf[i]
	if val > cur_max:
		cur_max = val
		ans = i
print(a[ans], end=' ')
for i in range(n):
	if i != ans:
		print(a[i], end=' ')
print()


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(a[0])
else:
	pref = [0] * n
	suff = [0] * n
	pref[1] = ~a[0]
	suff[n - 2] = ~a[-1]
	for i in range(2, n):
		pref[i] = pref[i - 1] & ~a[i - 1]
	for i in range(n - 3, -1, -1):
		suff[i] = suff[i + 1] & ~a[i + 1]
	max = a[0] & suff[0]
	pos = 0
	for i in range(1, n):
		if i == n - 1:
			A = pref[i] & a[i]
		else:
			A = pref[i] & a[i] & suff[i]
		if A > max:
			max = A
			pos = i
	print(a[pos], end=' ')
	for i in range(n):
		if i != pos:
			print(a[i], end=' ')


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)


n = int(input())
a = list(map(int, input().split()))
bits = {i: 0 for i in range(0, 32)}
for x in a:
	b = bin(x)[2:]
	counter = 0
	for i in range(len(b) - 1, -1, -1):
		bits[counter] += int(b[i])
		counter += 1
bits = list(filter(lambda x: x[1] == 1, bits.items()))
m = 0
n = -1
for i in a:
	b = bin(i)[2:]
	s = 0
	for l in bits:
		if len(b) > l[0]:
			if b[len(b) - l[0] - 1] == '1':
				s += pow(2, l[0])
	if s > m:
		n = i
		m = s
if n == -1:
	print(*a)
else:
	print(*[n] + [x for x in a if x != n])


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())
arr = [int(p) for p in input().split()]
pre = [~arr[0]]
suff = [~arr[-1]]
for i in range(1, n):
	pre.append(pre[-1] & ~arr[i])
for i in range(n - 2, -1, -1):
	suff.append(suff[-1] & ~arr[i])
curr = 0
if n == 1:
	print(*arr)
	exit(0)
suff = list(reversed(suff))
l = arr[0] & suff[1]
r = arr[-1] & pre[n - 2]
curr = max(l, r)
if curr == l:
	piv = arr[0]
else:
	piv = arr[-1]
for i in range(1, n - 1):
	prev = curr
	curr = max(curr, pre[i - 1] & arr[i] & suff[i + 1])
	if prev != curr:
		piv = arr[i]
ans = [piv]
arr.remove(piv)
arr.insert(0, piv)
print(*arr)


n = int(input())
a = list(map(int, input().split()))
max_c = 0
index = 0
bits = []
for i in range(n):
	w = bin(a[i])[2:]
	bits.append(w)
	p = w.count('1')
	if p > max_c:
		max_c = p
		index = i
yy = max(a)
for i in range(len(bits)):
	bits[i] = '0' * (len(bin(yy)[2:]) - len(bits[i])) + bits[i]
length = len(bin(yy)[2:])
tf = []
for i in range(length):
	count = 0
	for j in range(len(bits)):
		if bits[j][i] == '0':
			count += 1
	if count == len(bits) - 1:
		tf.append(1)
	else:
		tf.append(0)
max_number = 0
answer = 0
for i in range(len(bits)):
	max_n = 0
	for j in range(len(bits[i])):
		if tf[j] == 1 and bits[i][j] == '1':
			max_n += 2 << length - 1 - j
	if max_number < max_n:
		max_number = max_n
		answer = i
print(a[answer], end=' ')
del a[answer]
print(*a)


import os
import sys
from io import BytesIO, IOBase
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')
import bisect
import math
n = int(input())
vals = list(map(int, input().split()))
vals.sort()
vals = [bin(k)[2:] for k in vals]
maxy = 0
for j in range(len(vals[-1])):
	count = 0
	nums = []
	for s in range(len(vals) - 1, -1, -1):
		if len(vals[s]) >= len(vals[-1]) - j:
			d = len(vals[-1]) - len(vals[s])
			if vals[s][j - d] == '1':
				count += 1
				nums.append(s)
	if count == 1:
		maxy = nums[0]
		break
binmax = vals[maxy]
vals.pop(maxy)
common = []
for s in range(n - 1):
	val1 = vals[s]
	com = 0
	for k in range(min(len(binmax), len(val1))):
		if binmax[k] == val1[k] == 1:
			com += 2 ** k
	common.append((com, val1))
common.sort(key=lambda x: x[0])
newy = [str(int(binmax, 2))] + [str(int(s[1], 2)) for s in common]
print(' '.join(newy))


n = int(input())
a = list(map(int, input().split()))
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
read = lambda : list(map(int, sys.stdin.readline().strip().split()))
n = int(input())
a = read()
s = 30
nu = -1
while s >= 0:
	count = 0
	for i in range(n):
		if a[i] & 1 << s:
			count += 1
	if count == 1:
		for i in range(n):
			if a[i] & 1 << s:
				nu = a[i]
				break
		break
	s -= 1
if nu == -1:
	for num in a:
		print(num, end=' ')
	print()
else:
	print(nu, end=' ')
	for num in a:
		if num != nu:
			print(num, end=' ')


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))))


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)


from functools import reduce
import operator
n = int(input())
a = [int(x) for x in input().split()]
pows = [pow(2, i) for i in range(0, 30)]
init = [0 for i in range(0, 30)]
xl = reduce(lambda l, x: [l[i] + (x & pows[i]) for i in range(0, 30)], a, init)
bitmask = [xl[i] / pows[i] for i in range(0, 30)]
xr = 0
for i in range(0, 30):
	if bitmask[i] == 1:
		xr += pows[i]
m = 0
opt = a[0]
for e in a:
	c = xr & e
	if c > m:
		m = c
		opt = e
a.remove(opt)
a = [str(x) for x in a]
print(str(opt) + ' ' + ' '.join(a))


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
freq = {}
can = 0
for msb in range(32):
	for i in a:
		if i & 1 << msb > 0:
			if freq.get(msb) == None:
				freq[msb] = [i]
			else:
				freq[msb].append(i)
k = list(freq.keys())
k.sort(reverse=True)
can = -float('inf')
flag = False
for i in k:
	ll = freq[i]
	if len(ll) == 1:
		can = ll[0]
		flag = True
		break
if flag:
	ans = [can]
else:
	ans = []
for i in a:
	if can != i:
		ans.append(i)
print(*ans)


from math import ceil, log2

def f(a):
	mx = 0
	item = None
	pref = [~a[0]]
	for i in range(1, len(a)):
		pref.append(pref[-1] & ~a[i])
	suf = [~a[-1]]
	for i in range(len(a) - 1 - 1, -1, -1):
		suf.append(suf[-1] & ~a[i])
	suf = suf[::-1]
	for i in range(len(a)):
		ll = (1 << 60) - 1
		rr = (1 << 60) - 1
		if i != 0:
			ll = pref[i - 1]
		if i != len(a) - 1:
			rr = suf[i + 1]
		tt = ll & rr & a[i]
		if tt >= mx:
			mx = tt
			item = i
	mx = a.pop(item)
	return [mx] + a
n = input()
row = list(map(int, input().strip().split()))
print(*f(row))


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)


def wrapper(func):

	def inner():
		return next(func)
	return inner
n = int(input())
a = list(map(int, input().strip().split()))
INF = (1 << 60) - 1
pre = [0] * (n + 1)
pre[n] = INF
for i in range(n):
	if i == 0:
		pre[i] = ~a[i]
	else:
		pre[i] = pre[i - 1] & ~a[i]
suf = [0] * (n + 1)
suf[n] = INF
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suf[i] = ~a[i]
	else:
		suf[i] = suf[i + 1] & ~a[i]
(mx, id) = (-1, -1)
for i in range(n):
	tmp = a[i] & pre[i - 1] & suf[i + 1]
	if tmp > mx:
		mx = tmp
		id = i
print(a[id], end=' ')
for i in range(n):
	if i == id:
		continue
	print(a[i], end=' ')


n = int(input())
arr = [int(i) for i in input().split()]
ans = [i for i in arr]

def fun(arr, n, ans):
	for i in range(31, -1, -1):
		(f, v) = (0, 0)
		for j in range(n):
			if arr[j] & 1 << i != 0:
				f += 1
				v = j
			if f > 1:
				break
		if f == 1:
			(ans[0], ans[v]) = (ans[v], ans[0])
			return ans
	return ans
ans = fun(arr, n, ans)
for i in ans:
	print(i, end=' ')


n = int(input())
A = [int(x) for x in input().split()]
bit = 2 ** 30
while bit:
	B = []
	for i in range(n):
		if A[i] & bit:
			B.append(i)
	if len(B) == 1:
		print(' '.join((str(x) for x in [A[B[0]]] + A[:B[0]] + A[B[0] + 1:])))
		exit()
	bit >>= 1
print(' '.join((str(x) for x in A)))


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()


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)))


n = int(input())
a = [int(s) for s in input().split()]
b = [-1 for i in range(32)]
for i in range(n):
	t = a[i]
	j = 0
	while t > 0:
		if t % 2 == 1:
			if b[j] == -1:
				b[j] = i
			elif b[j] >= 0:
				b[j] = -2
		j += 1
		t //= 2
k = 0
for i in range(31, -1, -1):
	if b[i] >= 0:
		k = b[i]
		break
print(a[k], end=' ')
for i in range(n):
	if i != k:
		print(a[i], end=' ')


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())
a = input().split()
ls = [[0 for i in range(31)] for j in range(n)]
for i in range(n):
	a[i] = int(a[i])
	k = 1
	while k < 32:
		if a[i] & 1 << k - 1:
			ls[i][k - 1] = 1
		k += 1
j = 30
while j >= 0:
	count = 0
	store = 0
	for i in range(n):
		if ls[i][j]:
			count += 1
			store = i
	if count == 1:
		break
	j -= 1
print(a[store], end=' ')
for i in range(n):
	if i != store:
		print(a[i], end=' ')


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 sys
from collections import defaultdict
input = sys.stdin.readline
a = int(input())
z = list(map(int, input().split()))
ans = [0 for i in range(50)]
al = defaultdict(int)
save = 0
flag = 0
for i in range(len(z)):
	kap = str(bin(z[i]))
	s = kap[2:]
	for t in range(len(s)):
		if s[t] == '1':
			ans[len(s) - t - 1] += 1
			if ans[len(s) - t - 1] == 1:
				al[len(s) - t - 1] = z[i]
for d in range(len(ans) - 1, -1, -1):
	if ans[d] == 1:
		save = d
		flag = 1
		break
if flag == 1:
	z.remove(al[save])
	z.append(al[save])
	z = z[::-1]
	print(' '.join(map(str, z)))
else:
	print(' '.join(map(str, z)))


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)


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:]))))


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)))


import math, collections, sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
number = [[] for i in range(33)]
for i in a:
	binary = bin(i)[2:].zfill(32)
	for j in range(32):
		if binary[j] == '1':
			number[j].append(i)
first = -1
for i in number:
	if len(i) == 1:
		first = i[0]
		break
if first == -1:
	print(*a)
	exit()
ans = []
for i in a:
	if i != first:
		ans.append(i)
ans.append(first)
ans.reverse()
print(*ans)


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:])


n = int(input())
l = list(map(int, input().split()))
bitcount = [0] * 64
for v in l:
	i = 0
	while v:
		if v & 1:
			bitcount[i] += 1
		v >>= 1
		i += 1
bitstart = 0
for i in range(64):
	if bitcount[i] == 1:
		bitstart = i
start = 0
for v in l:
	if 1 << bitstart & v:
		start = v
		break
print(' '.join(map(str, sorted(l, key=lambda x: abs(x - start)))))


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()


n = int(input())
arr = list(map(int, input().split()))
bs = 0
for i in range(32, -1, -1):
	cnt = 0
	for (j, c) in enumerate(arr):
		if c >> i & 1:
			cnt += 1
			bs = j
	if cnt == 1:
		break
print(arr[bs], end=' ')
for (j, c) in enumerate(arr):
	if j != bs:
		print(c, 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())
array = list(map(int, input().split()))
max = 0
for i in array:
	if i > max:
		max = i
k = max
max = 0
while k != 0:
	k = int(k / 2)
	max += 1
array_of_arrays = []
for i in range(0, max):
	array_of_arrays.append([])
for i in range(0, n):
	k = array[i]
	counter = 0
	while k != 0:
		rem = k % 2
		if rem == 1:
			array_of_arrays[counter].append(i)
		k = int(k / 2)
		counter += 1
for i in range(0, max):
	if len(array_of_arrays[max - 1 - i]) == 1:
		first = array_of_arrays[max - 1 - i][0]
		temp = array[first]
		array[first] = array[0]
		array[0] = temp
		break
print(*array)


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)


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())
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)


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=' ')


n = int(input())
lst = list(map(int, input().split()))
positions = {}
for num in lst:
	t = str(bin(num))
	t = t[2:]
	t = t[::-1]
	for j in range(len(t)):
		if t[j] == '1':
			if j in positions:
				positions[j][1] += 1
			else:
				positions[j] = [0, 1]
		elif j in positions:
			positions[j][0] += 1
		else:
			positions[j] = [1, 0]
mx = 0
ans = -1
for num in lst:
	t = str(bin(num))
	t = t[2:][::-1]
	unique = [0] * len(t)
	for j in range(len(t)):
		cur = t[j]
		if cur == '0':
			continue
		elif positions[j][1] == 1:
			unique[j] = 1
	nm = ''.join(list(map(str, unique)))[::-1]
	nm = int(nm, 2)
	if nm >= mx:
		ans = num
		mx = nm
pos = lst.index(ans)
print(ans, end=' ')
for i in range(n):
	if i == pos:
		continue
	print(lst[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())
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()


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)


n = int(input())
a = list(map(int, input().split()))
r = 0
for i in range(29, -1, -1):
	c = 0
	for j in a:
		if j >> i & 1:
			c += 1
			r = j
	if c == 1:
		break
a.remove(r)
print(r, ' '.join(map(str, a)))
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)
input = sys.stdin.readline

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

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

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

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

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
n = ii()
a = li()
pos = -1
for j in range(32):
	(cnt, tmp) = (0, -1)
	for i in range(n):
		if a[i] >> j & 1:
			cnt += 1
			tmp = i
	if cnt == 1:
		pos = tmp
ans = []
if pos != -1:
	ans.append(a[pos])
for i in range(n):
	if i != pos:
		ans.append(a[i])
prr(ans, ' ')


def main():
	n = int(input())
	aa = list(map(int, input().split()))
	ll = [0] * n
	rr = [0] * n
	s = 0
	for i in range(n):
		s |= aa[i]
		ll[i] = s
	s = 0
	for i in range(n - 1, -1, -1):
		s |= aa[i]
		rr[i] = s
	mx = 0
	mxi = 0
	for i in range(n):
		s = 0
		if i - 1 >= 0:
			s |= ll[i - 1]
		if i + 1 < n:
			s |= rr[i + 1]
		cur = (aa[i] | s) - s
		if cur > mx:
			mx = cur
			mxi = i
	(aa[0], aa[mxi]) = (aa[mxi], aa[0])
	print(*aa)
main()


from sys import *
from math import *
from bisect import *
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
for i in range(32, -1, -1):
	s = x = 0
	for j in range(n):
		if 1 << i & a[j] != 0:
			x = j
			s += 1
	if s == 1:
		break
(a[0], a[x]) = (a[x], a[0])
print(*a)


n = int(input())
a = list(map(int, input().split()))
c = -float('inf')
val = 0
for i in range(32, -1, -1):
	ct = 0
	for j in a:
		v = 1 << i
		if j & v > 0:
			ct += 1
			val = j
	if ct == 1:
		c = val
		break
a.sort(reverse=True)
ans = []
if c > 0:
	ans.append(c)
for i in a:
	if i != c:
		ans.append(i)
print(*ans)


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())
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))


n = int(input())
a = list(map(int, input().split()))
mask = 2 ** 30
while mask and len([ai for ai in a if ai & mask]) != 1:
	mask //= 2
if mask:
	i = next((i for i in range(n) if a[i] & mask))
	(a[i], a[0]) = (a[0], a[i])
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=' ')


def main():
	n = int(input())
	a = list(map(int, input().split()))
	st = 2 ** 30
	while st > 0:
		ans = 0
		ind = 0
		for i in range(n):
			if a[i] % (st * 2) >= st:
				ans += 1
				ind = i
		if ans == 1:
			(a[0], a[ind]) = (a[ind], a[0])
			print(*a)
			return
		st = st // 2
	print(*a)
main()


from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
mask = 2 ** 30
while mask and len([ai for ai in a if ai & mask]) != 1:
	mask //= 2
if mask:
	i = next((i for i in range(n) if a[i] & mask))
	(a[i], a[0]) = (a[0], a[i])
print(*a)


from math import ceil, sqrt

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())
n = ii()
a = li()
m = {}
a.sort()
for i in range(32):
	m[i] = []
for i in a:
	x = bin(i)
	x = x[2:]
	if i == 0:
		m[31].append(i)
		continue
	y = 32 - len(x)
	x = '0' * y + x
	for j in range(32):
		if x[j] == '1':
			m[j].append(i)
f = 0
for i in range(32):
	if len(m[i]) == 1:
		x = m[i][0]
		f = 1
		break
if f == 0:
	print(*a)
else:
	print(x, end=' ')
	a.remove(x)
	print(*a)


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=' ')


n = int(input())
num = input().split()
for i in range(n):
	num[i] = int(num[i])
finals = []
l = [0] * n
for i in range(32, -1, -1):
	t = 0
	k = 0
	for j in range(n):
		a = num[j]
		if (a >> i) % 2 == 1 and l[j] != 1:
			k = j
			t += 1
	if t == 1:
		finals.append(num[k])
		l[k] = 1
for i in range(n):
	if l[i] != 1:
		finals.append(num[i])
for i in range(n):
	print(finals[i], end=' ')


N = int(input())
XX = list(map(int, input().split()))
X = ['{:030b}'.format(x) for x in XX]
for i in range(30):
	xj = None
	for (j, x) in enumerate(X):
		if x[i] == '1':
			if xj != None:
				xj = None
				break
			xj = j
	else:
		if xj != None:
			break
if xj == None:
	print(*XX)
else:
	R = [XX[xj]]
	for i in range(N):
		if i == xj:
			continue
		R.append(XX[i])
	print(*R)


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

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
n = int(input())
l = list(map(int, input().split()))
k = [0] * 32
u = 0
m = 0
for i in l:
	x = format(i, 'b')[::-1]
	for j in range(len(x)):
		if int(x[j]):
			k[j] += 1
p = 31 - (k[::-1].index(1) if 1 in k else 31)
for i in range(n):
	if l[i] >> p & 1:
		u = i
		break
print(l[u], *l[:u], *l[u + 1:])


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

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

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
pre = [~a[0]]
for i in range(1, n):
	pre.append(pre[-1] & ~a[i])
a = a[::-1]
suf = [~a[0]]
for i in range(1, n):
	suf.append(suf[-1] & ~a[i])
suf = suf[::-1]
a = a[::-1]
ma = -inf
key = 0
here = 0
for i in range(n):
	try:
		if i != 0 and i != n - 1:
			here = suf[i + 1] & pre[i - 1] & a[i]
		elif i == 0:
			here = suf[i + 1] & a[i]
		else:
			here = pre[i - 1] & a[i]
	except:
		pass
	if here > ma:
		ma = here
		key = i
print(a[key], *[a[i] for i in range(n) if i != key])


import sys
import collections
input = sys.stdin.readline

def solve(x, y):
	return (x | y) - y

def main():
	N = int(input())
	A = [int(x) for x in input().split()]
	acc1 = [0] * N
	acc1[0] = A[0]
	for i in range(1, N):
		acc1[i] = acc1[i - 1] | A[i]
	acc2 = [0] * N
	acc2[-1] = A[-1]
	for i in range(N - 2, -1, -1):
		acc2[i] = acc2[i + 1] | A[i]
	if N == 1:
		print(A[0])
		return
	ma = -1
	memo = 0
	for i in range(N):
		if i == 0:
			x = solve(A[i], acc2[1])
		elif i == N - 1:
			x = solve(A[i], acc1[N - 2])
		else:
			x = solve(A[i], acc2[i + 1] | acc1[i - 1])
		tmp = ma
		ma = max(x, ma)
		if ma != tmp:
			memo = i
	print(A[memo], end=' ')
	for (i, v) in enumerate(A):
		if i == memo:
			continue
		print(v, end=' ')
main()


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())
arr = list(map(int, input().split(' ')))
arr.sort()
arr.reverse()
binarr = list(map(lambda s: str(bin(s))[2:], arr))
l = len(binarr[0])
start = 0
placed = []
placed2 = {}
for pos in range(l):
	yes = None
	for a in binarr:
		a = a.zfill(l)
		if a[pos] == '1' and yes == None:
			yes = a
		elif a[pos] == '1' and yes != None:
			yes = None
			break
	if yes != None:
		if not placed2.get(yes):
			placed.append(yes)
			placed2[yes] = True
placed = list(map(lambda s: int(s, 2), placed))
for p in placed:
	print(p, end=' ')
for a in arr:
	if a not in placed:
		print(a, end=' ')


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
data = []
use = [True for _ in range(n)]
for i in range(30, -1, -1):
	key = -1
	for (j, x) in enumerate(a):
		if x >> i & 1 and key == -1:
			key = j
		elif x >> i & 1:
			key = -1
			break
	if key != -1:
		if use[key]:
			data.append(key)
			use[key] = False
ans = []
for key in data:
	ans.append(a[key])
for i in range(n):
	if use[i]:
		ans.append(a[i])
print(*ans)


def checker(n, lst):
	(a, b) = ([0] * n, [0] * n)
	for i in range(1, n):
		a[i] = a[i - 1] | lst[i - 1]
		b[n - i - 1] = b[len(lst) - i] | lst[n - i]
	(elem, pos) = (-1, -1)
	for i in range(n):
		y = a[i] | b[i]
		cur = (lst[i] | y) - y
		if cur > elem:
			(elem, pos) = (cur, i)
	(lst[0], lst[pos]) = (lst[pos], lst[0])
	return ' '.join([str(x) for x in lst])
m = int(input())
c = [int(i) for i in input().split()]
print(checker(m, c))


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=' ')


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=' ')


def solve(a):
	count = [0] * 30
	for e in a:
		for i in range(30):
			if e & 1 << i:
				count[i] += 1
	i = 29
	while i >= 0 and count[i] != 1:
		i -= 1
	if i < 0:
		return a
	for j in range(len(a)):
		if a[j] & 1 << i:
			return [a[j]] + a[:j] + a[j + 1:]
	assert False
n = int(input())
a = list(map(int, input().split()))
print(*solve(a))


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()))
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 collections import Counter
n = int(input())
a = list(map(int, input().split()))
bt = [0] * 32
for i in range(n):
	for j in range(32):
		if a[i] & 1 << j:
			bt[j] += 1
mx = 0
for i in range(n):
	tmp = 0
	for j in range(32):
		if a[i] & 1 << j and bt[j] == 1:
			tmp += 1 << j
	if tmp >= mx:
		idx = i
		mx = tmp
(a[idx], a[0]) = (a[0], a[idx])
print(*a)


I = lambda : int(input())
IL = lambda : list(map(int, input().split()))
n = I()
A = IL()
smask = 0
for i in range(32):
	mask = 1 << i
	c = sum((a & mask > 0 for a in A))
	if c > 1:
		smask += mask
B = [a - (a & smask) for a in A]
ma = max(B)
mi = [i for (i, a) in enumerate(B) if a == ma][0]
mA = A.pop(mi)
print(mA, *A)


input()
a = input().split()
for x in zip(*(f'{int(x):30b}' for x in a)):
	if x.count('1') == 1:
		i = x.index('1')
		a = [a.pop(i)] + a
		break
print(*a)


n = int(input())
mx = 0
a = [0] * n
k = 0
for i in input().split():
	a[k] = int(i)
	mx = max(a[k], mx)
	k += 1
m = 0
while 2 ** m <= mx:
	m += 1
rz = 0
for i in range(m, -1, -1):
	if rz:
		break
	for j in range(n):
		if a[j] & 2 ** i:
			if rz:
				rz = 0
				break
			else:
				rz = a[j]
if rz:
	print(rz, '', end='')
	a.remove(rz)
	n -= 1
if n:
	for i in range(n):
		print(a[i], '', end='')


import sys
from math import *
from itertools import permutations as pm
input = sys.stdin.readline
t = 1
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	for i in range(40):
		d[i] = 0
	for i in range(n):
		x = list(bin(a[i])[2:])
		x = x[::-1]
		for j in range(len(x)):
			if x[j] == '1':
				d[j] += 1
	maxi = 0
	z = -1
	for i in range(n):
		x = list(bin(a[i])[2:])
		x = x[::-1]
		s = 0
		for j in range(len(x)):
			if x[j] == '1' and d[j] == 1:
				s += 2 ** j
		if s > maxi:
			maxi = s
			z = i
	if z == -1:
		print(*a)
	else:
		q = a[z]
		a.pop(z)
		print(q, *a)


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=' ')


import sys
n = int(input())
lst = list(map(int, input().split()))
pre = [0 for i in range(n + 1)]
suf = [0 for i in range(n + 1)]
pre[0] = sys.maxsize
for i in range(n):
	pre[i + 1] = pre[i] & ~lst[i]
suf[n] = sys.maxsize
for i in range(n - 1, -1, -1):
	suf[i] = suf[i + 1] & ~lst[i]
Max = -sys.maxsize
Ans = None
for i in range(n):
	temp = pre[i] & lst[i] & suf[i + 1]
	if temp > Max:
		Ans = i
		Max = temp
print(lst[Ans], end=' ')
for i in range(n):
	if i == Ans:
		continue
	else:
		print(lst[i], end=' ')
print()


n = int(input())
arr = list(map(int, input().split()))
for bit in range(30, -1, -1):
	cnt = 0
	msb = -1
	for i in range(n):
		if arr[i] & 1 << bit:
			cnt += 1
			msb = i
	if cnt == 1:
		print(arr[msb], end=' ')
		for i in range(n):
			if i != msb:
				print(arr[i], end=' ')
		exit()
print(*arr)


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(' ')))
if n == 1:
	print(a[0])
else:
	prefix = [0] * n
	suffix = [0] * n
	prefix[0] = ~a[0]
	suffix[n - 1] = ~a[n - 1]
	for i in range(1, n):
		prefix[i] = prefix[i - 1] & ~a[i]
	for i in range(2, n + 1):
		suffix[n - i] = suffix[n - i + 1] & ~a[n - i]
	element = -1 * 2 ** 32
	elem_index = 0
	for i in range(n):
		if i == 0:
			if a[i] & suffix[i + 1] > element:
				element = a[i] & suffix[i + 1]
				elem_index = i
		elif i == n - 1:
			if a[i] & prefix[i - 1] > element:
				element = a[i] & prefix[i - 1]
				elem_index = i
		elif a[i] & prefix[i - 1] & suffix[i + 1] > element:
			element = a[i] & prefix[i - 1] & suffix[i + 1]
			elem_index = i
	temp = a[0]
	a[0] = a[elem_index]
	a[elem_index] = temp
	print(' '.join(list(map(str, a))))


from collections import defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
from math import gcd, floor, ceil
from sys import stdin, stdout

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

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

def solve(a):
	n = len(a)
	count = [0] * 32
	for i in range(n):
		for j in range(32):
			count[j] += a[i] >> j & 1
	maxi = [0, 0]
	for i in range(n):
		num = 0
		for j in range(32):
			b = a[i] >> j & 1
			if b == 1 and count[j] == 1:
				num += 1 << j
		if num > maxi[0]:
			maxi = [num, i]
	i = maxi[1]
	return ' '.join([str(e) for e in [a[i]] + a[:i] + a[i + 1:]])
n = iint()
a = ilist()
print(solve(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:])


n = int(input())
l = list(map(int, input().split()))
b = [0] * 32
for i in l:
	x = bin(i)[2:]
	d = 32 - len(x)
	s = '0' * d + x
	j = 0
	while j < 32:
		if s[j] == '1':
			b[j] += 1
		j += 1
f = -1
for i in range(len(b)):
	if b[i] == 1:
		f = i
		break
if f == -1:
	print(*l)
else:
	p = []
	v = set()
	for i in l:
		x = bin(i)[2:]
		d = 32 - len(x)
		s = '0' * d + x
		if s[f] == '1':
			p.append(i)
			v.add(i)
	for i in l:
		if i not in v:
			p.append(i)
	print(*p)


import math

class SegmentTree:

	def __init__(self, data, default, func=lambda a, b: a & b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
n = int(input())
l = list(map(int, input().split()))
e = max(l)
c = 0
while e > 0:
	e //= 2
	c += 1
l1 = [pow(2, c) - 1 - l[i] for i in range(n)]
s = SegmentTree(l1, pow(2, c) - 1)
ma = [0] * n
for i in range(n):
	if i == 0:
		ma[i] = l[i] & s.query(1, n - 1)
	elif i == n - 1:
		ma[i] = l[i] & s.query(0, n - 2)
	else:
		ma[i] = l[i] & s.query(0, i - 1) & s.query(i + 1, n - 1)
ind = ma.index(max(ma))
no = l[ind]
ans = [0] * n
ans[0] = no
t = 1
for i in range(n):
	if i == ind:
		continue
	else:
		ans[t] = l[i]
		t += 1
print(*ans, sep=' ')


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)


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])


import sys
input = sys.stdin.readline
from collections import *
n = int(input())
a = list(map(int, input().split()))
s = [0] * 40
for ai in a:
	for i in range(40):
		if ai >> i & 1:
			s[i] += 1
M = -1
f = -1
for i in range(n):
	M_cand = 0
	for j in range(40):
		if a[i] >> j & 1 and s[j] - 1 == 0:
			M_cand += 2 ** j
	if M_cand > M:
		M = M_cand
		f = i
ans = [a[f]]
for i in range(n):
	if i != f:
		ans.append(a[i])
print(*ans)


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()


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])


import sys
input = sys.stdin.readline
from itertools import permutations
n = int(input())
a = [int(item) for item in input().split()]
cnt = [0] * 32
for item in a:
	for i in range(32):
		if item & 1 << i:
			cnt[i] += 1
mask = 0
for (i, item) in enumerate(cnt):
	if item == 1:
		mask |= 1 << i
max_val = -1
index = -1
for (i, item) in enumerate(a):
	if item & mask > max_val:
		max_val = item & mask
		index = i
ans = [a[index]] + a[:index] + a[index + 1:]
print(*ans)


n = 0
a = [0]
i = 0
j = 0
x = 0
b = [0]
n = int(input())
b = list(map(int, input().split()))
a = a + b
cnt = [0] * 32
for i in range(1, n + 1):
	x = a[i]
	for j in range(0, 32):
		cnt[j] += x & 1
		x >>= 1
high = 0
for high in range(31, -1, -1):
	if cnt[high] == 1:
		break
if high == -1:
	for i in range(1, n + 1):
		print(a[i], end=' ')
else:
	r = 0
	for r in range(1, n + 1):
		if 1 << high & a[r] != 0:
			break
	print(a[r], end=' ')
	for i in range(1, n + 1):
		if i == r:
			continue
		else:
			print(a[i], end=' ')


import collections, heapq

def main():
	for _ in range(1):
		n = int(input().strip())
		a = [int(x) for x in input().strip().split(' ')]
		b = ['{:b}'.format(x)[::-1] for x in a]
		idx = 0
		for l in range(32, 0, -1):
			nums = 0
			for (i, c) in enumerate(b):
				if len(c) >= l and c[l - 1] == '1':
					nums += 1
					idx = i
				if nums >= 2:
					break
			if nums == 1:
				break
		if idx < len(a):
			print(a[idx], end='')
		for i in range(len(a)):
			if i != idx:
				print('', a[i], end='')
main()


from collections import *
(n, a) = (int(input()), list(map(int, input().split())))
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 + 1:], *a[:first])


n = int(input())
vals = list(map(int, input().split()))
(pref, suff) = ([0] * (n + 1), [0] * (n + 1))
for i in range(n):
	pref[i + 1] = pref[i] | vals[i]
	suff[n - i - 1] = suff[n - i] | vals[n - i - 1]
ret = (-float('inf'), -float('inf'))
for (i, a) in enumerate(vals):
	b = pref[i] | suff[i + 1]
	ret = max(ret, ((a | b) - b, i))
print(*[vals[ret[1]]] + [v for (i, v) in enumerate(vals) if i != ret[1]])


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


from itertools import permutations
import math
import sys
n = int(input())
a = input().split(' ')
b = [0] * n
for i in range(0, n):
	a[i] = b[i] = int(a[i])
for i in range(0, 31):
	cnt = 0
	for j in range(0, n):
		if a[j] >> i & 1:
			cnt = cnt + 1
	if cnt >= 2:
		for j in range(0, n):
			if a[j] >> i & 1:
				b[j] = b[j] - (1 << i)
c = []
for i in range(0, n):
	c.append((a[i], b[i]))
c.sort(key=lambda s: -s[1])
for i in range(0, n):
	print(c[i][0], end=' ')


import sys
import bisect
from math import ceil, floor
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
mod = 10 ** 9 + 7
n = int(ri())
a = RI()
pre = [-1] * (n + 1)
suf = [-1] * (n + 1)
for i in range(n):
	pre[i + 1] = ~a[i] & pre[i]
for i in range(n - 1, -1, -1):
	suf[i] = suf[i + 1] & ~a[i]
ans = -1
posans = 0
for i in range(len(a)):
	if pre[i] & suf[i + 1] & a[i] > ans:
		ans = pre[i] & suf[i + 1] & a[i]
		posans = i
print(a[posans], end=' ')
for i in range(len(a)):
	if i != posans:
		print(a[i], end=' ')
print()


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)))


N = int(input())
nums = [int(x) for x in input().split()]
bin_nums = [bin(x)[2:] for x in nums]
for pos in range(N):
	length = len(bin_nums[pos])
	bin_nums[pos] = '0' * (32 - length) + bin_nums[pos]
ind = 0
for bit in range(32):
	one = 0
	for pos in range(N):
		if bin_nums[pos][bit] == '1':
			one += 1
			ind = pos
	if one == 1:
		break
output = [nums[ind]]
nums.remove(nums[ind])
output += nums
output = [str(x) for x in output]
print(' '.join(output))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
arr = list(map(int, input().split()))
ans = -1
for i in range(32, -1, -1):
	s = 0
	ans = -1
	for j in arr:
		if 1 << i & j != 0:
			ans = j
			s += 1
	if s == 1:
		break
lis = []
if ans != -1:
	lis.append(ans)
for j in arr:
	if j != ans:
		lis.append(j)
	else:
		ans = -1
print(' '.join((str(x) for x in lis)))


import sys, math
MOD = 998244353
n = int(input())
a = list(map(int, input().split()))
x = [set() for i in range(32)]
for i in range(len(a)):
	t = a[i]
	j = 0
	while t > 0:
		if t & 1:
			x[j].add(i)
		t = t >> 1
		j += 1
indx = -1
for i in reversed(range(32)):
	if len(x[i]) == 1:
		indx = x[i].pop()
		break
if indx != -1:
	a = [a[indx]] + a[:indx] + a[indx + 1:]
print(*a)


n = int(input())
l = [int(x) for x in input().split()]
if n == 1:
	print(l[0])
	exit(0)
pre = l[0:n]
suf = l[0:n]
for i in range(1, n):
	pre[i] |= pre[i - 1]
for i in range(n - 2, -1, -1):
	suf[i] |= suf[i + 1]
ans = 0
cur = 0
k = 0
for i in range(n):
	if i == 0:
		cur = ~(~l[i] | suf[i + 1])
	elif i == n - 1:
		cur = ~(pre[i - 1] | ~l[i])
	else:
		cur = ~(pre[i - 1] | ~l[i] | suf[i + 1])
	if cur > ans:
		ans = cur
		k = i
ans = [l[k]]
ans.extend([x for (i, x) in enumerate(l) if i != k])
ans = [str(x) for x in ans]
print(' '.join(ans))


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()


from operator import or_
from itertools import accumulate
_ = input()
l = [*map(int, input().strip().split())]
lx = [0, *accumulate(l, or_)]
rx = [*reversed(list(accumulate(reversed(l), or_))), 0]
(_, idx) = max(((x & ~(a | b), i) for (i, (x, a, b)) in enumerate(zip(l, lx, rx[1:]))))
print(*l[idx:], *l[:idx])


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)


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=' ')


n = int(input())
a = list(map(int, input().split()))
ans = 0
f = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
	f[i] = a[i] | (f[i - 1] if i - 1 >= 0 else 0)
for i in range(n - 1, -1, -1):
	b[i] = a[i] | (b[i + 1] if i + 1 < n else 0)
index = 0
for i in range(n):
	left = (f[i - 1] if i - 1 >= 0 else 0) | (b[i + 1] if i + 1 < n else 0)
	temp = a[i] - (a[i] & left)
	ans = max(temp, ans)
	if temp == ans:
		index = i
print(a[index], end=' ')
for i in range(n):
	if i != index:
		print(a[i], end=' ')
print('')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = 1
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		b = [None] * 32
		for (k, c) in enumerate(a):
			i = 0
			while c > 0:
				if c % 2:
					if b[i] is None:
						b[i] = k
					else:
						b[i] = -1
				i += 1
				c //= 2
		r = a
		for k in b[::-1]:
			if k is not None and k >= 0:
				r = [a[k]] + a[:k] + a[k + 1:]
				break
		rr.append(JA(r, ' '))
	return JA(rr, '\n')
print(main())


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().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]) + ' ')
	for i in range(n):
		if i != idx:
			sys.stdout.write(str(a[i]) + ' ')
else:
	for i in range(n):
		sys.stdout.write(str(a[i]) + ' ')


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
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
bit = [0] * 31
for i in a:
	for j in range(31):
		if i & 1 << j:
			bit[j] += 1
ma = 0
ans = 0
for i in a:
	maa = 0
	for j in range(31):
		if i & 1 << j:
			if bit[j] <= 1:
				maa += 1 << j
		elif bit[j] == 0:
			maa += 1 << j
	if ma < maa:
		ma = maa
		ans = i
b = [ans]
for i in a:
	if ans == i:
		ans = -1
	else:
		b.append(i)
print(*b)


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)


n = int(input())
a = [int(i) for i in input().split()]
ones = [set() for _ in range(34)]
for (idx, i) in enumerate(a):
	for j in range(34):
		if i >> j & 1 == 1:
			ones[j].add(idx)
maxi = 0
for j in range(33, -1, -1):
	if len(ones[j]) == 1:
		maxi = ones[j].pop()
		break
(a[maxi], a[0]) = (a[0], a[maxi])
print(' '.join([str(i) for i in a]))


n = int(input())
l = list(map(int, input().split()))
a = [[] for i in range(32)]
for i in range(n):
	num = l[i]
	j = 0
	while num > 0:
		if num % 2 == 1:
			a[j].append(i)
		num = num // 2
		j += 1
flag = 0
ind = 0
for i in range(31, -1, -1):
	if len(a[i]) == 1:
		ind = a[i][0]
		flag = 1
		break
if flag == 0:
	for i in l:
		print(i, end=' ')
	print()
else:
	temp = l[0]
	l[0] = l[ind]
	l[ind] = temp
	for i in l:
		print(i, end=' ')
	print()


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)))


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)))


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=' ')


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ruiseki = [0] * (n + 1)
ruiseki2 = [0] * (n + 1)
for i in range(n):
	ruiseki[i + 1] = ruiseki[i] | a[i]
for i in range(n):
	ruiseki2[i + 1] = ruiseki2[i] | a[n - (i + 1)]
ans = 0
ind = 0
for i in range(n):
	tmp = ruiseki[i] | ruiseki2[n - (i + 1)]
	if ans <= (tmp | a[i]) - tmp:
		ans = (tmp | a[i]) - tmp
		ind = i
ans = [a[ind]] + a[0:ind] + a[ind + 1:]
print(*ans)


def decimalToBinary(n):
	return bin(n).replace('0b', '')
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
b = []
for i in range(n):
	s = decimalToBinary(a[i])
	s = '0' * (32 - len(s)) + s
	b.append(s)
ans = -1
for i in range(32):
	ind = -1
	for j in range(n):
		if b[j][i] == '1' and ind == -1:
			ind = j
			ans = j
		elif b[j][i] == '1' and ind != -1:
			ans = -1
			break
	if ans != -1:
		break
if ans != -1:
	print(a[ans], end=' ')
	for i in range(n):
		if i != ans:
			print(a[i], end=' ')
	print()
else:
	print(*a)


n = int(input())
a = [int(x) for x in input().split()]
bits = {i: [] for i in range(33)}
for i in range(n):
	cnt = 0
	x = a[i]
	while x:
		if x & 1:
			bits[cnt].append(i)
		x >>= 1
		cnt += 1
find = False
for i in range(32, -1, -1):
	if len(bits[i]) == 1:
		k = bits[i][0]
		ans = [a[k]] + a[:k] + a[k + 1:]
		print(*ans)
		find = True
		break
if not find:
	print(*a)


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))
(N,) = map(int, input().split())
A = list(map(int, input().split()))
ans = solve(A, N)
print(ans)


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))


import sys
sys.stdin.readline()
nums = list(map(int, sys.stdin.readline().split()))
bitRecords = [0] * 32
occurrences = [None] * 32
for n in nums:
	for i in range(32):
		if n & 1 << i != 0:
			bitRecords[i] += 1
			if bitRecords[i] == 1:
				occurrences[i] = n
firstNum = nums[0]
for i in reversed(range(32)):
	if bitRecords[i] == 1:
		firstNum = occurrences[i]
		break
nums.remove(firstNum)
nums.insert(0, firstNum)
print(*nums)


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())
l = list(map(int, input().split()))
try:
	ans = []
	for i in range(31, -1, -1):
		c = 0
		tem = -100
		flag = 0
		for j in l:
			if j >> i & 1:
				tem = j
				c += 1
		if c == 1:
			ans.append(tem)
			flag = 1
			break
	g = l.index(ans.pop(-1))
	an = [l[g]]
	for i in range(n):
		if i != g:
			an.append(l[i])
	print(*an)
except:
	print(*l)


n = int(input())
a = list(map(int, input().split()))
l = [set() for i in range(50)]
for (i, x) in enumerate(a):
	y = list(reversed(str(bin(x))))
	for (j, k) in enumerate(y):
		if k == '1':
			l[j].add(i)
ans = -1
for x in l:
	if len(x) == 1:
		ans = list(x)[0]
if ans == -1:
	a.sort(reverse=True)
	print(' '.join(map(str, a)))
else:
	x = a.pop(ans)
	a.sort(reverse=True)
	b = [x] + a
	print(' '.join(map(str, b)))


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)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
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')
n = int(input())
a = list(map(int, input().split()))
pre = [0] * n
suf = [0] * n
el = ~a[0]
for i in range(1, n):
	pre[i] = el & ~a[i - 1]
	el = el & ~a[i - 1]
el = ~a[-1]
for i in range(n - 2, -1, -1):
	suf[i] = el & ~a[i + 1]
	el = el & ~a[i + 1]
pre[0] = suf[0]
suf[-1] = pre[-1]
mx = 0
ind = -1
for i in range(n):
	curr = a[i] & pre[i] & suf[i]
	if curr > mx:
		mx = curr
		ind = i
t = a[ind]
a[ind] = a[0]
a[0] = t
print(*a)


n = int(input())
a = list(map(int, input().split()))
x = max(a).bit_length()
b = [[0] * x for i in range(n)]
for i in range(n):
	p = a[i]
	for j in range(x - 1, -1, -1):
		b[i][j] = p % 2
		p //= 2
cnt = [0] * x
for i in range(x):
	for j in range(n):
		cnt[i] += b[j][i]
if cnt.count(1) == 0:
	print(*a)
else:
	for i in range(x):
		if cnt[i] == 1:
			for j in range(n):
				if b[j][i] == 1:
					(a[0], a[j]) = (a[j], a[0])
					print(*a)
					exit()


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
import sys
input = sys.stdin.readline
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	l = [0 for i in range(40)]
	for i in range(n):
		u = bin(a[i])[2:]
		for k in range(len(u)):
			if u[k] == '1':
				l[len(u) - k - 1] += 1
	a.sort(reverse=True)
	maxi = -1
	for k in range(39, -1, -1):
		for i in range(n):
			if a[i] & 1 << k > 0 and l[k] == 1:
				maxi = a[i]
				break
		if maxi != -1:
			break
	if maxi == -1:
		maxi = a[0]
	a.remove(maxi)
	print(maxi, *a)


import os
from io import BytesIO, IOBase
import sys

def main():
	n = int(input())
	a = list(map(int, input().split()))
	mxl = 0
	b = []
	for i in a:
		b.append(bin(i)[2:])
		mxl = max(len(b[-1]), mxl)
	c = [0] * mxl
	for i in b:
		for j in range(len(i)):
			if i[-(j + 1)] == '1':
				c[j] += 1
	mav = 0
	maind = 0
	k = 0
	for i in b:
		val = 0
		for j in range(len(i)):
			if i[-(j + 1)] == '1':
				if c[j] == 1:
					val += 1 << j
		if val > mav:
			mav = val
			maind = k
		k += 1
	(a[maind], a[0]) = (a[0], a[maind])
	print(*a)
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()


import sys, os, io

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

def ri():
	return int(sys.stdin.readline())

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		n = ri()
		a = ria()
		nota = []
		for i in a:
			nota.append(~i)
		c = nota[0]
		prefixandnot = [c]
		for i in range(1, n):
			c = nota[i] & c
			prefixandnot.append(c)
		nota = nota[::-1]
		c = nota[0]
		suffixandnot = [c]
		for i in range(1, n):
			c = nota[i] & c
			suffixandnot.append(c)
		suffixandnot = suffixandnot[::-1]
		m = -999999999999999999999
		ind = 0
		for i in range(n):
			k = a[i]
			if i - 1 >= 0:
				k = k & prefixandnot[i - 1]
			if i + 1 < n:
				k = k & suffixandnot[i + 1]
			if k > m:
				m = k
				ind = i
		print(a[ind], *a[:ind] + a[ind + 1:])
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
M = (1 << 32) - 1
nA = [M ^ a for a in A]
ar = [M] + nA[:] + [M]
al = ar[:]
for i in range(1, N + 2):
	al[i] &= al[i - 1]
for i in range(N, -1, -1):
	ar[i] &= ar[i + 1]
d = -1
ans = None
for i in range(N):
	a = A[i]
	res = a & al[i] & ar[i + 2]
	if res > d:
		d = res
		ans = i
Ans = A[:]
Ans.remove(A[ans])
print(*[A[ans]] + Ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
left = [0]
right = [0]
for ai in a:
	left.append(left[-1] | ai)
for ai in reversed(a):
	right.append(right[-1] | ai)
maximum = 0
max_ind = 0
for (i, ai) in enumerate(a):
	mask = left[i] | right[n - 1 - i]
	tmp = (ai | mask) - mask
	if tmp > maximum:
		maximum = tmp
		max_ind = i
ans = [a[max_ind]]
for i in range(n):
	if i != max_ind:
		ans.append(a[i])
print(*ans)


n = int(input())
l = list(map(int, input().split()))
b = [0] * 32
for i in range(n):
	p = l[i]
	for j in range(32):
		b[j] += p % 2
		p = p // 2
ai = 0
m = -1
for i in range(n):
	p = l[i]
	q = 0
	for j in range(32):
		if p % 2 == 1 and b[j] == 1:
			q += 2 ** j
		p = p // 2
	if q > m:
		m = q
		ai = i
ans = [l[ai]] + l[:ai] + l[ai + 1:]
print(*ans)


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
import sys
mod = 1000000007

def invert(N):
	s = ''
	x = bin(N)[2:]
	for i in x:
		if i == '0':
			s += '1'
		else:
			s += '0'
	return s
n = int(input())
A = alele()
count = [0] * 32
for a in A:
	for i in range(32):
		if a & 1 << i != 0:
			count[i] += 1
init = 0
flag = 0
for i in range(31, -1, -1):
	if count[i] == 1:
		for j in range(n):
			if A[j] & 1 << i != 0:
				init = j
				flag = 1
				break
	if flag == 1:
		break
ANS = [A[init]]
for i in range(n):
	if i == init:
		continue
	ANS.append(A[i])
print(*ANS)


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())
ar = [int(x) for x in input().split()]
ar = sorted(ar)[::-1]
d = {k: 0 for k in range(32)}
for x in ar:
	for j in range(32):
		if x ^ 1 << j == x - (1 << j):
			d[j] += 1
maxn = 0
maxi = 0
for (i, e) in enumerate(ar):
	tmp = e
	for j in range(32):
		if e ^ 1 << j == e - (1 << j) and d[j] >= 2:
			tmp -= 1 << j
	if tmp > maxn:
		maxn = tmp
		maxi = i
print(ar[maxi], end=' ')
for i in range(len(ar)):
	if i != maxi:
		print(ar[i], end=' ')


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 math
import collections
import sys

def inpu():
	return input().split(' ')

def inti(a):
	for i in range(len(a)):
		a[i] = int(a[i])
	return a

def inp():
	a = inpu()
	a = inti(a)
	return a

def solution():
	N = inp()
	A = inp()
	ans = -1
	for i in range(32, -1, -1):
		count = 0
		for n in A:
			if 1 << i & n != 0:
				ans = n
				count += 1
		if count == 1:
			break
		ans = -1
	if ans != -1:
		print(ans, end=' ')
	for n in A:
		if n != ans:
			print(n, end=' ')
solution()


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)


import sys, math
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

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

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def solve():
	N = getInt()
	A = getInts()
	if N == 1:
		print(*A)
		return
	pref = [~A[0]]
	suff = [~A[N - 1]]
	for n in range(1, N):
		pref.append(pref[-1] & ~A[n])
		suff.append(suff[-1] & ~A[N - n - 1])
	suff.reverse()
	best = -10 ** 9
	ind = -1
	for n in range(N):
		if n == 0:
			tmp = A[n] & suff[n + 1]
		elif n == N - 1:
			tmp = A[n] & pref[n - 1]
		else:
			tmp = A[n] & pref[n - 1] & suff[n + 1]
		if tmp > best:
			ind = n
			best = tmp
	if ind == -1:
		print(*A)
	else:
		print(A[ind], *[A[j] for j in range(N) if j != ind])
	return
solve()


import math

def countBits(number):
	if number == 0:
		return 0
	return int(math.log(number) / math.log(2) + 1)

def complement(n, bits):
	return (1 << bits) - 1 ^ n
n = int(input())
a = list(map(int, input().split()))
bit = countBits(max(a))
pre = [0 for i in range(n)]
suf = [0 for i in range(n + 1)]
suf[n] = complement(0, bit)
for i in range(n):
	if i == 0:
		pre[i] = complement(a[i], bit)
	else:
		pre[i] = pre[i - 1] & complement(a[i], bit)
pre[-1] = complement(0, bit)
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suf[i] = complement(a[i], bit)
	else:
		suf[i] = suf[i + 1] & complement(a[i], bit)
max = -1
for i in range(n):
	if a[i] & pre[i - 1] & suf[i + 1] > max:
		max = a[i] & pre[i - 1] & suf[i + 1]
		p = i
print(a[p], end=' ')
for i in range(n):
	if i != p:
		print(a[i], end=' ')


def main():
	n = int(input())
	aa = [int(a) for a in input().split()]
	counts = [0] * 30
	candidate = [None] * 30
	for a in aa:
		i = 0
		b = a
		while b > 0:
			if b % 2 == 1:
				counts[i] += 1
				if counts[i] == 1:
					candidate[i] = a
				else:
					candidate[i] = None
			b = b // 2
			i += 1
	for c in candidate[::-1]:
		if c:
			aa.remove(c)
			print(c, ' '.join(map(str, aa)))
			break
	else:
		print(' '.join(map(str, aa)))
main()


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)


from sys import stdin, stdout

def main():
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	pow2 = [0] * 32
	max = 0
	ans = [0] * 32
	for (j, i) in enumerate(arr):
		x = int(i)
		z = 0
		while x > 0:
			if (x - pow(2, z)) % pow(2, z + 1) == 0:
				x -= pow(2, z)
				pow2[z] += 1
				if pow2[z] == 1:
					ans[z] = j
			z += 1
	for (i, x) in enumerate(pow2):
		if x == 1:
			max = i
	if pow2[max] == 1:
		(arr[0], arr[ans[max]]) = (arr[ans[max]], arr[0])
	stdout.write(' '.join((str(x) for x in arr)))
main()


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
bitc = [0] * 32
for v in a:
	i = 0
	while v:
		if v & 1:
			bitc[i] += 1
		i += 1
		v >>= 1
bits = 0
for i in range(32):
	if bitc[i] == 1:
		bits = i
st = 0
for i in range(n):
	if 1 << bits & a[i]:
		st = i
		break
s = a[st]
a = a[:st] + a[st + 1:]
print(s, *a)


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


n = int(input())
arr = [[] for i in range(32)]
l = input().split()
li = [int(i) for i in l]
for i in li:
	for j in range(32):
		if i & 1 << j:
			arr[j].append(i)
maxa = -1
for i in range(31, -1, -1):
	if len(arr[i]) == 1:
		maxa = arr[i][0]
		break
if maxa == -1:
	for i in li:
		print(i, end=' ')
else:
	done = 0
	print(maxa, end=' ')
	for i in li:
		if i == maxa and done == 0:
			done = 1
			continue
		print(i, end=' ')
	print()


from sys import stdin, stdout
n = int(stdin.readline().strip())
alist = list(map(int, stdin.readline().split()))
cnt = {}
for a in alist:
	k = 1
	while k <= a:
		if a & k:
			cnt.setdefault(k, []).append(a)
		k *= 2
mx = None
for (k, v) in sorted(list(cnt.items()), reverse=True):
	if len(v) == 1:
		mx = v[0]
		break
if mx:
	alist.remove(mx)
	alist.insert(0, mx)
stdout.write(' '.join([str(a) for a in alist]))


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)))


def calc(s):
	b = bin(s)[2:]
	b = '0' * (x - len(b)) + b
	c = ''
	for i in b:
		if i == '1':
			c += '0'
		else:
			c += '1'
	return int(c, 2)
n = int(input())
a = list(map(int, input().split()))
x = len(bin(max(a))[2:])
if n == 1:
	print(a[0])
	exit()
p = [calc(a[0])]
s = [0] * n
s[-1] = calc(a[-1])
for i in range(1, n):
	p.append(p[-1] & calc(a[i]))
for i in range(n - 2, -1, -1):
	s[i] = s[i + 1] & calc(a[i])
maxx = a[0] & s[1]
ind = 0
for i in range(1, n - 1):
	if p[i - 1] & a[i] & s[i + 1] > maxx:
		maxx = p[i - 1] & a[i] & s[i + 1]
		ind = i
if p[-2] & a[-1] > maxx:
	maxx = p[-2] & a[-1]
	ind = n - 1
print(a[ind], *a[0:ind] + a[ind + 1:])


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=' ')


n = int(input())
a = list(map(int, input().split()))
bits = [0] * 65
for i in a:
	s = bin(i)[2:]
	s = s[::-1]
	for i1 in range(len(s)):
		if s[i1] == '1':
			bits[i1] += 1
ans = [-1, -1]
for i2 in range(len(a)):
	i = a[i2]
	unique = 0
	s = bin(i)[2:]
	s = s[::-1]
	summ = 0
	sd = 1
	for i1 in range(len(s)):
		if s[i1] == '1' and bits[i1] == 1:
			unique += 1
			summ += sd
		sd *= 2
	if summ >= ans[0]:
		ans = [summ, i2]
print(a[ans[1]], end=' ')
for i in range(len(a)):
	if i != ans[1]:
		print(a[i], end=' ')