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

Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement — he only changed one word.

We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains two space-separated integers $L$, $R$.

------  Output ------
For each test case, print a single line containing one integer — the maximum bitwise OR.

------  Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ L ≤ R ≤ 10^{18}$

----- Sample Input 1 ------ 
4
1 3
4 5
1031 93714
1000000000123 1000000123456
----- Sample Output 1 ------ 
3
5
131071
1000000192511

Difficulty: MEDIUM_HARD
Tags: Bit manipulation

Greedy algorithms
Skill types: Bit manipulation

Greedy algorithms

Starter code:

Solutions:
t = int(input())
for i in range(t):
	(m, n) = map(int, input().split())
	(b1, b2) = (bin(m)[2:], bin(n)[2:])
	if len(b1) != len(b2):
		m = max(len(b1), len(b2))
		print(int('1' * m, 2))
	else:
		c = 0
		while c < len(b1) and b1[c] == b2[c]:
			c += 1
		ans = b1[:c] + '1' * (len(b1) - c)
		print(int(ans, 2))


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	(bm, bn) = (bin(m)[2:], bin(n)[2:])
	if len(bm) != len(bn):
		m = max(len(bm), len(bn))
		print(int('1' * m, 2))
	else:
		i = 0
		while i < len(bm) and bm[i] == bn[i]:
			i += 1
		ans = bm[:i] + '1' * (len(bm) - i)
		print(int(ans, 2))


T = int(input())
for i in range(T):
	(n, m) = map(int, input().split())
	(r, s) = (bin(n), bin(m))
	if len(r) != len(s):
		k = max(len(r), len(s)) - 2
		h = '1' * k
		print(int(h, 2))
	else:
		i = 0
		while i < len(r) and r[i] == s[i]:
			i += 1
		ans = r[:i] + '1' * (len(r) - i)
		print(int(ans[2:], 2))


try:
	t = int(input())
	for i in range(t):
		(n, m) = map(int, input().split())
		(r, s) = (bin(n), bin(m))
		if len(r) != len(s):
			k = max(len(r), len(s)) - 2
			h = '1' * k
			print(int(h, 2))
		else:
			i = 0
			while i < len(r) and r[i] == s[i]:
				i += 1
			ans = r[:i] + '1' * (len(r) - i)
			print(int(ans[2:], 2))
except:
	pass


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	mi = str(bin(x)[2:])
	ma = str(bin(y)[2:])
	mi = '0' * (len(ma) - len(mi)) + mi
	for i in range(len(ma)):
		if mi[i] != ma[i]:
			break
	print(eval('0b' + ma[:i + 1] + '1' * (len(ma) - (i + 1))))


T = int(input())
ans = []
for _ in range(T):
	(L, R) = [int(i) for i in input().split()]
	l = bin(L)[2:]
	r = bin(R)[2:]
	if len(l) != len(r):
		ans.append(int(max(len(l), len(r)) * '1', 2))
	else:
		i = 0
		while i < len(r) and l[i] == r[i]:
			i += 1
		s = l[:i] + '1' * (len(l) - i)
		ans.append(int(s, 2))
for i in ans:
	print(i)


from math import log2
for T in range(int(input())):
	(l, r) = map(int, input().split())
	if r == 0:
		print(0)
		continue
	lbit_pos = []
	rbit_pos = []
	for i in range(int(log2(r)), -1, -1):
		if l >> i & 1:
			lbit_pos.append(i)
		if r >> i & 1:
			rbit_pos.append(i)
	index = min(len(lbit_pos), len(rbit_pos))
	flag = True
	value = 0
	for i in range(index):
		if lbit_pos[i] != rbit_pos[i]:
			value += (1 << rbit_pos[i] + 1) - 1
			flag = False
			break
		else:
			value += 1 << rbit_pos[i]
	if flag == True:
		if index != len(rbit_pos):
			value += (1 << rbit_pos[i + 1] + 1) - 1
	print(value)


for T in range(int(input())):
	(l, r) = map(int, input().split())
	lbit_pos = []
	rbit_pos = []
	for i in range(100, -1, -1):
		if l >> i & 1:
			lbit_pos.append(i)
		if r >> i & 1:
			rbit_pos.append(i)
	index = min(len(lbit_pos), len(rbit_pos))
	flag = True
	value = 0
	for i in range(index):
		if lbit_pos[i] != rbit_pos[i]:
			value += (1 << rbit_pos[i] + 1) - 1
			flag = False
			break
		else:
			value += 1 << rbit_pos[i]
	if flag == True:
		if index != len(rbit_pos):
			value += (1 << rbit_pos[i + 1] + 1) - 1
	print(value)


test = int(input())
for _ in range(test):
	(l, r) = map(int, input().split())
	xor1 = l ^ r
	max1 = 0
	while xor1:
		max1 = max1 << 1
		max1 = max1 | 1
		xor1 = xor1 >> 1
	max1 = r | max1
	print(max(max1, r))


def max_bitwise_or(L, R):
	v1 = []
	v2 = []
	v3 = []
	z = 0
	i = 0
	ans = 0
	cnt = 1
	while L > 0:
		v1.append(L % 2)
		L = L // 2
	while R > 0:
		v2.append(R % 2)
		R = R // 2
	while len(v1) != len(v2):
		v1.append(0)
	for i in range(len(v2) - 1, -1, -1):
		if v2[i] == 1 and v1[i] == 0 and (z == 0):
			z = 1
			continue
		if z == 1:
			v1[i] = 1
	for i in range(len(v2)):
		v3.append(v2[i] | v1[i])
	for i in range(len(v2)):
		if v3[i] == 1:
			ans += cnt
		cnt *= 2
	return ans
try:
	t = int(input())
	for _ in range(t):
		(L, R) = map(int, input().rstrip().split())
		print(max_bitwise_or(L, R))
except:
	pass


t = int(input())
for you in range(t):
	l = input().split()
	L = int(l[0])
	R = int(l[1])
	if L == R:
		print(L)
	else:
		num = 0
		for i in range(63, -1, -1):
			if L & 1 << i == 0 and R & 1 << i > 0:
				num = num + (2 * (1 << i) - 1)
				break
			else:
				num = num + (L & 1 << i)
		print(num)


import math
t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	x = int(math.log(r, 2))
	count = 0
	while x >= 0:
		k = 2 ** x
		if l < k:
			print(count + 2 * k - 1)
			break
		else:
			l -= k
			r -= k
			count += k
			if r == 0:
				print(count)
				break
			else:
				x = int(math.log(r, 2))


MAX = 64

def maxOR(L, R):
	if L == R:
		return L
	ans = 0
	for i in range(MAX - 1, -1, -1):
		p = 1 << i
		lbit = L >> i & 1
		rbit = R >> i & 1
		if rbit == 1 and lbit == 0:
			ans += (p << 1) - 1
			break
		if rbit == 1:
			ans += p
	return ans
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(maxOR(l, r))


t = int(input())
while t:
	t -= 1
	(l, r) = [int(i) for i in input().split()]
	bl = bin(l)[2:]
	br = bin(r)[2:]
	ans = ''
	if l == r:
		print(r)
	elif len(bl) < len(br):
		for i in range(len(br)):
			ans += '1'
		print(int('0b' + ans, 2))
	else:
		for i in range(len(br)):
			if br[i] == '1' and bl[i] == '0':
				temp = i
				break
		for i in range(temp):
			ans += br[i]
		for i in range(temp, len(br)):
			ans += '1'
		print(int('0b' + ans, 2))


import math as mt

def check(l, r, power):
	bal = 0
	for i in range(power, -1, -1):
		new_l = l >> i
		new_r = r >> i
		bitOfL = new_l & 1
		bitOfR = new_r & 1
		if bitOfL & bitOfR:
			if r - (1 << i) >= l:
				r = r ^ 1 << i
				bal += int(pow(2, i))
	m = [bal, r]
	return m

def maximise(l, r, bal, power, mask):
	for i in range(power, -1, -1):
		new_l = l >> i
		new_r = r >> i
		bitOfL = new_l & 1
		bitOfR = new_r & 1
		if bitOfL | bitOfR:
			mask = mask | 1 << i
		elif 1 << i <= bal:
			bal = bal - (1 << i)
			mask = mask | 1 << i
		elif l + (1 << i) <= r:
			l += 1 << i
			mask = mask | 1 << i
		else:
			continue
	return mask
t = int(input())
while t:
	(l, r) = map(int, input().split())
	power = mt.floor(mt.log(r) / mt.log(2))
	checker = check(l, r, power)
	r = checker[1]
	bal = checker[0]
	print(maximise(l, r, bal, power, 0))
	t -= 1


t = int(input())
while t > 0:
	(n, m) = map(int, input().split())
	a = bin(n)
	a = a[2:]
	c = bin(m)
	c = c[2:]
	minlen = min(len(a), len(c))
	if len(a) > len(c):
		maxdiff = len(a) - minlen
		c = '0' * maxdiff + c
	else:
		maxdiff = len(c) - minlen
		a = '0' * maxdiff + a
	res = ''
	for i in range(len(a)):
		if a[i] == '0' and c[i] == '1':
			res = res + (len(a) - i) * '1'
			break
		else:
			res = res + a[i]
	p = int(res, 2)
	print(p)
	t -= 1


def max_bitwise_or(L, R):
	v1 = []
	v2 = []
	v3 = []
	z = 0
	i = 0
	ans = 0
	cnt = 1
	while L > 0:
		v1.append(L % 2)
		L = L // 2
	while R > 0:
		v2.append(R % 2)
		R = R // 2
	while len(v1) != len(v2):
		v1.append(0)
	for i in range(len(v2) - 1, -1, -1):
		if v2[i] == 1 and v1[i] == 0 and (z == 0):
			z = 1
			continue
		if z == 1:
			v1[i] = 1
	for i in range(len(v2)):
		v3.append(v2[i] | v1[i])
	for i in range(len(v2)):
		if v3[i] == 1:
			ans += cnt
		cnt *= 2
	return ans
for _ in range(int(input())):
	a = list(map(int, input().split()))
	print(max_bitwise_or(a[0], a[1]))


def bi(X, l):
	return (l - len(bin(X)[2:])) * '0' + bin(X)[2:]
for _ in range(int(input())):
	(L, R) = list(map(int, input().split()))
	q = bin(R)[2:]
	length = len(q)
	p = bi(L, length)
	changed = False
	ans = ''
	for i in range(length):
		if p[i] != q[i]:
			ans += (length - i) * '1'
			break
		else:
			ans += p[i]
	print(int(ans, 2))


def ans(a, b):
	if a == b:
		return a | b
	a1 = str(bin(a)[2:])
	b1 = str(bin(b)[2:])
	if len(a1) > len(b1):
		b1 = '0' * (len(a1) - len(b1)) + b1
	elif len(b1) > len(a1):
		a1 = '0' * (len(b1) - len(a1)) + a1
	i = 0
	s = 0
	while a1[i] == b1[i]:
		s += int(a1[i]) * 2 ** (len(a1) - i - 1)
		i += 1
	for j in range(i, len(a1)):
		if a1[j] == '0' and b1[j] == '0':
			if a + 2 ** (len(a1) - j - 1) <= b:
				s = s + 2 ** (len(a1) - j - 1)
				a = a + 2 ** (len(a1) - j - 1)
		else:
			s = s + 2 ** (len(a1) - j - 1)
	return s
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(ans(a, b))


import sys
from math import ceil, floor, tan
import bisect
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
rw = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	(l, r) = RI()
	ans = 0
	flag = False
	for i in range(60, -1, -1):
		if l >> i & 1 != r >> i & 1:
			flag = True
		if flag:
			ans = ans | 1 << i
		elif l >> i & 1 == 1:
			ans = ans | 1 << i
	print(ans)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	x = l ^ r
	m = 1
	while x:
		x >>= 1
		m <<= 1
	print(r | m - 1)


tests = int(input())
while tests:
	tests -= 1
	(l, r) = map(int, input().split())
	(l, r) = (bin(l)[2:], bin(r)[2:])
	m = max(len(l), len(r))
	l = l.zfill(m)
	ans = ''
	for (i, j) in zip(l, r):
		if i == j:
			ans += i
		else:
			ans += '1'
			break
	for i in range(m - len(ans)):
		ans += '1'
	print(int(ans, 2))


try:
	num = int(input())
	while num > 0:
		(l, r) = map(int, input().split())
		r = bin(r)
		l = bin(l)
		size = len(r)
		count = 0
		i = 2
		if len(r) != len(l):
			r = list(r)
			while i < size:
				r[i] = '1'
				i += 1
			r = ''.join(r)
		else:
			while i < size:
				if r[i] == l[i]:
					count += 1
				else:
					break
				i += 1
			index = 2 + count
			r = list(r)
			while index < size:
				r[index] = '1'
				index += 1
			r = ''.join(r)
		print(int(r, 2))
		num -= 1
except:
	pass


import math

def fun(l, r):
	(bitl, bitr) = (1, 1)
	if (l == 0 or l == 1) and (r == 0 or r == 1):
		return l | r
	if l == r:
		return l
	if l != 0:
		bitl = int(math.log2(l)) + 1
	if r != 0:
		bitr = int(math.log2(r)) + 1
	ans = 0
	if bitl != bitr:
		ans = 2 ** bitr - 1
	else:
		v = 2 ** (bitr - 1)
		ans = 2 ** (bitr - 1) + fun(l % v, r % v)
	return ans
for _ in range(int(input())):
	(l, r) = list(map(int, input().split()))
	print(fun(l, r))


import math
for _ in range(int(input())):
	(l, r) = list(map(int, input().split()))
	bitl = int(math.log2(l)) + 1
	bitr = int(math.log2(r)) + 1
	if bitr != bitl:
		ans = 2 ** bitr - 1
		print(ans)
	else:
		ans = 0
		flag = False
		tmp = 2 ** (bitr - 1)
		for i in range(bitr - 1, -1, -1):
			if l & tmp != r & tmp or flag:
				ans += tmp
				flag = True
			elif l & tmp:
				ans += tmp
			tmp //= 2
		print(ans)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	(first, second) = (bin(l)[2:], bin(r)[2:])
	if len(first) == len(second):
		cur = len(first)
		for i in range(len(first)):
			if first[i] == second[i]:
				cur -= 1
			else:
				break
		print(r | (1 << cur) - 1)
	else:
		print((1 << len(second)) - 1)


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	ans = ''
	x = f'{l:64b}'
	y = f'{r:64b}'
	pos = 64
	for i in range(64):
		if x[i] != y[i]:
			pos = i
			break
		else:
			ans += x[i]
	for j in range(pos, 64):
		ans += '1'
	print(int(ans, 2))


t = int(input())
for _ in range(t):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	p = bin(a).replace('0b', '')
	q = bin(b).replace('0b', '')
	if len(p) < len(q):
		q = q.replace('0', '1')
		print(int(q, 2))
	else:
		temp = 0
		for i in range(1, len(q) + 1):
			if q[0:i] != p[0:i]:
				temp = i - 1
				break
		if temp != 0:
			q = q[0:temp] + q[temp:].replace('0', '1')
		print(int(q, 2))


from numpy import binary_repr
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	l_bin = binary_repr(l, 64)
	r_bin = binary_repr(r, 64)
	i = -1
	if l == r:
		print(l)
	else:
		while True:
			i += 1
			if l_bin[i] == r_bin[i]:
				continue
			else:
				break
		print(int(r_bin[0:i] + '1' * (64 - i), 2))


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	a = str(bin(l).replace('0b', ''))
	b = str(bin(r).replace('0b', ''))
	k = ''
	q = 0
	if len(a) == len(b):
		for i in range(len(a)):
			if a[i] == '1' and b[i] == '1':
				k += '1'
			elif a[i] == '0' and b[i] == '1':
				k += '1'
				q = 1
			elif a[i] == '1' and b[i] == '0':
				k += '1'
			elif a[i] == '0' and b[i] == '0':
				if q == 1:
					k += '1'
				else:
					k += '0'
	else:
		k = '1' * len(b)
	print(int(k, 2))


from sys import stdin
t = int(stdin.readline())
while t:
	(l, r) = map(int, stdin.readline().split())
	(bl, br) = (str(bin(l))[2:], str(bin(r))[2:])
	if len(bl) == len(br):
		res = ['1'] * len(br)
		for i in range(len(br)):
			if br[i] == bl[i]:
				res[i] = br[i]
			else:
				break
		res = ''.join(res)
	else:
		res = '1' * len(br)
	print(int(res, 2))
	t -= 1


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	L = str(bin(l))[2:]
	R = str(bin(r))[2:]
	if len(L) == len(R):
		res = ['1'] * len(R)
		for i in range(len(R)):
			if R[i] == L[i]:
				res[i] = R[i]
			else:
				break
		res = ''.join(res)
	else:
		res = '1' * len(R)
	num = int(res, 2)
	print(num)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	(a, b) = (bin(l), bin(r))
	(a, b) = (a[2:], b[2:])
	if len(b) > len(a):
		x = len(b) - len(a)
		a = '0' * x + a
	ans = ''
	for i in range(len(a)):
		if a[i] == b[i]:
			ans += a[i]
		elif a[i] == '0' and b[i] == '1':
			ans += '1'
			break
	y = len(ans)
	if y < len(b):
		z = len(a) - y
		ans += '1' * z
	print(int(ans, 2))


for _ in range(int(input())):
	(l, r) = [int(x) for x in input().split()]
	c = 1
	while l != r:
		l = l >> 1
		r = r >> 1
		c = c << 1
	l = l * c
	c = c - 1
	l = l | c
	print(l)


import math
t = int(input())
while t > 0:
	(l, r) = input().split()
	l = int(l)
	r = int(r)
	if l == r:
		print(l)
	else:
		a = 0
		b = 0
		length = int(math.log(r, 2)) + 1
		for i in range(length - 1, -1, -1):
			n = 1 << i
			if l & n != 0 and r & n != 0:
				a = a | n
				b = b | n
			elif l & n == 0 and r & n == 0:
				continue
			else:
				break
		if i >= 0:
			b = b | n
			i -= 1
		while i >= 0:
			n = 1 << i
			a = a | n
			i -= 1
		print(a | b)
	t -= 1


for _ in range(int(input())):
	(L, R) = map(int, input().split())
	maxl = 1
	while maxl << 1 <= R:
		maxl = maxl << 1
	if maxl - 1 >= L:
		print(maxl - 1 | R)
	else:
		while L | L + 1 <= R:
			L |= L + 1
		print(L | R)


def countTotalBits(num):
	binary = bin(num)[2:]
	return (len(binary), str(binary))

def comparision(string_l, string_r):
	l = len(string_l)
	c = 0
	for k in range(l):
		if string_l[k] == string_r[k]:
			c += 1
		else:
			break
	return c
number_of_test_cases = int(input())
for t in range(number_of_test_cases):
	(l, r) = input().split()
	(l, r) = [int(l), int(r)]
	(min_length, string_l) = countTotalBits(l)
	(max_length, string_r) = countTotalBits(r)
	if max_length - min_length > 0:
		temp = int('1' * (max_length - 1), 2)
		print(temp | r)
	else:
		temp = comparision(string_l, string_r)
		answer = string_l[:temp] + '1' * (max_length - temp)
		print(int(answer, 2))


t = int(input())
for _ in range(t):
	(L, R) = map(int, input().split())
	bl = bin(L)[2:]
	br = bin(R)[2:]
	if len(bl) < len(br):
		nl = '1' * len(br)
		print(int(nl, 2))
	else:
		com_end = 0
		while com_end < len(bl):
			if bl[com_end] != br[com_end]:
				break
			com_end = com_end + 1
		t = '1' * (len(bl) - com_end)
		ans = br[:com_end] + t
		print(int(ans, 2))


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	binary = bin(r)
	binary = binary[2:]
	binary = list(binary)
	ans = r
	for i in range(len(binary)):
		if binary[i] == '0':
			num = ['1'] * (len(binary) - i)
			num = int(''.join(num), 2)
			if num < r and num >= l:
				ans = r | num
				break
			else:
				alter = bin(l)
				alter = alter[2:]
				alter = list(alter)
				for j in range(len(alter) - 1, -1, -1):
					if alter[j] == '0':
						x = alter[:]
						x[j] == '1'
						x = int(''.join(x), 2)
						if x >= r:
							break
						else:
							alter[j] = '1'
				alter = ''.join(alter)
				alter = int(alter, 2)
				ans = r | alter
				break
	print(ans)


for p in range(int(input())):
	(l, r) = map(int, input().split())
	bl = bin(l)[2:]
	br = bin(r)[2:]
	br = str(br)
	bl = str(bl)
	bl = '0' * (len(br) - len(bl)) + str(bl)
	a = ''
	b = ''
	for i in range(len(bl)):
		if bl[i] == '0' and br[i] == '1' and (i < len(bl) - 1):
			a += '01'
			b += '10'
			break
		else:
			a += bl[i]
			b += br[i]
	a += '1' * (len(br) - len(a))
	b += '1' * (len(br) - len(b))
	print(int(a, 2) | int(b, 2))


t = int(input())
for _ in range(t):
	(L, R) = map(int, input().split())
	first = '{0:b}'.format(R)
	second = '{0:b}'.format(L)
	first = '0' * (64 - len(first)) + first
	second = '0' * (64 - len(second)) + second
	i = 0
	while i < 64:
		if first[i] == '1' and second[i] == '0':
			break
		i += 1
	ans = 0
	for j in range(i):
		if first[j] == '1':
			ans += 2 ** (63 - j)
	for k in range(i, 64):
		ans += 2 ** (63 - k)
	print(ans)


try:
	for _ in range(int(input())):
		(l, r) = [int(a) for a in input().split()]
		lb = bin(l)[2:]
		rb = bin(r)[2:]
		n = len(lb)
		m = len(rb)
		x = ['0'] * (m - n) + list(lb)
		y = list(rb)
		i = 0
		while i < m:
			if x[i] != y[i]:
				break
			i += 1
		x = x[:i] + ['1'] * (m - i)
		x = ''.join(x)
		print(int(x, 2) | r)
except:
	pass


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	(lb, rb) = (bin(l)[2:], bin(r)[2:])
	(ll, rl) = (len(lb), len(rb))
	if l == 0 and r == 0:
		print(0)
		continue
	if l == r:
		print(l)
		continue
	ans = ''
	if ll == rl:
		i = 0
		for i in range(ll):
			if lb[i] != rb[i]:
				break
			else:
				ans += lb[i]
		ans += '1' * (ll - i)
	else:
		ans += '1' * rl
	print(int(ans, 2))


for ii in range(int(input())):
	(l, r) = map(int, input().split())
	lb = bin(l)[2:]
	rb = bin(r)[2:]
	lb = '0' * (len(rb) - len(lb)) + lb
	s = ''
	flag = 0
	for i in range(len(rb)):
		if flag or lb[i] != rb[i]:
			flag = 1
			s += '1'
		else:
			s += rb[i]
	x = 0
	ss = s[::-1]
	p = 1
	for i in ss:
		x += int(i) * p
		p = p * 2
	print(x)


import math
t = int(input())
while t > 0:
	t -= 1
	(a, b) = map(int, input().split())
	c = bin(a)
	d = bin(b)
	if len(c) != len(d):
		for i in range(0, len(d) - len(c)):
			c = c[0:2] + '0' + c[2:]
	i = 0
	while i < len(d) and c[i] == d[i]:
		i += 1
	c = c[0:i]
	for j in range(i, len(d)):
		c += '1'
	print(int(c, 2))


from sys import stdin
t = int(stdin.readline())
while t:
	(l, r) = [int(x) for x in stdin.readline().split()]
	if l == r:
		print(r)
	else:
		bin_l = f'{l:064b}'
		bin_r = f'{r:064b}'
		i = 0
		max_val = ''
		while bin_l[i] == bin_r[i]:
			max_val += bin_l[i]
			i += 1
		temp = 64 - i
		for i in range(temp):
			max_val += '1'
		print(int(max_val, 2))
	t -= 1


def till_what_length(str1, str2):
	count = 0
	for i in range(len(str1)):
		if str1[i] != str2[i]:
			break
		count += 1
	return count

def get_bin(x, l):
	return format(x, 'b').zfill(l)
test = int(input())
for _ in range(test):
	(l, r) = input().split()
	l = int(l)
	r = int(r)
	n = len(bin(r)[2:])
	l1 = get_bin(l, n)
	r1 = get_bin(r, n)
	counter = till_what_length(l1, r1)
	final_bin = l1[0:counter] + '1' * (n - counter)
	print(int(final_bin, 2))


a = int(input())
for i in range(a):
	(t, r) = map(int, input().split())
	s1 = bin(t)
	k1 = s1[2:]
	s2 = bin(r)
	k2 = s2[2:]
	final = ''
	fin = ''
	if len(k1) != len(k2):
		print(2 ** len(k2) - 1)
	else:
		for i in range(len(k1)):
			if k1[i] != k2[i]:
				for j in range(i + 1, len(k1)):
					final = final + '1'
				break
		fin = k1[0:i + 1] + final
		print(r | int(fin, 2))


def binaryToDecimal(n):
	return int(n, 2)

def decimalToBinary(n):
	return bin(n).replace('0b', '')
t = int(input())
for i in range(t):
	(l, r) = input().split()
	l = int(l)
	r = int(r)
	str1 = decimalToBinary(l)
	str2 = decimalToBinary(r)
	lendiff = len(str2) - len(str1)
	temp = '0' * lendiff
	newstr = temp + str1[:]
	cnt = 0
	for j in range(len(str2)):
		if str2[j] == newstr[j]:
			cnt += 1
		else:
			break
	ans = str2[:cnt] + '1' * (len(str2) - cnt)
	print(binaryToDecimal(ans))


import math

def invert_bits(num):
	x = math.floor(math.log(num, 2) + 1)
	for i in range(0, x):
		num = num ^ 1 << i
	return num

def turn_on(l, r):
	bit_l = list(bin(l)[2:])
	bit_r = list(bin(r)[2:])
	bit_l = ['0'] * (len(bit_r) - len(bit_l)) + bit_l
	for i in range(len(bit_l)):
		if bit_r[i] == '0':
			bit = False
			if bit_l[i] == '0':
				bit = True
			bit_l[i] = '1'
			ans = int(''.join(bit_l), 2)
			if ans >= l and ans <= r:
				continue
			if bit:
				bit_l[i] = '0'
	return int(''.join(bit_r), 2) | int(''.join(bit_l), 2)
t = int(input())
while t > 0:
	t -= 1
	(l, r) = map(int, input().split())
	print(turn_on(l, r))


def IntegerCreator(x2, start, end):
	for i in range(start, end):
		if x2[i] == '0':
			x2[i] = '1'
	return x2

def preComputation():
	arr = [1]
	for i in range(1, 64):
		arr.append(arr[-1] * 2)
	return arr
t = int(input())
arr = preComputation()
while t:
	t -= 1
	(l, r) = map(int, input().split())
	x1 = bin(l)[2:]
	x2 = bin(r)[2:]
	l1 = len(x1)
	l2 = len(x2)
	for i in range(l2 - l1):
		x1 = '0' + x1
	x1 = list(x1)
	x2 = list(x2)
	for i in range(l2):
		if x2[i] == '1':
			if x1[i] == '1':
				pass
			else:
				x2 = IntegerCreator(x2, i, l2)
				break
	x2 = ''.join(x2)
	ans = 0
	for i in range(l2):
		if x2[l2 - i - 1] == '1':
			ans += arr[i]
	print(ans)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	sl = bin(l)[2:]
	sr = bin(r)[2:]
	(nl, nr) = (len(sl), len(sr))
	sl = '0' * (nr - nl) + sl
	idx = 0
	for i in range(nr):
		if sl[i] != sr[i]:
			break
	idx = i + 1
	sl = sl[:idx] + '1' * (nr - idx)
	print(int(sl, 2) | r)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	x = bin(a)[2:]
	y = bin(b)[2:]
	n = len(x)
	m = len(y)
	x = ['0'] * (m - n) + list(x)
	y = list(y)
	i = 0
	while i < m:
		if x[i] != y[i]:
			break
		i += 1
	x = x[:i] + ['1'] * (m - i)
	x = ''.join(x)
	print(int(x, 2) | b)


import math
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	lol = int(math.log(r, 2))
	z = pow(2, lol) - 1
	if pow(2, lol) - 1 >= l:
		print(z | z + 1)
	else:
		lol = bin(l)[2:]
		i = -1
		while int(lol, base=2) < r:
			lol = list(lol)
			lol[i] = '1'
			lol = ''.join(lol)
			i -= 1
		print(int(int(lol, base=2)) | r)


t = int(input())
for f in range(t):
	(L, R) = map(int, input().split())
	ls = bin(L)
	rs = bin(R)
	l = ls[2:]
	r = rs[2:]
	for i in range(len(rs) - len(ls)):
		l = '0' + l
	s = len(r)
	for i in range(s):
		if l[i] != r[i]:
			r = r[:i + 1]
			for j in range(i + 1, s):
				r += '1'
			break
	r = '0b' + r
	print(int(r, 2))


for _ in range(int(input())):
	(L, R) = map(int, input().split())
	R1 = R
	(L, R) = (bin(L)[2:], bin(R)[2:])
	(x, y) = (len(L), len(R))
	L = ['0'] * (y - x) + list(L)
	R = list(R)
	i = 0
	while i < y:
		if L[i] != R[i]:
			break
		i += 1
	L = L[:i] + ['1'] * (y - i)
	L = ''.join(L)
	print(int(L, 2) | R1)


t = int(input())
for x0 in range(t):
	(l, r) = input().split()
	l = int(l)
	r = int(r)
	strs1 = '{0:b}'.format(max(l, r))
	strs2 = '{0:b}'.format(min(l, r))
	if len(strs1) != len(strs2):
		ans = 2 ** max(len(strs1), len(strs2)) - 1
	else:
		i = 0
		fin = ''
		while i < len(strs1):
			if strs1[i] == strs2[i]:
				fin += strs1[i]
			else:
				while i < len(strs1):
					fin += '1'
					i += 1
			i += 1
		ans = int(fin, 2)
	print(ans)


for _ in range(int(input())):
	(a, b) = [int(j) for j in input().split()]
	(s1, s2) = (bin(a)[2:], bin(b)[2:])
	l1 = s1
	l2 = s2
	if len(l1) != len(l2):
		ans = '1' * len(l2)
	else:
		ans = ''
		for i in range(len(l1)):
			if l1[i] == l2[i]:
				ans = ans + l1[i]
			else:
				break
		ans = ans + (len(l1) - len(ans)) * '1'
	print(int(ans, 2))


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	x = list(bin(x)[2:])
	y = list(bin(y)[2:])
	x = list('0' * (len(y) - len(x))) + x
	for i in range(len(y)):
		if y[i] == '1' and x[i] == '0':
			for j in range(i + 1, len(y)):
				y[j] = '1'
			break
	y = int(''.join(y), 2)
	print(y)


def solve(l, r):
	r = bin(r)[2:]
	l = bin(l)[2:]
	l = '0' * (len(r) - len(l)) + l
	(res, beg) = ('', True)
	for (u, d) in zip(r, l):
		if u == d and beg:
			res += u
		elif beg:
			beg = False
			res += u
		else:
			res += '1'
	return int(res, 2)
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(solve(l, r))


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	xr = l ^ r
	msb = 1
	while xr:
		xr = xr >> 1
		msb = msb << 1
	print(r | msb - 1)


T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	if N == M:
		print(N)
		continue
	a = 0
	b = 0
	x = N
	y = M
	s = 0
	t = 0
	while N > 0:
		a = a * 10 + N % 2
		N = N // 2
		s += 1
	while M > 0:
		b = b * 10 + M % 2
		M = M // 2
		t += 1
	N = x
	M = y
	if s == t:
		p = s
		p = 2 ** (p - 1)
		v = 0
		while b > 0:
			if a % 10 != b % 10:
				break
			v += p * (b % 10)
			a = a // 10
			b = b // 10
			p = p // 2
		v += p + p - 1
		print(v)
	else:
		p = t
		p = 2 ** (p - 1)
		v = p + p - 1
		print(v)


def convert(n, arr):
	for i in range(60):
		arr[-(i + 1)] = n % 2
		n = n // 2
t = int(input())
for i in range(t):
	(l, r) = [int(i) for i in input().split()]
	l_bit = [0 for i in range(60)]
	r_bit = [0 for i in range(60)]
	convert(l, l_bit)
	convert(r, r_bit)
	ans = 0
	for i in range(60):
		if l_bit[i] == 1 and r_bit[i] == 1:
			ans += 2 ** (59 - i)
		elif l_bit[i] == 0 and r_bit[i] == 1:
			ans += 2 ** (60 - i) - 1
			break
	print(ans)


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	d = l ^ r
	m = 1
	while d:
		d >>= 1
		m <<= 1
	print(r | m - 1)


for w in range(int(input())):
	(l, r) = [int(n) for n in input().split()]
	l = str(bin(l))
	r = str(bin(r))
	while len(l) != len(r):
		l = l[0:2] + '0' + l[2:]
	index = 0
	if l != r:
		while l[index] == r[index]:
			index += 1
		l = l[:index + 1] + ''.join(['1' * (len(l) - index - 1)])
		r = r[:index + 1] + ''.join(['0' * (len(r) - index - 1)])
	print(int(l, 2) | int(r, 2))


from math import log
for _ in range(int(input())):
	(N, M) = map(int, input().split())
	val = log(M, 2)
	if N == M:
		print(N)
	elif val == int(val):
		print(M | M - 1)
	elif len(bin(N)) != len(bin(M)):
		print(M | 2 ** int(val) - 1)
	else:
		p = bin(M)[2:]
		q = bin(N)[2:]
		for i in range(len(p)):
			if p[i] != q[i]:
				w = i
				break
		plo = ''
		for i in range(len(q)):
			if i > w:
				plo += '1'
			else:
				plo += q[i]
		new = int(plo, 2)
		print(M | new)


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

def binaryToDecimal(n):
	return int(n, 2)
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a == 0 and b == 0:
		print(0)
	elif a == b:
		print(a)
	else:
		a_bin = decimalToBinary(a)
		b_bin = decimalToBinary(b)
		if len(a_bin) == len(b_bin):
			for i in range(len(a_bin)):
				if a_bin[i] == b_bin[i]:
					continue
				else:
					ind = i
					break
			ans_st = ''
			for i in range(len(a_bin)):
				if i < ind:
					ans_st += a_bin[i]
				else:
					ans_st += '1'
			ans = binaryToDecimal(ans_st)
			print(ans)
		elif len(a_bin) < len(b_bin):
			ans_st = ''
			for i in range(len(b_bin)):
				ans_st += '1'
			ans = binaryToDecimal(ans_st)
			print(ans)


def decimalToBinary(n):
	return bin(n).replace('0b', '')
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	(a, b) = (decimalToBinary(l), decimalToBinary(r))
	if len(a) != len(b):
		print(pow(2, max(len(a), len(b))) - 1)
	else:
		ans = ''
		index = 0
		for i in range(len(a)):
			if a[i] == b[i]:
				ans += a[i]
				continue
			index = i
			break
		if index != 0:
			for i in range(index, len(a)):
				ans += '1'
		print(int(ans, 2))


import sys

def different_bit_pos(x, y):
	max_len = max(len(x), len(y))
	if len(x) == max_len:
		new_y = ''
		for i in range(max_len - len(y)):
			new_y += '0'
		new_y += y
		y = new_y
	elif len(y) == max_len:
		new_x = ''
		for i in range(max_len - len(x)):
			new_x += '0'
		new_x += x
		x = new_x
	for i in range(max_len):
		if x[i] != y[i]:
			return (i, x, y)
	return (-1, x, y)
t = int(sys.stdin.readline())
for test_case in range(t):
	(l, r) = map(int, sys.stdin.readline().split())
	left_digit = bin(l)[2:]
	right_digit = bin(r)[2:]
	(pos, left_digit, right_digit) = different_bit_pos(left_digit, right_digit)
	if pos != -1:
		constructed_left = left_digit[:pos]
		constructed_right = right_digit[:pos]
		constructed_left += '0'
		constructed_right += '1'
		for i in range(len(left_digit) - pos - 1):
			constructed_left += '1'
			constructed_right += '0'
		l = int(constructed_left, 2)
		r = int(constructed_right, 2)
		print(l | r)
	else:
		print(l)


for i in range(int(input())):
	(l, r) = map(int, input().split())
	sr = bin(r).replace('0b', '')
	sl = bin(l).replace('0b', '')
	ans = 0
	if len(sr) > len(sl):
		for i in range(len(sr)):
			ans += 2 ** i
		print(ans)
	else:
		k = r
		t = l
		for i in range(len(sr)):
			if sr[i] == '0':
				if sl[i] == '0':
					if t + 2 ** (len(sr) - 1 - i) <= r:
						k += 2 ** (len(sr) - 1 - i)
						t += 2 ** (len(sr) - 1 - i)
				if sl[i] == '1':
					k += 2 ** (len(sr) - 1 - i)
		print(k)


while True:
	try:
		for _ in range(int(input())):
			(l, r) = map(int, input().split())
			(l, r) = (min(l, r), max(l, r))
			binL = bin(l)[2:]
			binR = bin(r)[2:]
			llen = len(binL)
			rlen = len(binR)
			binL = '0' * (rlen - llen) + binL
			llen = rlen
			ans = []
			ind = -1
			i = 0
			while i < rlen and binR[i] == binL[i]:
				ans.append(binR[i])
				ind = i
				i += 1
			ans += ['1'] * (rlen - 1 - ind)
			print(int(''.join(ans), 2))
	except (ValueError, EOFError) as e:
		break


t = int(input())
while t > 0:
	t -= 1
	(l, r) = list(map(int, input().split()))
	l = bin(l).replace('0b', '')
	r = bin(r).replace('0b', '')
	if len(l) < len(r):
		print(int('1' * len(r), 2))
	elif len(l) == len(r):
		for i in range(len(l)):
			if r[i] == '1' and l[i] == '0':
				r = r[:i] + '1' * (len(l) - i)
				break
		print(int(r, 2))


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	i = 0
	while l | 1 << i < r:
		l = l | 1 << i
		i += 1
	print(l | r)


import math
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	numberOfbitsofL = int(math.log2(l) + 1)
	numberOfbitsofR = int(math.log2(r) + 1)
	bil = bin(l).replace('0b', '')
	bir = bin(r).replace('0b', '')
	if numberOfbitsofL == numberOfbitsofR:
		maxx = 0
		s = ''
		for i in range(numberOfbitsofR):
			s += bir[i]
			if bil[i] != bir[i]:
				break
		s += '1' * (numberOfbitsofR - i - 1)
		print(int(s, 2))
		continue
	else:
		print(int('1' * numberOfbitsofR, 2))
		continue


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	(bl, br) = (bin(l)[2:], bin(r)[2:])
	(l1, l2) = (len(bl), len(br))
	bl = '0' * (l2 - l1) + bl
	for i in range(l2):
		if bl[i] != br[i]:
			break
	bl = bl[:i + 1] + (l2 - (i + 1)) * '1'
	print(int(bl, 2) | int(br, 2))


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if l == r:
		print(l)
		continue
	bin_l = bin(l)[2:]
	bin_r = bin(r)[2:]
	if len(bin_l) < len(bin_r):
		bin_l = '0' * (len(bin_r) - len(bin_l)) + bin_l
	i = 0
	while bin_l[i] == bin_r[i]:
		i += 1
	ans = bin_r[:i] + '1' * (len(bin_r) - i)
	print(int(ans, 2))