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

Chef got interested in bits and wanted to learn about them, so his friend Pintu gave him a special function $F(X,Y,Z) = (X \wedge Z) \cdot (Y \wedge Z)$, where $\wedge$ is the [bitwise AND] operator and $X, Y, Z$ are non-negative integers.

Pintu wants Chef to maximise the function $F(X,Y,Z)$ for given $X$ and $Y$ by choosing an appropriate $Z$. However, to make it interesting, Pintu also gave Chef limits $L$ and $R$ for $Z$. In other words, he wants Chef to find a non-negative integer $Z$ ($L ≤ Z ≤ R$) such that $F(X,Y,Z) = \mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$. If there is more than one such value of $Z$, he should find the smallest one in the range $[L, R]$.

Since Chef is busy cooking in the kitchen, he needs you to help him solve this problem.

Note: $X$, $Y$, $L$ and $R$ are chosen in such a way that $\mathrm{max}_{L ≤ k ≤ R} ( F(X,Y,k) )$ never exceeds $2^{62}$.

------  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 four space-separated integers $X$, $Y$, $L$ and $R$.

------  Output ------
For each test case, print a single line containing one integer ― the smallest value of $Z$ in the given range that maximises $F$.

------  Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ X,Y ≤ 10^{12}$
$0 ≤ L ≤ R ≤ 10^{12}$

------  Subtasks ------
Subtask #1 (15 points):
$L = 0$
$R ≥ 2 \cdot \mathrm{max}(X,Y)$

Subtask #2 (25 points): $L = 0$

Subtask #3 (60 points): original constraints

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

7 12 4 17

7 12 0 8
----- Sample Output 1 ------ 
15

7
----- explanation 1 ------ 
Example case 1: Here, $Z = 15$ maximises the function, since $F(7,12,15) = 84$. It is impossible to reach $F > 84$ with any $Z$ in the given range.

Example case 2: The smallest $Z$ which maximises $F$ is $Z = 7$, and the value of $F$ for this $Z$ is $28$.

Difficulty: VERY_HARD

Solutions:
def func(x, y, z):
	return (x & z) * (y & z)

def recursion(lo, ans):
	global l, r
	if lo & lo - 1 == 0:
		x = lo | ans
		return x
	else:
		y = recursion(lo & lo - 1, ans)
		if y >= l:
			return y
		return lo | ans
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().strip().split(' '))
	q = x | y
	if x == 0 or y == 0:
		print(l)
	elif q >= l and q <= r:
		print(q)
	else:
		maxx = func(x, y, r)
		ans = r
		z = r
		while z:
			temp = z - 1
			if temp < l:
				break
			if func(x, y, temp) >= maxx:
				maxx = func(x, y, temp)
				ans = temp
			z = z & z - 1
		if func(x, y, l) >= maxx:
			maxx = func(x, y, l)
			ans = l
		s = ans & q
		if s >= l:
			print(s)
		else:
			x = recursion(l, s)
			print(x)


import math
for _ in range(int(input())):
	str1 = input()
	lst1 = str1.split()
	x = int(lst1[0])
	y = int(lst1[1])
	l = int(lst1[2])
	r = int(lst1[3])
	ans = x | y
	if l == 0 and r >= 2 * max(x, y):
		if x == 0 or y == 0:
			print(0)
		else:
			print(ans)
		continue
	rbin = str(bin(r)[2:])
	rlen = len(rbin)
	maxval = l
	maxprod = (x & l) * (y & l)
	for i in range(0, rlen):
		if rbin[i] == '1':
			rbin2 = rbin[:i] + '0' + '1' * (rlen - i - 1)
			val = int(rbin2, 2)
			prod = (x & val) * (y & val)
			if prod > maxprod and val >= l:
				maxprod = prod
				maxval = val
	if (x & r) * (y & r) > maxprod:
		maxprod = (x & r) * (y & r)
		maxval = r
	binans = '{0:064b}'.format(ans)
	binmax = '{0:064b}'.format(maxval)
	temp = 0
	for i in range(0, 64):
		if binmax[i] == '1' and binans[i] == '0':
			temp = pow(2, 63 - i)
			if maxval - temp >= l:
				maxval = maxval - temp
	print(maxval)


def rangingl(lmin, ans):
	global l
	if lmin & lmin - 1 == 0:
		x = lmin | ans
		return x
	else:
		y = rangingl(lmin & lmin - 1, ans)
		if y >= l:
			return y
		return lmin | ans
t = int(input())
for test in range(t):
	(x, y, l, r) = map(int, input().split())
	p = x | y
	p1 = bin(p)
	fmax = x * y
	if x == 0 or y == 0:
		print(l)
	elif p <= r and p >= l:
		print(p)
	else:
		fmax = (x & r) * (y & r)
		z = r
		ans = r
		while z:
			temp = z - 1
			if temp < l:
				break
			if (x & temp) * (y & temp) >= fmax:
				fmax = (x & temp) * (y & temp)
				ans = temp
			z = z & z - 1
		if (x & l) * (y & l) >= fmax:
			fmax = (x & l) * (y & l)
			ans = l
		s = ans & p
		if s >= l:
			print(s)
		else:
			x = rangingl(l, s)
			print(x)


for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	x_or_y = x | y
	if l == 0 and r >= 2 * max(x, y):
		if x == 0 or y == 0:
			print(0)
			continue
		print(x_or_y)
		continue
	maxp = (x & l) * (y & l)
	m_val = l
	binr = bin(r)
	binr = binr[2:]
	bl = len(binr)
	for i in range(bl):
		if binr[i] == '1':
			p = binr[:i] + '0' + '1' * (bl - i - 1)
			p = int(p, 2)
			if maxp < (x & p) * (y & p) and p >= l:
				maxp = (x & p) * (y & p)
				m_val = p
	p = r
	if maxp < (x & p) * (y & p):
		maxp = (x & p) * (y & p)
		m_val = p
	bx = '{0:064b}'.format(x_or_y)
	bm = '{0:064b}'.format(m_val)
	temp = 0
	for i in range(0, 64):
		if bx[i] == '0' and bm[i] == '1':
			temp = pow(2, 63 - i)
			if m_val - temp >= l:
				m_val = m_val - temp
	print(m_val)


def xor(x, y, z):
	return (x & z) * (y & z)

def find(new_l, ans):
	global l
	if new_l & new_l - 1 == 0:
		x = new_l | ans
		return x
	else:
		y = find(new_l & new_l - 1, ans)
		if y >= l:
			return y
		return new_l | ans
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	diff = x | y
	if x == 0 or y == 0:
		print(l)
	elif diff >= l and diff <= r:
		print(diff)
	else:
		maxi = xor(x, y, r)
		ans = r
		z = r
		while z:
			t = z - 1
			if t < l:
				break
			temp = xor(x, y, t)
			if temp >= maxi:
				maxi = temp
				ans = t
			z = z & t
		if xor(x, y, l) >= maxi:
			maxi = xor(x, y, l)
			ans = l
		s = ans & diff
		if s >= l:
			print(s)
		else:
			z = find(l, s)
			print(z)


def findz(l, s):
	temp = l
	if temp & temp - 1 == 0:
		x = temp | s
		return x
	else:
		y = findz(temp & temp - 1, s)
		if y >= l:
			return y
		return temp | s

def f(x, y, z):
	return (x & z) * (y & z)
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().strip().split())
	q = x | y
	if x == 0 or y == 0:
		print(l)
	elif q >= l and q <= r:
		print(q)
	else:
		maxx = f(x, y, r)
		a = r
		z = r
		while z:
			temp = z - 1
			if temp < l:
				break
			if f(x, y, temp) >= maxx:
				maxx = f(x, y, temp)
				a = temp
			z = z & z - 1
		if f(x, y, l) >= maxx:
			maxx = f(x, y, l)
			a = l
		s = a & q
		if s >= l:
			print(s)
		else:
			x = findz(l, s)
			print(x)


def chandf(x, y, l, r):
	a = (x & l) * (y & l)
	id = l
	Z = x | y
	for i in range(64):
		if r & 1 << i:
			k = r ^ 1 << i | (1 << i) - 1
			if k < l:
				continue
			for j in range(i - 1, -1, -1):
				if Z & 1 << j == 0:
					if k ^ 1 << j >= l:
						k = k ^ 1 << j
			temp = (k & x) * (k & y)
			if temp > a:
				a = temp
				id = k
			elif temp == a:
				id = min(id, k)
	if (x & r) * (y & r) > a:
		id = r
	print(id)
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	chandf(x, y, l, r)


dry_run = False
inputs = [[252, 523, 2, 199], [485, 719, 0, 159], [104, 600, 4, 460], [5, 33, 22, 28], [37, 75, 0, 68], [51, 33, 0, 5], [32, 5, 0, 36], [360, 845, 0, 410], [108, 595, 3, 415], [234, 470, 4, 165], [362, 851, 2, 434], [487, 725, 2, 183], [111, 603, 1, 435], [236, 477, 1, 184], [364, 840, 4, 441], [489, 714, 4, 190], [113, 591, 3, 442], [238, 465, 3, 191], [366, 847, 1, 424], [492, 722, 2, 174], [115, 598, 0, 426], [241, 473, 1, 176], [351, 354, 1, 429], [476, 728, 1, 178], [100, 105, 0, 430], [225, 479, 0, 179], [353, 360, 3, 453], [478, 734, 3, 202], [102, 111, 2, 454], [227, 485, 2, 203], [355, 849, 0, 454], [481, 724, 1, 204], [230, 475, 0, 205], [357, 855, 2, 443], [483, 730, 3, 193], [106, 106, 1, 444], [232, 481, 2, 194], [377, 397, 2, 447], [3, 272, 3, 197], [126, 148, 1, 449], [379, 404, 4, 471], [5, 279, 0, 216], [128, 155, 3, 473], [254, 530, 4, 223], [382, 393, 2, 474], [7, 267, 2, 223], [130, 144, 0, 474], [256, 519, 1, 224], [384, 400, 4, 463], [9, 274, 4, 212], [133, 151, 3, 464], [258, 525, 3, 213], [368, 406, 3, 466], [494, 781, 4, 216], [117, 157, 2, 467], [243, 532, 3, 217], [371, 413, 1, 345], [496, 787, 1, 94], [119, 163, 4, 350], [245, 538, 0, 95], [373, 402, 3, 352], [498, 776, 3, 101], [122, 153, 2, 353], [247, 527, 2, 102], [375, 408, 0, 335], [0, 282, 0, 84], [124, 160, 4, 342], [249, 534, 4, 91], [395, 810, 0, 340], [20, 184, 0, 89], [144, 561, 4, 346], [269, 435, 4, 95], [397, 816, 2, 364], [22, 190, 2, 113], [146, 567, 1, 365], [271, 441, 1, 114], [399, 805, 4, 370], [25, 180, 0, 115], [148, 556, 3, 371], [274, 431, 4, 121], [401, 811, 1, 354], [27, 186, 2, 104], [150, 562, 0, 355], [276, 437, 1, 105], [386, 818, 1, 358], [11, 192, 1, 107], [135, 569, 0, 360], [260, 443, 0, 109], [388, 825, 3, 382], [14, 200, 4, 132], [137, 576, 2, 384], [263, 451, 3, 134], [390, 813, 0, 384], [16, 188, 1, 134], [139, 565, 4, 390], [265, 440, 0, 135], [393, 821, 3, 374], [18, 195, 3, 123], [141, 571, 1, 374], [267, 446, 2, 124], [412, 862, 2, 377], [38, 237, 3, 127], [161, 613, 1, 378], [287, 488, 2, 128], [415, 869, 0, 397], [40, 243, 0, 146], [163, 619, 3, 402], [289, 494, 4, 152], [417, 858, 2, 403], [42, 232, 2, 152], [166, 609, 1, 405], [291, 483, 1, 154], [419, 864, 4, 392], [44, 238, 4, 141], [168, 616, 3, 393], [293, 490, 3, 142], [404, 871, 4, 397], [29, 245, 4, 146], [152, 622, 2, 397], [278, 497, 3, 147], [406, 878, 1, 275], [31, 252, 1, 24], [155, 629, 0, 276], [280, 503, 0, 25], [408, 867, 3, 281], [33, 241, 3, 30], [157, 618, 2, 282], [282, 492, 2, 31], [410, 873, 0, 265], [36, 248, 1, 15], [159, 624, 4, 271], [285, 499, 0, 16], [289, 633, 4, 273], [415, 508, 0, 18], [38, 384, 3, 275], [164, 259, 4, 25], [291, 640, 1, 292], [417, 515, 2, 42], [40, 391, 0, 294], [166, 266, 1, 44], [294, 629, 4, 300], [419, 503, 4, 49], [42, 380, 2, 300], [168, 255, 3, 50], [296, 636, 1, 284], [421, 510, 1, 33], [45, 387, 0, 285], [170, 261, 0, 34], [280, 642, 0, 287], [406, 517, 1, 37], [29, 393, 4, 293], [155, 268, 0, 38], [283, 649, 3, 312], [408, 523, 3, 61], [31, 399, 1, 312], [157, 274, 2, 62], [285, 638, 0, 313], [410, 512, 0, 62], [34, 389, 4, 320], [159, 263, 4, 69], [287, 644, 2, 302], [412, 518, 2, 51], [36, 396, 1, 303], [161, 270, 1, 52], [307, 686, 2, 307], [432, 560, 2, 56], [56, 438, 1, 308], [181, 312, 1, 57], [309, 693, 4, 331], [434, 567, 4, 80], [58, 444, 3, 332], [183, 318, 3, 81], [311, 682, 1, 332], [437, 557, 2, 82], [60, 433, 0, 333], [186, 308, 1, 83], [313, 688, 3, 321], [439, 563, 4, 71], [62, 439, 2, 322], [188, 314, 3, 72], [298, 695, 3, 325], [423, 569, 3, 74], [47, 446, 2, 326], [172, 320, 2, 75], [300, 701, 0, 267], [426, 576, 1, 17], [49, 453, 4, 273], [175, 328, 0, 18], [302, 690, 2, 273], [428, 565, 3, 23], [51, 441, 1, 274], [177, 316, 2, 24], [305, 698, 0, 258], [430, 572, 0, 7], [53, 448, 3, 263], [179, 323, 4, 13], [324, 598, 4, 266], [450, 473, 0, 11], [73, 349, 3, 267], [199, 224, 4, 17], [327, 605, 2, 286], [452, 479, 2, 35], [75, 355, 0, 286], [201, 230, 1, 36], [329, 594, 4, 293], [454, 468, 4, 42], [78, 345, 3, 294], [203, 219, 3, 43], [331, 600, 1, 276], [456, 474, 1, 25], [80, 352, 0, 278], [205, 226, 0, 27], [316, 607, 1, 281], [441, 481, 1, 30], [64, 358, 4, 286], [190, 233, 0, 31], [318, 614, 3, 305], [443, 488, 3, 54], [67, 365, 2, 306], [192, 239, 2, 55], [320, 603, 0, 306], [445, 477, 0, 55], [69, 354, 4, 312], [194, 228, 4, 61], [322, 609, 2, 295], [448, 484, 3, 45], [71, 360, 1, 296], [197, 235, 2, 46], [342, 651, 2, 299], [467, 525, 2, 48], [91, 402, 1, 301], [216, 276, 1, 50], [344, 657, 4, 323], [470, 532, 0, 68], [93, 409, 3, 325], [219, 284, 4, 75], [346, 646, 1, 325], [472, 521, 2, 75], [95, 397, 0, 326], [221, 272, 1, 76], [349, 654, 4, 315], [474, 528, 4, 64], [97, 404, 2, 315], [223, 279, 3, 65], [333, 660, 3, 318], [459, 535, 4, 68], [82, 411, 2, 319], [208, 286, 3, 69], [335, 666, 0, 196], [461, 541, 1, 446], [84, 417, 4, 202], [210, 292, 0, 447], [338, 656, 3, 204], [463, 530, 3, 453], [86, 406, 1, 204], [212, 281, 2, 454], [340, 662, 0, 187], [465, 536, 0, 436], [89, 413, 4, 194], [214, 287, 4, 443], [219, 486, 4, 196], [344, 360, 4, 445], [468, 737, 3, 197], [93, 111, 3, 446], [221, 492, 1, 215], [346, 366, 1, 464], [470, 743, 0, 216], [95, 117, 0, 465], [223, 481, 3, 221], [349, 356, 4, 471], [472, 732, 2, 222], [98, 107, 3, 472], [225, 487, 0, 205], [351, 362, 1, 455], [474, 738, 4, 211], [100, 113, 0, 456], [210, 494, 0, 209], [335, 368, 0, 458], [459, 745, 4, 216], [84, 119, 4, 465], [212, 501, 2, 233], [338, 376, 3, 483], [461, 752, 1, 235], [87, 127, 2, 485], [214, 489, 4, 240], [340, 364, 0, 485], [463, 741, 3, 241], [89, 116, 4, 491], [217, 497, 2, 225], [342, 371, 2, 474], [465, 747, 0, 225], [91, 122, 1, 475], [236, 538, 1, 228], [362, 413, 2, 478], [485, 789, 0, 229], [111, 164, 1, 479], [239, 545, 4, 253], [364, 419, 4, 502], [487, 795, 2, 253], [113, 170, 3, 3], [241, 534, 1, 254], [366, 408, 1, 3], [490, 785, 0, 256], [115, 159, 0, 5], [243, 540, 3, 243], [368, 414, 3, 492], [492, 792, 2, 244], [117, 166, 2, 493], [228, 547, 3, 248], [353, 421, 3, 497], [476, 798, 1, 248], [102, 173, 2, 498], [230, 554, 0, 126], [355, 428, 0, 375], [479, 805, 4, 132], [104, 179, 4, 381], [232, 543, 2, 132], [357, 417, 2, 381], [481, 794, 1, 133], [106, 168, 1, 382], [234, 549, 4, 121], [360, 424, 0, 366], [483, 800, 3, 122], [109, 175, 4, 372], [254, 450, 4, 125], [379, 824, 4, 374], [3, 201, 3, 127], [128, 575, 3, 376], [256, 457, 1, 144], [382, 832, 2, 394], [5, 208, 0, 146], [131, 583, 1, 396], [258, 445, 3, 151], [384, 820, 4, 401], [7, 197, 2, 152], [133, 572, 3, 402], [261, 453, 1, 136], [386, 827, 1, 385], [9, 203, 4, 141], [135, 578, 0, 386], [245, 459, 0, 139], [371, 834, 1, 389], [494, 710, 4, 145], [120, 585, 0, 390], [247, 465, 2, 163], [373, 840, 3, 413], [496, 716, 1, 164], [122, 591, 2, 414], [250, 455, 0, 165], [375, 829, 0, 414], [498, 705, 3, 171], [124, 580, 4, 421], [252, 461, 2, 154], [377, 835, 2, 403], [1, 213, 1, 155], [126, 587, 1, 404], [272, 503, 2, 159], [397, 877, 2, 408], [20, 254, 0, 159], [146, 629, 1, 409], [274, 510, 4, 183], [399, 884, 4, 432], [23, 261, 3, 184], [148, 635, 3, 433], [276, 499, 1, 184], [401, 873, 1, 433], [25, 250, 0, 185], [150, 624, 0, 434], [278, 505, 3, 173], [404, 880, 4, 423], [27, 256, 2, 174], [153, 631, 3, 424], [263, 512, 3, 177], [388, 886, 3, 426], [12, 263, 2, 178], [137, 137, 2, 427], [265, 518, 0, 55], [390, 392, 0, 304], [14, 270, 4, 62], [139, 144, 4, 311], [267, 507, 2, 62], [393, 882, 3, 312], [16, 258, 1, 63], [142, 633, 2, 313], [269, 514, 4, 51], [395, 889, 0, 296], [18, 265, 3, 52], [144, 640, 4, 302], [148, 274, 3, 54], [274, 649, 4, 304], [397, 525, 2, 55], [23, 400, 3, 305], [151, 281, 1, 74], [276, 655, 1, 323], [399, 531, 4, 79], [25, 406, 0, 324], [153, 270, 3, 80], [278, 644, 3, 329], [402, 521, 2, 82], [27, 395, 2, 331], [155, 276, 0, 64], [280, 650, 0, 313], [404, 528, 4, 70], [29, 402, 4, 319], [140, 283, 0, 69], [265, 657, 0, 318], [388, 534, 3, 74], [14, 409, 4, 324], [142, 290, 2, 93], [267, 664, 2, 342], [391, 541, 1, 94], [16, 415, 1, 343], [144, 279, 4, 99], [269, 653, 4, 348], [393, 530, 3, 100], [18, 404, 3, 349], [146, 285, 1, 83], [272, 660, 2, 333], [395, 536, 0, 84], [21, 411, 1, 334], [166, 327, 1, 87], [291, 701, 1, 336], [415, 578, 0, 88], [40, 452, 0, 337], [168, 333, 3, 111], [294, 708, 4, 361], [417, 585, 2, 112], [43, 460, 3, 362], [170, 322, 0, 113], [296, 697, 1, 363], [419, 573, 4, 119], [45, 448, 0, 364], [173, 330, 3, 102], [298, 704, 3, 351], [421, 580, 1, 103], [47, 455, 2, 353], [157, 336, 2, 106], [283, 711, 3, 356], [406, 587, 1, 107], [32, 462, 2, 357], [159, 342, 4, 52], [285, 717, 0, 297], [408, 593, 3, 53], [34, 468, 4, 303], [162, 332, 2, 55], [287, 706, 2, 304], [410, 582, 0, 55], [36, 457, 1, 305], [164, 338, 4, 43], [289, 712, 4, 292], [413, 589, 3, 45], [38, 463, 3, 294], [184, 239, 4, 48], [309, 613, 4, 297], [432, 490, 2, 48], [58, 365, 3, 298], [186, 246, 1, 67], [311, 620, 1, 316], [435, 497, 0, 68], [60, 371, 0, 317], [188, 235, 3, 73], [313, 609, 3, 322], [437, 486, 2, 74], [62, 360, 2, 323], [190, 241, 0, 57], [316, 616, 1, 307], [439, 492, 4, 63], [65, 367, 0, 308], [175, 248, 0, 61], [300, 622, 0, 310], [424, 499, 4, 68], [49, 373, 4, 317], [177, 254, 2, 85], [302, 628, 2, 334], [426, 506, 1, 87], [51, 380, 1, 336], [179, 243, 4, 92], [305, 618, 0, 337], [428, 494, 3, 93], [54, 369, 4, 343], [181, 250, 1, 76], [307, 625, 2, 326], [430, 501, 0, 77], [56, 376, 1, 327], [201, 292, 1, 80], [327, 667, 2, 330], [450, 543, 0, 81], [76, 418, 1, 331], [203, 298, 3, 104], [329, 673, 4, 354], [452, 549, 2, 105], [78, 424, 3, 355], [206, 288, 1, 106], [331, 662, 1, 355], [454, 538, 4, 112], [80, 413, 0, 357], [208, 294, 3, 95], [333, 668, 3, 344], [457, 545, 2, 96], [82, 419, 2, 345], [192, 300, 2, 99], [318, 675, 3, 349], [441, 551, 1, 100], [67, 426, 2, 350], [195, 308, 0, 478], [320, 682, 0, 227], [443, 558, 3, 483], [69, 433, 4, 233], [197, 296, 2, 484], [322, 670, 2, 233], [446, 548, 1, 485], [71, 422, 1, 234], [199, 303, 4, 473], [324, 677, 4, 222], [448, 554, 3, 474], [73, 428, 3, 223], [141, 189, 1, 474], [267, 564, 2, 224], [390, 440, 0, 476], [16, 315, 1, 226], [143, 196, 3, 498], [269, 571, 4, 248], [392, 447, 2, 500], [18, 322, 3, 250], [145, 184, 0, 0], [271, 559, 1, 250], [394, 436, 4, 6], [20, 311, 0, 251], [148, 192, 3, 490], [273, 566, 3, 239], [397, 443, 2, 491], [22, 317, 2, 240], [132, 198, 2, 493], [258, 573, 3, 243], [381, 449, 1, 494], [7, 324, 2, 244], [134, 204, 4, 17], [260, 579, 0, 262], [383, 455, 3, 18], [9, 330, 4, 268], [137, 194, 2, 19], [262, 568, 2, 268], [386, 445, 1, 21], [11, 319, 1, 270], [139, 200, 4, 8], [264, 574, 4, 257], [388, 452, 3, 9], [13, 326, 3, 258], [159, 242, 4, 13], [284, 616, 4, 262], [408, 494, 3, 14], [33, 368, 3, 263], [161, 249, 1, 32], [286, 623, 1, 281], [410, 500, 0, 33], [35, 374, 0, 282], [163, 238, 3, 38], [289, 613, 4, 288], [412, 489, 2, 39], [38, 364, 3, 289], [165, 244, 0, 22], [291, 619, 1, 272], [414, 495, 4, 28], [40, 370, 0, 273], [150, 251, 0, 26], [275, 625, 0, 275], [399, 502, 4, 32], [24, 376, 4, 281], [152, 257, 2, 409], [278, 632, 3, 159], [401, 509, 1, 411], [27, 384, 2, 161], [154, 246, 4, 416], [280, 621, 0, 161], [403, 497, 3, 417], [29, 372, 4, 167], [156, 253, 1, 400], [282, 628, 2, 150], [405, 504, 0, 401], [31, 379, 1, 151], [176, 654, 1, 404], [302, 529, 2, 154], [425, 905, 0, 405], [51, 280, 1, 155], [178, 660, 3, 428], [304, 535, 4, 178], [427, 911, 2, 429], [53, 286, 3, 179], [181, 650, 1, 430], [306, 524, 1, 179], [430, 901, 0, 432], [55, 275, 0, 181], [183, 656, 3, 419], [308, 530, 3, 168], [432, 908, 2, 420], [57, 282, 2, 169], [167, 662, 2, 423], [293, 537, 3, 173], [416, 914, 1, 424], [42, 289, 2, 174], [170, 170, 0, 443], [295, 544, 0, 192], [419, 421, 4, 449], [44, 295, 4, 198], [172, 659, 2, 449], [297, 533, 2, 198], [421, 910, 1, 450], [46, 284, 1, 199], [174, 665, 4, 438], [300, 540, 0, 183], [423, 916, 3, 439], [49, 291, 4, 189], [194, 207, 4, 442], [319, 581, 4, 191], [443, 458, 3, 443], [68, 332, 3, 192], [196, 213, 1, 461], [322, 588, 2, 211], [445, 465, 0, 462], [71, 340, 1, 212], [198, 202, 3, 468], [324, 577, 4, 218], [447, 453, 2, 469], [73, 328, 3, 219], [200, 209, 0, 451], [326, 584, 1, 201], [449, 460, 4, 458], [75, 335, 0, 203], [185, 216, 0, 456], [311, 591, 1, 206], [434, 467, 4, 462], [60, 342, 0, 207], [187, 222, 2, 339], [313, 597, 3, 89], [436, 473, 1, 340], [62, 348, 2, 90], [189, 211, 4, 345], [315, 586, 0, 90], [438, 462, 3, 347], [64, 337, 4, 97], [192, 218, 2, 330], [317, 592, 2, 79], [441, 469, 1, 331], [66, 343, 1, 80], [71, 478, 1, 334], [196, 352, 1, 83], [320, 730, 0, 335], [445, 604, 0, 84], [73, 485, 3, 358], [198, 359, 3, 107], [322, 736, 2, 359], [447, 610, 2, 108], [75, 474, 0, 359], [201, 349, 1, 109], [324, 725, 4, 365], [450, 600, 0, 110], [77, 480, 2, 348], [203, 355, 3, 98], [326, 731, 1, 349], [452, 606, 2, 99], [62, 487, 2, 352], [187, 361, 2, 101], [311, 738, 1, 353], [436, 612, 1, 102], [64, 493, 4, 376], [190, 368, 0, 121], [313, 745, 3, 377], [439, 620, 4, 127], [66, 482, 1, 378], [192, 357, 2, 128], [315, 733, 0, 379], [441, 608, 1, 129], [69, 490, 4, 367], [194, 364, 4, 116], [317, 740, 2, 368], [443, 615, 3, 118], [88, 531, 3, 371], [214, 406, 4, 121], [337, 782, 2, 372], [463, 657, 3, 122], [90, 537, 0, 390], [216, 412, 1, 140], [339, 788, 4, 396], [465, 663, 0, 141], [93, 527, 3, 397], [218, 401, 3, 146], [342, 778, 2, 398], [467, 652, 2, 147], [95, 533, 0, 381], [220, 407, 0, 130], [344, 784, 4, 387], [469, 658, 4, 136], [80, 540, 0, 385], [205, 414, 0, 134], [328, 790, 3, 391], [454, 665, 4, 141], [82, 547, 2, 332], [207, 421, 2, 81], [331, 798, 1, 333], [456, 672, 1, 82], [84, 535, 4, 338], [209, 409, 4, 87], [333, 787, 3, 339], [458, 661, 3, 88], [86, 542, 1, 322], [212, 417, 2, 72], [335, 793, 0, 323], [461, 668, 1, 73], [106, 443, 1, 326], [231, 317, 1, 75], [355, 694, 0, 328], [480, 568, 0, 76], [108, 449, 3, 350], [234, 324, 4, 100], [357, 701, 2, 352], [483, 576, 3, 101], [110, 438, 0, 352], [236, 313, 1, 102], [359, 689, 4, 358], [485, 564, 0, 103], [112, 445, 2, 341], [238, 320, 3, 90], [361, 696, 1, 342], [487, 571, 2, 92], [97, 452, 2, 345], [223, 327, 3, 95], [346, 703, 1, 346], [472, 578, 2, 96], [99, 458, 4, 369], [225, 333, 0, 114], [348, 709, 3, 370], [474, 584, 4, 120], [101, 447, 1, 370], [227, 322, 2, 120], [350, 698, 0, 372], [476, 573, 1, 121], [104, 454, 4, 360], [229, 328, 4, 109], [353, 705, 3, 361], [478, 579, 3, 110], [123, 495, 3, 364], [249, 370, 4, 113], [372, 746, 2, 365], [498, 621, 3, 115], [126, 503, 1, 384], [251, 377, 1, 132], [375, 754, 0, 385], [0, 128, 0, 134], [128, 491, 3, 390], [253, 365, 3, 139], [377, 743, 2, 391], [2, 117, 2, 140], [130, 498, 0, 374], [256, 373, 1, 124], [379, 749, 4, 380], [5, 124, 0, 125], [115, 505, 0, 378], [240, 379, 0, 127], [364, 756, 4, 384], [489, 630, 4, 133], [117, 511, 2, 261], [242, 385, 2, 10], [366, 762, 1, 263], [491, 636, 1, 11], [119, 500, 4, 268], [245, 375, 0, 13], [368, 751, 3, 269], [494, 626, 4, 19], [121, 506, 1, 252], [247, 381, 2, 2], [370, 758, 0, 253], [496, 633, 1, 3], [0, 330, 0, 255], [126, 205, 1, 5], [249, 581, 4, 261], [375, 456, 0, 6], [3, 337, 3, 280], [128, 211, 3, 29], [251, 587, 1, 280], [377, 462, 2, 30], [5, 326, 0, 281], [130, 200, 0, 30], [254, 577, 4, 288], [379, 451, 4, 36], [7, 332, 2, 270], [132, 206, 2, 19], [256, 584, 1, 271], [381, 457, 1, 20], [492, 839, 2, 275], [117, 213, 2, 23], [240, 590, 0, 275], [366, 464, 1, 25], [494, 846, 4, 299], [119, 220, 4, 47], [243, 597, 3, 300], [368, 471, 3, 49], [496, 835, 1, 300], [121, 208, 1, 49], [245, 586, 0, 301], [370, 460, 0, 50], [498, 841, 3, 289], [124, 216, 4, 39], [247, 592, 2, 290], [373, 467, 3, 40], [18, 383, 3, 293], [143, 257, 3, 42], [267, 634, 2, 294], [392, 508, 2, 43], [20, 389, 0, 312], [146, 264, 1, 62], [269, 641, 4, 318], [395, 515, 0, 63], [22, 378, 2, 319], [148, 253, 3, 68], [271, 629, 1, 320], [397, 504, 2, 70], [25, 386, 0, 303], [150, 259, 0, 52], [273, 636, 3, 309], [399, 511, 4, 58], [9, 392, 4, 312], [135, 266, 0, 57], [258, 643, 3, 313], [384, 518, 4, 63], [11, 398, 1, 190], [137, 273, 2, 440], [260, 649, 0, 191], [386, 524, 1, 441], [14, 388, 4, 197], [139, 262, 4, 446], [262, 638, 2, 198], [388, 513, 3, 448], [16, 394, 1, 181], [141, 268, 1, 430], [265, 645, 0, 182], [390, 519, 0, 431], [36, 295, 1, 186], [161, 169, 1, 435], [284, 546, 4, 191], [410, 420, 0, 436], [38, 302, 3, 210], [163, 176, 3, 459], [287, 553, 2, 211], [412, 427, 2, 460], [40, 291, 0, 211], [165, 165, 0, 460], [289, 542, 4, 217], [414, 416, 4, 466], [42, 297, 2, 200], [168, 172, 3, 450], [291, 548, 1, 201], [417, 423, 2, 451], [27, 304, 2, 204], [152, 178, 2, 453], [276, 555, 1, 205], [401, 429, 1, 454], [29, 310, 4, 228], [154, 184, 4, 477], [278, 562, 3, 229], [403, 435, 3, 478], [31, 299, 1, 230], [157, 174, 2, 480], [280, 550, 0, 231], [406, 425, 1, 481], [33, 306, 3, 218], [159, 180, 4, 468], [282, 557, 2, 220], [408, 432, 3, 470], [53, 348, 3, 223], [179, 222, 4, 473], [302, 599, 2, 224], [428, 474, 3, 474], [55, 354, 0, 242], [181, 229, 1, 492], [304, 605, 4, 248], [430, 480, 0, 493], [58, 344, 3, 249], [183, 218, 3, 498], [306, 594, 1, 249], [432, 469, 2, 499], [60, 350, 0, 233], [185, 224, 0, 482], [309, 601, 4, 239], [434, 475, 4, 488], [44, 356, 4, 241], [170, 231, 0, 486], [293, 607, 3, 243], [419, 482, 4, 493], [47, 364, 2, 121], [172, 237, 2, 370], [295, 614, 0, 121], [421, 489, 1, 371], [49, 352, 4, 127], [174, 226, 4, 376], [298, 604, 3, 128], [423, 477, 3, 377], [51, 359, 1, 111], [176, 233, 1, 360], [300, 610, 0, 112], [425, 484, 0, 361], [430, 619, 0, 114], [55, 493, 0, 363], [179, 370, 4, 120], [304, 744, 4, 369], [432, 625, 2, 138], [58, 500, 3, 388], [181, 377, 1, 139], [307, 752, 2, 389], [434, 614, 4, 145], [60, 489, 0, 390], [183, 365, 3, 146], [309, 740, 4, 396], [437, 622, 2, 129], [62, 496, 2, 378], [185, 372, 0, 130], [311, 747, 1, 380], [421, 628, 1, 133], [47, 503, 2, 383], [170, 379, 0, 134], [296, 754, 1, 384], [423, 634, 3, 157], [49, 509, 4, 407], [172, 385, 2, 158], [298, 760, 3, 408], [426, 624, 1, 159], [51, 498, 1, 408], [174, 374, 4, 164], [300, 749, 0, 409], [428, 630, 3, 148], [53, 504, 3, 397], [177, 381, 2, 149], [302, 755, 2, 398], [448, 672, 3, 152], [73, 546, 3, 401], [196, 422, 1, 153], [322, 797, 2, 403], [450, 679, 0, 171], [75, 553, 0, 420], [199, 430, 4, 178], [324, 804, 4, 427], [452, 667, 2, 178], [77, 541, 2, 427], [201, 419, 1, 179], [326, 793, 1, 428], [454, 674, 4, 167], [80, 549, 0, 412], [203, 425, 3, 168], [329, 800, 4, 418], [439, 681, 4, 171], [64, 555, 4, 420], [188, 432, 3, 172], [313, 806, 3, 421], [441, 687, 1, 360], [66, 561, 1, 109], [190, 438, 0, 361], [315, 812, 0, 110], [443, 676, 3, 366], [69, 551, 4, 116], [192, 427, 2, 367], [318, 802, 3, 117], [445, 682, 0, 350], [71, 557, 1, 100], [194, 434, 4, 356], [320, 809, 0, 101], [465, 584, 0, 354], [91, 459, 1, 104], [214, 335, 4, 361], [340, 710, 0, 105], [467, 590, 2, 378], [93, 465, 3, 128], [216, 341, 1, 380], [342, 716, 2, 129], [470, 580, 0, 381], [95, 454, 0, 130], [218, 330, 3, 386], [344, 705, 4, 136], [472, 586, 2, 370], [97, 460, 2, 118], [221, 337, 1, 371], [346, 711, 1, 120], [456, 592, 1, 373], [82, 467, 2, 123], [205, 343, 0, 374], [331, 718, 1, 124], [459, 600, 4, 398], [84, 474, 4, 147], [207, 350, 2, 398], [333, 725, 3, 148], [461, 588, 1, 399], [86, 462, 1, 148], [210, 340, 0, 401], [335, 714, 0, 149], [463, 595, 3, 388], [88, 469, 3, 137], [212, 346, 2, 389], [337, 720, 2, 138], [483, 637, 3, 393], [108, 511, 3, 141], [232, 388, 2, 394], [357, 762, 2, 143], [485, 643, 0, 412], [110, 517, 0, 160], [234, 394, 4, 418], [359, 768, 4, 167]]
T = len(inputs)
if not dry_run:
	T = int(input())
full_mask = 2 ** 40 - 1

def correct_value(x, y, l, r):
	max_product = 0
	max_product_z = l
	for i in range(l, r + 1):
		z = (x & i) * (y & i)
		if z > max_product:
			max_product = z
			max_product_z = i
	return max_product_z

def get_z(x, y, l, r):
	choices = [l, r]
	alterable_bits = len(bin(l ^ r)) - 3
	max_possible_z = x | y
	max_product = -1
	max_z = 0
	set_bit = 2 ** alterable_bits
	mask = set_bit - 1
	for i in range(alterable_bits):
		set_bit = set_bit >> 1
		mask = mask >> 1
		masked_or = mask & max_possible_z
		if l & set_bit == 0 and max_possible_z & set_bit > 0:
			z = (l | set_bit) ^ l & mask | masked_or
			choices.append(z)
	set_bit = 2 ** alterable_bits
	mask = set_bit - 1
	for i in range(alterable_bits):
		set_bit = set_bit >> 1
		mask = mask >> 1
		masked_or = mask & max_possible_z
		if r & set_bit > 0:
			z = r ^ r & (set_bit << 1) - 1 | masked_or
			choices.append(z)
	for z in choices:
		product = (x & z) * (y & z)
		if product > max_product:
			max_product = product
			max_possible_z = z
		elif product == max_product and z < max_possible_z:
			max_possible_z = z
	return max_possible_z
for t in range(T):
	x = y = l = r = 0
	if dry_run:
		(x, y, l, r) = inputs[t]
	else:
		(x, y, l, r) = map(int, input().split())
	my = get_z(x, y, l, r)
	if dry_run:
		correct = correct_value(x, y, l, r)
		if my == correct:
			print('verified')
			pass
		else:
			print('not-verified     | x : {}, y : {}, l : {}, r : {}, calculated : {}, correct : {}'.format(x, y, l, r, my, correct))
			print('bin(x) :', bin(x), '\tbin(y) :', bin(y), '\tbin(correct) :', bin(correct), '\tbin(calculated) :', bin(my), '\n')
	else:
		print(my)


a = int(input())
for i in range(a):
	(x, y, l, r) = map(int, input().split())
	if x == 0 or y == 0:
		print(l)
		continue
	ans = x | y
	if l == 0 and r >= ans:
		print(ans)
		continue
	arr = []
	prev = 0
	if l == r:
		print(l)
		continue
	for j in range(43):
		k = 1 << j
		if ans & k != 0:
			arr.append(prev + k)
		else:
			arr.append(prev)
		prev = arr[j]
	z = 0
	k = 0
	for j in range(42, -1, -1):
		m = 1 << j
		k = j
		if l & m == r & m:
			if r & m != 0:
				z += m
		else:
			break
	p2 = l
	p1 = r
	ans1 = (x & p1) * (y & p1)
	ans2 = (x & p2) * (y & p2)
	z1 = z + (1 << k)
	z2 = z
	k -= 1
	for j in range(k, 0, -1):
		m = 1 << j
		add = arr[j - 1]
		if r & m != 0:
			p = z1 + add
			if (x & p) * (y & p) > ans1 or (ans1 == (x & p) * (y & p) and p < p1):
				ans1 = (x & p) * (y & p)
				p1 = p
			z1 += m
		if l & m == 0 and ans & m != 0:
			p = z2 + m + add
			if (x & p) * (y & p) > ans2 or ((x & p) * (y & p) == ans2 and p < p2):
				ans2 = (x & p) * (y & p)
				p2 = p
		elif l & m != 0:
			z2 += m
	add = 0
	m = 1
	if r & m != 0:
		p = z1 + add
		if (x & p) * (y & p) > ans1 or (ans1 == (x & p) * (y & p) and p < p1):
			ans1 = (x & p) * (y & p)
			p1 = p
	if l & m == 0 and ans & 1 != 0:
		p = z2 + m + add
		if (x & p) * (y & p) > ans2 or ((x & p) * (y & p) == ans2 and p < p2):
			ans2 = (x & p) * (y & p)
			p2 = p
	if p1 == p2:
		print(p1)
	elif ans1 > ans2:
		print(p1)
	elif ans1 < ans2:
		print(p2)
	else:
		print(min(p1, p2))


def rangingl(lmin, ans):
	global l
	if lmin & lmin - 1 == 0:
		x = lmin | ans
		return x
	else:
		y = rangingl(lmin & lmin - 1, ans)
		if y >= l:
			return y
		return lmin | ans

def func(x, y, z):
	return (x & z) * (y & z)
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().strip().split())
	q = x | y
	if x == 0 or y == 0:
		print(l)
	elif q >= l and q <= r:
		print(q)
	else:
		maxi = func(x, y, r)
		ans = r
		z = r
		while z:
			temp = z - 1
			if temp < l:
				break
			if func(x, y, temp) >= maxi:
				maxi = func(x, y, temp)
				ans = temp
			z = z & z - 1
		if func(x, y, l) >= maxi:
			maxi = func(x, y, l)
			ans = l
		s = ans & q
		if s >= l:
			print(s)
		else:
			x = rangingl(l, s)
			print(x)


import math
t = int(input())
for _ in range(t):
	x = input().split()
	list1 = [int(i) for i in x]
	a = list1[0]
	b = list1[1]
	l = list1[2]
	r = list1[3]
	if r == 0:
		print(0)
	else:
		list2 = []
		flag = False
		x = math.floor(math.log2(r))
		for i in range(x, -1, -1):
			x0 = 1 << i
			r1 = (r & x0) >> i
			if r1 == 1:
				l1 = (l & x0) >> i
				if r1 == l1 and flag == False:
					continue
				else:
					flag = True
					ans = r & ~x0 | x0 - 1
					list2.append(ans)
		list2.extend([l, r])
		list2.sort()
		ans = l
		for i in list2:
			if (a & i) * (b & i) > (a & ans) * (b & ans):
				ans = i
		if ans == 0:
			print(0)
		else:
			x = math.floor(math.log2(ans))
			for i in range(x, -1, -1):
				x0 = 1 << i
				ans0 = (ans & x0) >> i
				if ans0 == 1:
					a0 = (a & x0) >> i
					b0 = (b & x0) >> i
					if a0 == 0 and b0 == 0:
						ans1 = ans & ~x0
						if ans1 >= l:
							ans = ans1
						else:
							continue
			print(ans)


def F(X, Y, Z):
	return (X & Z) * (Y & Z)

def gettingabovel(lm, s):
	global l
	if lm & lm - 1 == 0:
		x = lm | s
		return x
	else:
		y = gettingabovel(lm & lm - 1, s)
		if y >= l:
			return y
		return lm | s
for _ in range(int(input())):
	(x, y, l, r) = list(map(int, input().split()))
	if not x or not y:
		print(l)
		continue
	q = x | y
	if q in range(l, r + 1):
		print(q)
		continue
	(m, ans, z) = (F(x, y, r), r, r)
	while z:
		temp = z - 1
		if temp < l:
			break
		if F(x, y, temp) >= m:
			m = F(x, y, temp)
			ans = temp
		z = z & z - 1
	if F(x, y, l) >= m:
		ans = l
	s = ans & q
	if s >= l:
		print(s)
		continue
	x = gettingabovel(l, s)
	print(x)


def F(X, Y, Z):
	return (X & Z) * (Y & Z)

def solve(X, Y, L, R):
	Z = 0
	bits_equal = True
	possible_zs = [L, R]
	for bit in range(62, -1, -1):
		mask = 1 << bit
		if bits_equal and L & mask == R & mask:
			Z |= R & mask
			continue
		if bits_equal:
			bits_equal = False
		if R & mask:
			possible_zs.append(Z | mask - 1)
		Z |= R & mask
	(_, _, Z) = max(((F(X, Y, z), -z, z) for z in possible_zs))
	for bit in range(62, -1, -1):
		mask = 1 << bit
		if Z & mask == 0:
			continue
		if X & mask == 0 and Y & mask == 0 and (Z & ~mask >= L):
			Z &= ~mask
	return Z

def main():
	import sys
	T = int(sys.stdin.readline())
	for _ in range(T):
		(X, Y, L, R) = (int(i) for i in sys.stdin.readline().split())
		print(solve(X, Y, L, R))
main()


def calF(zIn):
	return (x & zIn) * (y & zIn)

def makeEql(str1, dis):
	toAdd = ''
	for _ in range(dis):
		toAdd += '0'
	str1 = toAdd + str1
	return str1

def calZ(length, dis, ch, curList, binZ):
	prev = 0
	pz = l
	az = r
	if lenZ < length:
		binZ = makeEql(binZ, dis)
		dis = 0
	for index in indexContainer:
		newList = curList.copy()
		newList[index] = ch
		for j in range(index + 1, length):
			newList[j] = binZ[j + dis]
		z = int(''.join(newList), 2)
		cur = calF(z)
		if cur >= prev:
			if cur == prev:
				if z < az:
					pz = z
					az = z
			else:
				prev = cur
				pz = z
				az = z
	cur = calF(l)
	if cur >= prev:
		prev = cur
		pz = l
	cur = calF(r)
	if cur > prev:
		prev = cur
		pz = r
	return pz
t = int(input())
for _ in range(t):
	(x, y, l, r) = map(int, input().split())
	if l == r:
		resZ = l
	elif min(x, y) == 0:
		resZ = l
	else:
		bestZ = x | y
		flag = 1
		if bestZ <= r and bestZ >= l:
			resZ = bestZ
			flag = 0
		binR = bin(r).replace('0b', '')
		binZ = bin(bestZ).replace('0b', '')
		lenR = len(binR)
		lenZ = len(binZ)
		listR = list(binR)
		diff = abs(lenZ - lenR)
		if l == 0 and flag:
			indexContainer = []
			for i in range(lenR):
				if listR[i] == '1':
					indexContainer.append(i)
			resZ = calZ(lenR, diff, '0', listR, binZ)
		elif flag:
			binL = bin(l).replace('0b', '')
			lenL = len(binL)
			difRL = lenR - lenL
			binL = makeEql(binL, difRL)
			lenL = lenR
			for i in range(lenL):
				if binL[i] != binR[i]:
					break
			indexContainer = []
			for m in range(i, lenR):
				if listR[m] == '1':
					indexContainer.append(m)
			resZ1 = calZ(lenR, diff, '0', listR, binZ)
			if resZ1 <= r and resZ1 >= l:
				resZ = resZ1
			else:
				listL = list(binL)
				difL = abs(lenZ - lenL)
				indexContainer = []
				for k in range(i + 1, lenL):
					if listL[k] == '0':
						indexContainer.append(k)
				resZ = calZ(lenL, difL, '1', listL, binZ)
	print(resZ)


for _ in range(int(input())):
	(x, y, l, r) = list(map(int, input().split()))
	if l == r:
		print(l)
		continue
	xb = bin(x).replace('0b', '')
	yb = bin(y).replace('0b', '')
	rb = bin(r).replace('0b', '')
	lb = bin(l).replace('0b', '')
	(t_1, t_2, poss) = ('', '', [])
	maxLen = max(len(xb), len(yb), len(rb), len(lb))
	xb = '0' * (maxLen - len(xb)) + xb
	yb = '0' * (maxLen - len(yb)) + yb
	rb = '0' * (maxLen - len(rb)) + rb
	lb = '0' * (maxLen - len(lb)) + lb
	right = ''
	for i in range(maxLen):
		if xb[i] == '1' or yb[i] == '1':
			right = right + '1'
		else:
			right = right + '0'
	for i in range(maxLen):
		if lb[i] != rb[i]:
			ChangeInd = i
			break
	j = ChangeInd + 1
	checkZgreaterThanL = False
	t_1 = rb[:ChangeInd]
	tempRor = ''
	while j < maxLen:
		if checkZgreaterThanL == False:
			if lb[j] == '1':
				tempRor += '1'
			elif xb[j] == '1' or yb[j] == '1':
				tempRor += '1'
				checkZgreaterThanL = True
			else:
				tempRor += '0'
		elif xb[j] == '1' or yb[j] == '1':
			tempRor += '1'
		else:
			tempRor += '0'
		j += 1
	t_2 = t_1 + '0' + tempRor
	poss.append(t_2)
	t_1 = t_1 + '1'
	for i in range(ChangeInd + 1, maxLen - 1):
		Ror = ''
		if rb[i] == '1':
			if i + 1 < maxLen:
				Ror = right[i + 1:]
			t_2 = t_1 + '0' + Ror
			poss.append(t_2)
			t_1 += '1'
		else:
			t_1 += '0'
	poss.append(rb[:-1] + '0')
	poss.append(rb)
	poss.append(lb)
	maxVal = -1
	z = r + 1
	pOnce = {}
	for i in poss:
		pOnce[int(i, 2)] = True
	for i in pOnce:
		fVal = (x & i) * (y & i)
		if fVal > maxVal:
			z = i
			maxVal = fVal
		elif fVal == maxVal:
			z = min(i, z)
			maxVal = fVal
	if maxVal == 0:
		z = l
	print(z)


def recurse(lmin, ans):
	global l
	if lmin & lmin - 1 == 0:
		x = lmin | ans
		return x
	else:
		y = recurse(lmin & lmin - 1, ans)
		if y >= l:
			return y
		return lmin | ans

def f(x, y, z):
	return (x & z) * (y & z)
t = int(input())
for _ in range(t):
	(x, y, l, r) = map(int, input().strip().split())
	bor = x | y
	if x == 0 or y == 0:
		print(l)
	elif l <= bor and r >= bor:
		print(bor)
	else:
		maxi = f(x, y, r)
		ans = r
		z = r
		while z:
			t = z - 1
			if t < l:
				break
			if f(x, y, t) >= maxi:
				maxi = f(x, y, t)
				ans = t
			z = z & z - 1
		if f(x, y, l) >= maxi:
			maxi = f(x, y, l)
			ans = l
		s = ans & bor
		if s >= l:
			print(s)
		else:
			x = recurse(l, s)
			print(x)


def duvidha(binary, ini):
	for i in range(ini, len(binary)):
		a1 = 2 ** (len(binary) - i - 1)
		if int(binary, 2) + a1 <= l or (i == len(binary) - 1 and (binary_x[i] == '1' or binary_y[i] == '1') and (int(binary, 2) + a1 <= r)):
			binary = binary[:i] + '1' + binary[i + 1:]
		elif binary_x[i] == '1' and binary_y[i] == '1':
			if int(binary, 2) + a1 <= r:
				binary = binary[:i] + '1' + binary[i + 1:]
		elif binary_x[i] == '1' or binary_y[i] == '1':
			if int(binary, 2) + a1 <= r:
				binary1_x = binary_x[i + 1:]
				binary1_y = binary_y[i + 1:]
				a = int(binary1_x, 2) | int(binary1_y, 2)
				if int(binary, 2) + a1 + a <= r:
					binary = binary[:i] + '1' + binary[i + 1:]
					binary = int(binary, 2) | a
					binary = bin(binary)
					binary = binary[2:]
					break
				else:
					b = int(binary1_x, 2) | int(binary1_y, 2)
					b = b | int(binary, 2)
					binary = binary[:i] + '1' + binary[i + 1:]
					binary = duvidha(binary, i + 1)
					binary1 = bin(b)
					binary1 = binary1[2:]
					binary1 = '0' * (len(binary_x) - len(binary1)) + binary1
					if (int(binary1, 2) & x) * (int(binary1, 2) & y) >= (int(binary, 2) & x) * (int(binary, 2) & y):
						binary = binary1
					break
	return binary

def less_than(binary, l):
	diff = l - int(binary, 2)
	for i in range(len(binary)):
		if binary[i] == '0':
			a1 = 2 ** (len(binary) - i - 1)
			if binary[i] == '0':
				if i == len(binary) - 1:
					binary = binary[:i] + '1'
				else:
					temp = binary[i + 1:]
					temp = ''.join(('1' if x == '0' else '0' for x in temp))
					if int(temp, 2) < diff:
						binary = binary[:i] + '1' + binary[i + 1:]
						diff = diff - a1
						if diff <= 0:
							break
	return binary
t = int(input())
for i in range(t):
	(x, y, l, r) = [int(x) for x in input().split()]
	q = x | y
	if q >= l and q <= r:
		if q & x == 0 or q & y == 0:
			print(l)
		else:
			print(q)
	else:
		binary_r = bin(r)
		binary_r = binary_r[2:]
		binary_x = bin(x)
		binary_x = binary_x[2:]
		binary_y = bin(y)
		binary_y = binary_y[2:]
		binary = '0' * len(binary_r)
		if len(binary_x) > len(binary_r):
			binary_x = binary_x[len(binary_x) - len(binary_r):]
		else:
			mi = len(binary_r) - len(binary_x)
			binary_x = '0' * mi + binary_x
		if len(binary_y) > len(binary_r):
			binary_y = binary_y[len(binary_y) - len(binary_r):]
		else:
			mi = len(binary_r) - len(binary_y)
			binary_y = '0' * mi + binary_y
		binary = duvidha(binary, 0)
		if int(binary, 2) < l:
			binary = less_than(binary, l)
		if int(binary, 2) & x == 0 or int(binary, 2) & y == 0:
			print(l)
		else:
			print(int(binary, 2))


import sys
import math
from collections import Counter
from collections import deque as deq
from functools import lru_cache
INT_MAX = float('inf')
INT_MIN = float('-inf')
input = sys.stdin.readline

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

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
while t:
	a = inlt()
	(x, y, l, r) = a
	zs = []
	curr = 0
	eq = True
	for i in range(62, -1, -1):
		p = 1 << i
		if l & p == r & p and eq:
			curr += r & p
			continue
		if eq:
			eq = not eq
		if r & p:
			zs.append(curr + p - 1)
		curr += r & p
	zs.append(r)
	zs.append(l)
	zs.sort()
	ans = zs[0]
	for z in zs:
		if (x & z) * (y & z) > (x & ans) * (y & ans):
			ans = z
	for i in range(62, -1, -1):
		p = 1 << i
		if ans & p == 0:
			continue
		if x & p == 0 and y & p == 0 and (ans - p >= l):
			ans -= p
	print(ans)
	t -= 1


import math, ctypes
bit = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808]

def turnK(n, k):
	if k <= 0:
		return n
	return n & ~(1 << k)
tc = int(input())
for _ in range(tc):
	(x, y, l, r) = map(int, input().split())
	if x == 0 or y == 0:
		print(l)
		continue
	if l == r:
		print(l)
		continue
	m = (x & r) * (y & r)
	z = r
	t = 1
	new_r = r
	flag = 1
	i = 63
	while i >= 0:
		if r & bit[i]:
			d = bit[i] - t
			a = (r ^ d) & d
			b = turnK(r, i) | a
			if (x & b) * (y & b) > m and b >= l:
				m = (x & b) * (y & b)
				z = b
		new_r = ctypes.c_ulong(new_r - bit[i]).value
		if new_r != 0:
			i = int(math.log(new_r, 2))
		else:
			i = -2147483648
	i = 63
	while i >= 0:
		if z & bit[i]:
			x2 = bit[i] - t
			a2 = (z ^ x2) & x2
			pp = turnK(z, i) | a2
			if pp >= l and (x & pp) * (y & pp) >= m:
				z = pp
		i -= 1
	if (x & z) * (y & z) == 0:
		print(l)
	else:
		t1 = (x & z - 1) * (y & z - 1)
		if t1 == m and z - 1 >= l:
			print(z - 1)
		else:
			print(z)


import math
from sys import stdin

def clearBits(num, bits):
	num &= ~((1 << bits) - 1)
	return num

def reducer2(z, cur, l):
	m = int(math.log(cur, 2))
	while m >= 0:
		if z >> m & 1 == 0 and cur >> m & 1:
			if cur - (1 << m) >= l:
				cur -= 1 << m
		m -= 1
	return cur

def solver(x, y, l, r):
	if x == 0 or y == 0 or r == 0:
		return l
	elif l == r:
		return l
	else:
		z = x | y
		if l <= z <= r:
			return z
		m = int(math.log(r, 2))
		max_set_bit = m
		maxi = (x & r) * (y & r)
		cur = r
		while max_set_bit >= 0:
			if r >> max_set_bit & 1:
				t1 = (1 << max_set_bit) - 1
				t2 = clearBits(r, max_set_bit + 1) | t1
				if (x & t2) * (y & t2) > maxi and t2 >= l:
					maxi = (x & t2) * (y & t2)
					cur = t2
				elif (x & t2) * (y & t2) == maxi and t2 >= l:
					cur = min(t2, cur)
			max_set_bit -= 1
		if (x & cur) * (y & cur) == 0:
			return l
		else:
			m = reducer2(z, cur, l)
			return m
test = int(input())
for _ in range(test):
	(x, y, l, r) = map(int, input().split())
	print(solver(x, y, l, r))


power = [1]
for i in range(1, 42):
	power.append(2 * power[i - 1])

def makebin(n):
	llist = [0] * 42
	i = 41
	temp = n
	while temp != 0:
		llist[i] = temp % 2
		temp = temp // 2
		i = i - 1
	return llist

def recurse(ans, index, x, y, l, r, careL, careR):
	if index == 41:
		t = ans
		p = ans + power[41 - index]
		if p > R:
			return t
		elif t < L:
			return p
		else:
			z1 = (X & t) * (Y & t)
			z2 = (X & p) * (Y & p)
			if z1 == z2:
				return min(t, p)
			elif z1 > z2:
				return t
			else:
				return p
	elif careL == 0 and careR == 1:
		if x[index] == 0 and y[index] == 0:
			if r[index] == 1:
				return recurse(ans, index + 1, x, y, l, r, 0, 0)
			else:
				return recurse(ans, index + 1, x, y, l, r, 0, 1)
		elif x[index] == 1 and y[index] == 1:
			if r[index] == 1:
				ans = ans + power[41 - index]
				return recurse(ans, index + 1, x, y, l, r, 0, 1)
			else:
				return recurse(ans, index + 1, x, y, l, r, 0, 1)
		elif r[index] == 0:
			return recurse(ans, index + 1, x, y, l, r, 0, 1)
		else:
			t = recurse(ans + power[41 - index], index + 1, x, y, l, r, 0, 1)
			p = recurse(ans, index + 1, x, y, l, r, 0, 0)
			z1 = (X & t) * (Y & t)
			z2 = (X & p) * (Y & p)
			if z1 == z2:
				return min(t, p)
			elif z1 > z2:
				return t
			else:
				return p
	elif careL == 1 and careR == 0:
		if x[index] == 1 and y[index] == 1:
			ans = ans + power[41 - index]
			if l[index] == 1:
				return recurse(ans, index + 1, x, y, l, r, 1, 0)
			else:
				return recurse(ans, index + 1, x, y, l, r, 0, 0)
		elif x[index] == 0 and y[index] == 0:
			if l[index] == 0:
				ans = ans
				return recurse(ans, index + 1, x, y, l, r, 1, 0)
			else:
				ans = ans + power[41 - index]
				return recurse(ans, index + 1, x, y, l, r, 1, 0)
		elif l[index] == 1:
			ans = ans + power[41 - index]
			return recurse(ans, index + 1, x, y, l, r, 1, 0)
		else:
			t = recurse(ans + power[41 - index], index + 1, x, y, l, r, 0, 0)
			p = recurse(ans, index + 1, x, y, l, r, 1, 0)
			z1 = (X & t) * (Y & t)
			z2 = (X & p) * (Y & p)
			if z1 == z2:
				return min(t, p)
			elif z1 > z2:
				return t
			else:
				return p
	elif careL == 1 and careR == 1 and (r[index] == 0) and (l[index] == 0):
		return recurse(ans, index + 1, x, y, l, r, 1, 1)
	elif careL == 1 and careR == 1 and (r[index] == 1) and (l[index] == 1):
		ans = ans + power[41 - index]
		return recurse(ans, index + 1, x, y, l, r, 1, 1)
	elif careL == 1 and careR == 1 and (r[index] == 1) and (l[index] == 0):
		if x[index] == 1 and y[index] == 1:
			ans = ans + power[41 - index]
			return recurse(ans, index + 1, x, y, l, r, 0, 1)
		elif x[index] == 0 and y[index] == 0:
			return recurse(ans, index + 1, x, y, l, r, 1, 0)
		else:
			t = recurse(ans + power[41 - index], index + 1, x, y, l, r, 0, 1)
			p = recurse(ans, index + 1, x, y, l, r, 1, 0)
			z1 = (X & t) * (Y & t)
			z2 = (X & p) * (Y & p)
			if z1 == z2:
				return min(t, p)
			elif z1 > z2:
				return t
			else:
				return p
	elif x[index] == 1 and y[index] == 1:
		return recurse(ans + power[41 - index], index + 1, x, y, l, r, 0, 0)
	elif x[index] == 0 and y[index] == 0:
		return recurse(ans, index + 1, x, y, l, r, 0, 0)
	else:
		return recurse(ans + power[41 - index], index + 1, x, y, l, r, 0, 0)
for _ in range(int(input())):
	(X, Y, L, R) = list(map(int, input().split()))
	a = X | Y
	if X == 0 or Y == 0 or L == R:
		print(L)
	elif a >= L and a <= R:
		print(a)
	else:
		x = makebin(X)
		y = makebin(Y)
		l = makebin(L)
		r = makebin(R)
		q = list(bin(R)[2:])
		careL = 1
		careR = 1
		print(recurse(0, 41 - len(q), x, y, l, r, careL, careR))


def decimalToBinary(n):
	return bin(n).replace('0b', '')
from sys import stdin
t = int(stdin.readline())
for test in range(t):
	(X, Y, L, R) = map(int, stdin.readline().split())
	maxi = str(decimalToBinary(X | Y))
	binL = str(decimalToBinary(L))
	binR = str(decimalToBinary(R))
	maxLen = max(len(maxi), len(binL), len(binR))
	maxi0 = maxi.zfill(maxLen)
	binL0 = binL.zfill(maxLen)
	binR0 = binR.zfill(maxLen)
	maxi0Copy = maxi0
	binL0Copy = binL0
	binR0Copy = binR0
	listR = []
	if X == 0 or Y == 0:
		print(L)
		continue
	elif L == 0:
		for i in range(len(binR0)):
			if binR0Copy[i] == '1':
				binary = '0b' + binR0[0:i] + '0' + maxi0[i + 1:]
				if L <= int(binary, 2) <= R:
					listR.append(int(binary, 2))
	elif L > 0 and R > 0:
		for i in range(len(binR0)):
			if binR0Copy[i] == '1':
				binary = '0b' + binR0[0:i] + '0' + maxi0[i + 1:]
				if L <= int(binary, 2) <= R:
					listR.append(int(binary, 2))
		for i in range(len(binL0)):
			if binL0Copy[i] == '1':
				binary = '0b' + binL0[0:i + 1] + maxi0[i + 1:]
				if L <= int(binary, 2) <= R:
					listR.append(int(binary, 2))
	if L not in listR:
		listR.append(L)
	if R not in listR:
		listR.append(R)
	if L <= X | Y <= R and X | Y not in listR:
		listR.append(X | Y)
	ele = {}
	for i in range(len(listR)):
		key = (X & listR[i]) * (Y & listR[i])
		if key in ele:
			ele[key].append(listR[i])
		else:
			ele[key] = [listR[i]]
	maximum = 0
	for i in ele:
		if i > maximum:
			maximum = i
	print(min(ele[maximum]))


import math
t = int(input())
for _ in range(t):
	(x, y, l, r) = map(int, input().split())
	if 1:
		if 1:
			str_r = bin(r).replace('0b', '')
			n = len(str_r)
			mx = (x & r) * (y & r)
			finalAns = r
			list_ans = []
			list_ans.append(r)
			for i in range(n - 1, -1, -1):
				if str_r[i] == '1':
					str_temp = str_r[:i] + '0' + '1' * (n - i - 1)
					temp = int(str_temp, 2)
					if temp > r or temp < l:
						continue
					val = (x & temp) * (y & temp)
					if val == mx:
						list_ans.append(temp)
						mx = val
					elif val > mx:
						list_ans = []
						list_ans.append(temp)
						mx = val
			finalAns = min(list_ans)
			if mx == (x & l) * (y & l):
				finalAns = l
				print(finalAns)
				continue
			str_ans = bin(finalAns).replace('0b', '')
			str_x = bin(x).replace('0b', '')
			diff = len(str_x) - len(str_r)
			if diff > 0:
				str_x = str_x[diff:]
			elif diff < 0:
				str_x = '0' * abs(diff) + str_x
			str_y = bin(y).replace('0b', '')
			diff = len(str_y) - len(str_r)
			if diff > 0:
				str_y = str_y[diff:]
			elif diff < 0:
				str_y = '0' * abs(diff) + str_y
			diff = len(str_ans) - len(str_r)
			if diff > 0:
				str_ans = str_ans[diff:]
			elif diff < 0:
				str_ans = '0' * abs(diff) + str_ans
			for i in range(n):
				if str_x[i] == '0' and str_y[i] == '0':
					temp_str_ans = str_ans[:i] + '0' + str_ans[i + 1:]
					if int(temp_str_ans, 2) >= l:
						str_ans = temp_str_ans
			finalAns = int(str_ans, 2)
			if int(str_x, 2) == 0 or int(str_y, 2) == 0:
				finalAns = l
			print(finalAns)


def process(v, l, x, y, m):
	p = 50
	while p >= 0:
		if v >> p & 1 == 1:
			k = v ^ 1 << p
			if (k & x) * (k & y) == m and k >= l:
				v = k
		p -= 1
	return v
t = int(input())
while t > 0:
	inp = input()
	inp = inp.split()
	x = int(inp[0])
	y = int(inp[1])
	l = int(inp[2])
	r = int(inp[3]) + 1
	ans = set()
	p = 50
	curr = 0
	while p >= 0 and r >> p & 1 == l >> p & 1:
		if r >> p & 1 == 1:
			curr = curr | 1 << p
		p -= 1
	while p >= 0:
		if r >> p & 1 == 1:
			ans.add(curr + ((1 << p) - 1))
			curr = curr | 1 << p
		p -= 1
	m = 0
	for v in ans:
		m = max(m, (v & x) * (v & y))
	res = r
	for v in ans:
		if (v & x) * (v & y) == m:
			res = min(res, process(v, l, x, y, m))
	print(res)
	t -= 1


import sys
import numpy as np

def find_min(x2, x1, r, l, i):
	if int(x1, 2) <= int(r, 2) and int(x1, 2) >= int(l, 2):
		return x1
	if int(x1, 2) > int(r, 2):
		v1 = x1[:i] + '0' + '0' * (len(x1) - i - 1)
		v2 = x1[:i] + '1' + '0' * (len(x1) - i - 1)
		if int(v1, 2) > int(r, 2):
			v1 = False
		if int(v2, 2) > int(r, 2):
			v2 = False
	if int(x1, 2) < int(l, 2):
		v1 = x1[:i] + '0' + '1' * (len(x1) - i - 1)
		v2 = x1[:i] + '1' + '1' * (len(x1) - i - 1)
		if int(v1, 2) < int(l, 2):
			v1 = False
		if int(v2, 2) < int(l, 2):
			v2 = False
	if not v1 and (not v2):
		return '0' * len(x1)
	elif not v1:
		temp2 = find_min(x2, x1[:i] + '1' + x1[i + 1:], r, l, i + 1)
		return temp2
	elif not v2:
		temp1 = find_min(x2, x1[:i] + '0' + x1[i + 1:], r, l, i + 1)
		return temp1
	if x2[i] == '1':
		temp2 = find_min(x2, x1[:i] + '1' + x1[i + 1:], r, l, i + 1)
		return temp2
	temp1 = find_min(x2, x1[:i] + '0' + x1[i + 1:], r, l, i + 1)
	temp2 = find_min(x2, x1[:i] + '1' + x1[i + 1:], r, l, i + 1)
	atemp1 = int(temp1, 2)
	btemp2 = int(temp2, 2)
	a1 = x & atemp1
	a2 = y & atemp1
	b1 = x & btemp2
	b2 = y & btemp2
	a3 = np.multiply(a1, a2)
	b3 = np.multiply(b1, b2)
	done.append(0)
	if a3 > b3:
		return temp1
	elif b3 > a3:
		return temp2
	elif atemp1 > btemp2:
		return temp2
	else:
		return temp1
	if i == len(x1) - 1:
		return '0' * len(x1)
	return find_min(x2, x1, r, l, i + 1)
t = int(sys.stdin.readline())
for _ in range(t):
	(x, y, l, r) = map(int, sys.stdin.readline().split())
	maxi = 0
	value = 0
	done = []
	if x == 0 or y == 0:
		value = l
	else:
		m = x | y
		m1 = x & y
		if m >= l and m <= r:
			value = m
		elif m >= l:
			brm = format(m, 'b')
			brm1 = format(m1, 'b')
			brr = format(r, 'b')
			brl = format(l, 'b')
			if len(brm) > len(brm1):
				abc = '0' * (len(brm) - len(brm1))
				brm1 = abc + brm1
			brm = brm[len(brm) - len(brr):]
			brm1 = brm1[len(brm1) - len(brr):]
			if len(brr) > len(brl):
				abc = '0' * (len(brr) - len(brl))
				brl = abc + brl
			brm1 = find_min(brm1, brm, brr, brl, 0)
			value = int(brm1, 2)
		elif m < l:
			brm = format(m, 'b')
			brm1 = format(m1, 'b')
			brr = format(r, 'b')
			brl = format(l, 'b')
			if len(brr) > len(brl):
				abc = '0' * (len(brr) - len(brl))
				brl = abc + brl
				brl = brl[len(brl) - len(brr):]
			if len(brr) > len(brm):
				abc = '0' * (len(brr) - len(brm))
				brm = abc + brm
				brm = brm[len(brm) - len(brr):]
			if len(brr) > len(brm1):
				abc = '0' * (len(brr) - len(brm1))
				brm1 = abc + brm1
				brm1 = brm1[len(brm1) - len(brr):]
			value = int(find_min(brm1, brm, brr, brl, 0), 2)
	if x & value == 0 or y & value == 0:
		value = l
	sys.stdout.write(str(value) + '\n')


t = int(input())
for i in range(t):
	(x, y, l, r) = map(int, input().split(' '))
	if l > r:
		t = l
		l = r
		r = t
	a = x | y
	b = bin(a)[2:]
	c = bin(l)[2:]
	r1 = bin(r)[2:]
	l1 = bin(l)[2:]
	st = ''
	st1 = ''
	if len(r1) < len(b):
		for k in range(len(b) - len(r1)):
			st = st + '0'
		r1 = st + r1
	if len(l1) < len(b):
		for k in range(len(b) - len(l1)):
			st1 = st1 + '0'
		l1 = st1 + l1
	st1 = ''
	if len(b) < len(l1):
		for k in range(len(l1) - len(b)):
			st1 = st1 + '0'
	b = st1 + b
	fix = list(r1)
	fix2 = list(l1)
	s = list()
	if x == 0 or y == 0:
		print(l)
	else:
		if a <= r and a >= l:
			s.append(a)
		for j in range(0, len(r1)):
			if fix[j] == '0':
				pass
			else:
				binary = '0b' + r1[0:j] + '0' + b[j + 1:]
				if int(binary, 2) >= l and int(binary, 2) <= r:
					s.append(int(binary, 2))
		s.append(r)
		s.append(l)
		for j in range(0, len(l1)):
			if fix2[j] == '0':
				pass
			else:
				binary = '0b' + l1[0:j] + '1' + b[j + 1:]
				if int(binary, 2) >= l and int(binary, 2) <= r:
					s.append(int(binary, 2))
		for j in range(len(l1) - 1, -1, -1):
			if fix2[j] == '0':
				pass
			else:
				binary = '0b' + b[0:j] + '1' + l1[j + 1:]
				if int(binary, 2) >= l and int(binary, 2) <= r:
					s.append(int(binary, 2))
		s = set(s)
		d = {}
		s1 = 0
		for k in s:
			s1 = (x & k) * (y & k)
			if s1 in d:
				d[s1].append(k)
			else:
				d[s1] = [k]
		c = sorted(d)
		h = c[-1]
		print(min(d[h]))


def initial(x, y, l, r):
	ton = l
	isan = 0
	for i in reversed(range(41)):
		flag = 1 << i & r
		if flag >> i == 0:
			continue
		mtan = (1 << i) - 1 | r - (1 << i)
		use = (x & mtan) * (y & mtan)
		if use > isan and mtan >= l:
			isan = use
			ton = mtan
	here = (x & r) * (y & r)
	if here > isan:
		ton = r
	return ton

def final(x, y, l, r, ton):
	left = 0
	temp = x | y
	for i in reversed(range(41)):
		flag = 1 << i
		if (temp & flag) >> i == 1:
			continue
		if ton - flag < l:
			continue
		if (ton & flag) >> i == 0:
			continue
		ton -= flag
	for i in reversed(range(41)):
		left = (x & ton) * (y * ton)
		if left > r or left < l:
			left = 0
		else:
			continue
	return ton
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	if x != 0 and y != 0:
		ton = initial(x, y, l, r)
		ton = final(x, y, l, r, ton)
		print(ton)
	else:
		print(l)


def chk(oppo):
	return (x & oppo) * (y & oppo)
t = int(input())
for _ in range(t):
	(x, y, l, r) = map(int, input().split())
	possible_ans = x | y
	lbin = bin(l)[2:]
	rbin = bin(r)[2:]
	lbin = '0' * (len(rbin) - len(lbin)) + lbin
	if possible_ans > r:
		possible_ans = possible_ans & 2 ** len(rbin) - 1
	pbin = bin(possible_ans)[2:]
	pbin = '0' * (len(rbin) - len(pbin)) + pbin
	i = 0
	cur = 2 ** (len(rbin) - 1)
	ans = 0
	while i < len(rbin) and lbin[i] == rbin[i]:
		ans = ans + cur if lbin[i] == '1' else ans
		i += 1
		cur = cur >> 1
	i += 1
	temp = (cur, i)
	ansa = ans
	cur = cur >> 1
	while i < len(rbin):
		if lbin[i] == '1' and pbin[i] == '1':
			ansa += cur
		elif lbin[i] == '1' and pbin[i] == '0':
			ansa += cur
		elif lbin[i] == '0' and pbin[i] == '1':
			while i < len(rbin):
				if pbin[i] == '1':
					ansa += cur
				i += 1
				cur = cur >> 1
		i += 1
		cur = cur >> 1
	(cur, i) = temp
	ansb = ans + cur
	cur = cur >> 1
	while i < len(rbin):
		if rbin[i] == '1' and pbin[i] == '0':
			i += 1
			cur = cur >> 1
			while i < len(rbin):
				if pbin[i] == '1':
					ansb += cur
				i += 1
				cur = cur >> 1
		elif rbin[i] == '1' and pbin[i] == '1':
			ansb += cur
		i += 1
		cur = cur >> 1
	anss = [ansb]
	(cur, i) = temp
	ansc = ans + cur
	cur = cur >> 1
	while i < len(rbin):
		if rbin[i] == '1' and pbin[i] == '1' and (chk(ansc + (cur - 1 & possible_ans)) >= chk(ansb)):
			anss.append(ansc + (cur - 1 & possible_ans))
		if rbin[i] == '1' and pbin[i] == '0':
			i += 1
			cur = cur >> 1
			while i < len(rbin):
				if pbin[i] == '1':
					ansc += cur
				i += 1
				cur = cur >> 1
		elif rbin[i] == '1' and pbin[i] == '1':
			ansc += cur
		i += 1
		cur = cur >> 1
	anss.append(ansa)
	anss.append(possible_ans & 2 ** len(rbin) - 1)
	anss = [l] + anss
	anss.append(r)
	anss.append(ans)
	yo = anss[0]
	yoop = chk(anss[0])
	for ea in anss:
		zc = chk(ea)
		if l <= ea <= r and (zc == yoop and ea < yo or zc > yoop):
			(yo, yoop) = (ea, zc)
	print(yo)


def solve(x, y, l, r, z, bit):
	least_reduction = 2 ** 62
	best_bit = -1
	right = z - r
	left = l - z
	if left <= 0 and right <= 0:
		return z
	while bit > right:
		if left > bit:
			bit >>= 1
			continue
		else:
			reduction = (x & bit) * (y & ~bit) + (x & ~bit) * (y & bit) + (x & bit) * (y & bit)
			if reduction < least_reduction and z & ~bit >= l:
				least_reduction = reduction
				best_bit = bit
		bit >>= 1
	old_z = z & ~best_bit
	old_right = old_z - r
	old_left = l - old_z
	if old_left > 0 or old_right > 0:
		least_reduction = 2 ** 62
	new_z = z & ~bit
	new_z = solve(x & new_z, y & new_z, l, r, new_z, bit >> 1)
	new_reduction = (x & z) * (y & z) - (x & new_z) * (y & new_z)
	new_right = new_z - r
	new_left = l - new_z
	if new_left > 0 or new_right > 0:
		new_reduction = 2 ** 62
	if new_reduction < least_reduction:
		return new_z
	return old_z

def reduce_z(x, y, z, l, r):
	bit = 2 ** 40
	while bit > 0:
		if bit & (x | y) == 0 and z & ~bit >= l:
			z &= ~bit
		bit >>= 1
	return z

def testcase():
	(x, y, l, r) = map(int, input().split())
	z = 2 ** 40 - 1
	bit = 2 ** 40
	while bit > r:
		z &= ~bit
		bit >>= 1
	z = solve(x & z, y & z, l, r, z, bit)
	z = reduce_z(x, y, z, l, r)
	if (x & z) * (y & z) == 0:
		return l
	return z
t = int(input())
for _ in range(t):
	print(testcase())


import math
for _ in range(int(input())):
	(x, y, l, r) = input().split(' ')
	(x, y, l, r) = (int(x), int(y), int(l), int(r))
	xString = bin(x)[2:]
	yString = bin(y)[2:]
	bitsStore = 10 ** 13
	if x == 0 or y == 0:
		bitsStore = l
	elif l == 0 and r == 0:
		bitsStore = 0
	else:
		lengthM = max(int(math.log(x) / math.log(2)) + 1, int(math.log(y) / math.log(2)) + 1)
		lengthS = min(int(math.log(x) / math.log(2)) + 1, int(math.log(y) / math.log(2)) + 1)
		bits = int(math.log(r) / math.log(2)) + 1
		if x < y:
			temp = x
			x = y
			y = temp
			temp = xString
			xString = yString
			yString = temp
		bitsNum = 2 ** bits - 1
		maximum = 0
		if bitsNum >> 1 >= l:
			temp = bitsNum >> 1
			maximum = (x & temp) * (y & temp)
			bitsStore = bitsNum >> 1
		temp = 1 << bits - 1
		for i in range(bits - 2, -1, -1):
			gen = temp | bitsNum >> bits - i
			if gen < l:
				temp = temp | 1 << i
			if gen >= l and gen <= r:
				if maximum < (gen & x) * (gen & y):
					maximum = (gen & x) * (gen & y)
					bitsStore = gen
				elif maximum == (gen & x) * (gen & y):
					if gen < bitsStore:
						maximum = (gen & x) * (gen & y)
						bitsStore = gen
				temp = temp | 1 << i
		diff = lengthM - lengthS
		for i in range(diff):
			yString = '0' + yString
		bitsNew = int(math.log(bitsStore) / math.log(2)) + 1
		bitDiff = 40 - bitsNew
		tex = 1
		bex = 2 ** bitsNew - 1
		bex = bex >> 1
		kex = bex
		array = []
		for i in range(bitsNew - 1, -1, -1):
			array.append(kex)
			bex = bex >> 1
			kex = tex << i | bex
			tex = tex << 1 | 1
		for i in range(bitsNew):
			temp = bitsStore & array[i]
			if (y & temp) * (x & temp) == maximum and temp >= l and (temp <= r):
				bitsStore = temp
	print(bitsStore)


def findmax(careL, careR, answer, ind):
	if not careL and (not careR):
		ans = ''
		for i in range(ind, lenR):
			if binX[i] == '1' or binY[i] == '1':
				ans += '1'
			else:
				ans += '0'
		return ans
	elif careL and (not careR):
		if ind == lenR - 1:
			if binL[ind] == '0':
				s = ''
				for i in range(lenR):
					s += answer[i]
				addans = int(s, 2)
				X1 = 1 + addans
				X2 = 0 + addans
				if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
					return '1'
				else:
					return '0'
			else:
				return '1'
		elif binL[ind] == '1':
			answer[ind] = '1'
			return '1' + findmax(careL, careR, answer.copy(), ind + 1)
		elif binX[ind] == '1' and binY[ind] == '1':
			answer[ind] = '1'
			return '1' + findmax(False, careR, answer.copy(), ind + 1)
		elif binX[ind] == '0' and binY[ind] == '0':
			answer[ind] = '0'
			return '0' + findmax(careL, careR, answer.copy(), ind + 1)
		else:
			s = ''
			for i in range(lenR):
				s += answer[i]
			addans = int(s, 2)
			answer[ind] = '0'
			x2 = '0' + findmax(careL, False, answer.copy(), ind + 1)
			answer[ind] = '1'
			x1 = '1' + findmax(False, False, answer.copy(), ind + 1)
			X1 = int(x1, 2) + addans
			X2 = int(x2, 2) + addans
			if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
				return x1
			else:
				return x2
	elif not careL and careR:
		if ind == lenR - 1:
			if binR[ind] == '1':
				s = ''
				for i in range(lenR):
					s += answer[i]
				addans = int(s, 2)
				X1 = 1 + addans
				X2 = 0 + addans
				if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
					return '1'
				else:
					return '0'
			else:
				return '0'
		elif binR[ind] == '0':
			answer[ind] = '0'
			return '0' + findmax(careL, careR, answer.copy(), ind + 1)
		elif binX[ind] == '1' and binY[ind] == '1':
			answer[ind] = '1'
			return '1' + findmax(careL, careR, answer.copy(), ind + 1)
		elif binX[ind] == '0' and binY[ind] == '0':
			answer[ind] = '0'
			return '0' + findmax(careL, False, answer.copy(), ind + 1)
		else:
			s = ''
			for i in range(lenR):
				s += answer[i]
			addans = int(s, 2)
			answer[ind] = '0'
			x2 = '0' + findmax(careL, False, answer.copy(), ind + 1)
			answer[ind] = '1'
			x1 = '1' + findmax(False, careR, answer.copy(), ind + 1)
			X1 = int(x1, 2) + addans
			X2 = int(x2, 2) + addans
			if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
				return x1
			else:
				return x2
	elif ind == lenR - 1:
		if binL[ind] != binR[ind]:
			s = ''
			for i in range(lenR):
				s += answer[i]
			addans = int(s, 2)
			X1 = 1 + addans
			X2 = 0 + addans
			if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
				return '1'
			else:
				return '0'
		else:
			return binL[ind]
	elif binR[ind] == '1' and binL[ind] == '1':
		answer[ind] = '1'
		return '1' + findmax(careL, careR, answer.copy(), ind + 1)
	elif binR[ind] == '0' and binL[ind] == '0':
		answer[ind] = '0'
		return '0' + findmax(careL, careR, answer.copy(), ind + 1)
	elif binX[ind] == '1' and binY[ind] == '1':
		answer[ind] = '1'
		return '1' + findmax(False, careR, answer.copy(), ind + 1)
	elif binX[ind] == '0' and binY[ind] == '0':
		answer[ind] = '0'
		return '0' + findmax(careL, False, answer.copy(), ind + 1)
	else:
		s = ''
		for i in range(lenR):
			s += answer[i]
		addans = int(s, 2)
		answer[ind] = '0'
		x2 = '0' + findmax(careL, False, answer.copy(), ind + 1)
		answer[ind] = '1'
		x1 = '1' + findmax(False, careR, answer.copy(), ind + 1)
		X1 = int(x1, 2) + addans
		X2 = int(x2, 2) + addans
		if (X1 & Xo) * (X1 & Yo) > (X2 & Xo) * (X2 & Yo):
			return x1
		else:
			return x2
import math
for _ in range(int(input())):
	(X, Y, L, R) = map(int, input().split())
	if X == 0 or Y == 0 or L == R:
		print(L)
	else:
		ansor = X | Y
		if ansor <= R and ansor >= L:
			print(ansor)
		else:
			bR = bin(R)[2:]
			bL = bin(L)[2:]
			bX = bin(X)[2:]
			bY = bin(Y)[2:]
			global lenR
			lenR = len(bR)
			maxl = max(lenR, len(bX), len(bY))
			global binR
			binR = ('0' * (maxl - len(bR)) + bR)[-lenR::1]
			global binX
			binX = ('0' * (maxl - len(bX)) + bX)[-lenR::1]
			global binY
			binY = ('0' * (maxl - len(bY)) + bY)[-lenR::1]
			global binL
			binL = ('0' * (maxl - len(bL)) + bL)[-lenR::1]
			answer = ['0'] * lenR
			global Xo
			Xo = X
			global Yo
			Yo = Y
			ans = findmax(True, True, answer, 0)
			print(int(ans, 2))


try:
	for _ in range(int(input())):
		(x, y, l, r) = map(int, input().split())
		_or = x | y | l
		if 0 in [x, y, r]:
			print(l)
			continue
		if l == 0 and r > 2 * max(x, y):
			print(x | y)
			continue
		rBin = bin(r)[2:]
		lBin = bin(l)[2:]
		rLen = len(rBin)
		xLen = len(bin(x)[2:])
		yLen = len(bin(y)[2:])
		for i in range(rLen, max(xLen, yLen)):
			sb = 1 << i
			ub = ~sb
			x &= ub
			y &= ub
		if x == 0 or y == 0:
			print(l)
			continue
		_or = x | y | l
		binOr = bin(_or)[2:]
		bLen = len(binOr)
		dif = bLen - rLen
		if dif > 0:
			binOr = binOr[dif:]
		elif dif < 0:
			binOr = '0' * -dif + binOr
		_or = int(binOr, 2)
		if _or == 0:
			print(l)
			continue
		maxVal = -1
		ans = -1
		bLen = len(binOr)
		for i in range(bLen):
			z = int(binOr, 2)
			if z >= l and z <= r:
				if (x & z) * (y & z) > maxVal:
					maxVal = (x & z) * (y & z)
					ans = z
			temp = binOr
			binOr = binOr[:i] + '0' + binOr[i + 1:]
			z = int(binOr, 2)
			if z >= l and z <= r:
				if (x & z) * (y & z) > maxVal:
					maxVal = (x & z) * (y & z)
					ans = z
			if rBin[i] == '0':
				binOr = binOr[:i] + '0' + binOr[i + 1:]
			else:
				binOr = temp
		if l == 0:
			if maxVal == 0:
				ans = l
			print(ans)
			continue
		binAns = bin(ans)[2:]
		ansLen = len(binAns)
		if rLen > ansLen:
			binAns = '0' * (rLen - ansLen) + binAns
		else:
			binAns = binAns[ansLen - rLen:]
		_or1 = x | y
		binOr1 = bin(_or1)[2:]
		orLen = len(binOr1)
		if rLen > orLen:
			binOr1 = '0' * (rLen - orLen) + binOr1
		else:
			binOr1 = binOr1[orLen - rLen:]
		for i in range(rLen):
			if binOr1[i] == '0':
				tmp = binAns[:i] + '0' + binAns[i + 1:]
				if int(tmp, 2) >= l:
					ans = int(tmp, 2)
					binAns = tmp
		if maxVal == 0:
			ans = l
		print(ans)
except Exception as e:
	print(e)
	pass


def get_final_ans(x, y, answers, l, r):
	mx = -1
	final_ans = -1
	for ans in answers:
		if ans >= l and ans <= r:
			val = (x & ans) * (y & ans)
			if val > mx:
				final_ans = ans
				mx = val
			elif val == mx:
				final_ans = min(final_ans, ans)
	return final_ans

def get_ans(i, ans, dp, dp2, nearest, flag):
	if flag == True:
		if i == 42:
			return ans
		return dp[i + 1] + ans
	if i + 1 >= 42:
		return ans
	next_ind = nearest[i + 1]
	if next_ind == -1:
		return ans + dp[i + 1]
	else:
		if next_ind == 42:
			return ans + dp2[i + 1]
		temp = dp2[i + 1] - dp2[next_ind + 1]
		temp2 = dp[next_ind + 1]
		return ans + temp + temp2
anss = ''

def solve():
	(x, y, l, r) = [int(i) for i in input().split()]
	(x2, y2) = (x, y)
	(l2, r2) = (l, r)
	global anss
	bin_x = list(('0' * 43 + bin(x)[2:])[-43:])
	bin_y = list(('0' * 43 + bin(y)[2:])[-43:])
	bin_l = list(('0' * 43 + bin(l)[2:])[-43:])
	bin_r = list(('0' * 43 + bin(r)[2:])[-43:])
	dp = [-1] * 43
	x = 0
	for i in range(len(bin_l) - 1, -1, -1):
		if bin_x[i] == '1' or bin_y[i] == '1':
			x += 2 ** (42 - i)
		dp[i] = x
	dp2 = [-1] * 43
	x = 0
	for i in range(len(bin_l) - 1, -1, -1):
		if bin_x[i] == '1' or bin_y[i] == '1' or bin_l[i] == '1':
			x += 2 ** (42 - i)
		dp2[i] = x
	nearest = [-1] * 43
	for i in range(43):
		if (bin_x[i] == '1' or bin_y[i] == '1') and bin_l[i] == '0':
			nearest[i] = i
	x = -1
	for i in range(42, -1, -1):
		if nearest[i] == -1:
			nearest[i] = x
		else:
			x = nearest[i]
	ans = 0
	answers = []
	flag = False
	for i in range(43):
		if bin_r[i] == '1':
			temp_val = get_ans(i, ans, dp, dp2, nearest, flag)
			answers.append(temp_val)
			ans += 2 ** (42 - i)
			if flag == False and bin_l[i] == '0':
				flag = True
	answers.append(ans)
	print(get_final_ans(x2, y2, answers + [l], l2, r2))
t = int(input())
while t:
	t -= 1
	solve()


def binary(x):
	return bin(x)[2:]
power = []
for i in range(63):
	power.append(2 ** i)
t = int(input())
for rep in range(t):
	(x, y, l, r) = list(map(int, input().split()))
	if x == 0 or y == 0 or r == l:
		print(l)
	else:
		res = l | x | y
		c = binary(x | y)
		c = list(map(int, c))
		d = binary(res)
		d = list(map(int, d))
		c.reverse()
		d.reverse()
		pset = [1 * d[0]]
		for i in range(1, len(d)):
			pset.append(pset[i - 1] + d[i] * power[i])
		if res > r:
			right = binary(r)
			right = list(map(int, right))
			right.reverse()
			covered = 0
			maxi = 0
			op = 0
			if d[0] == 0 and right[0] == 1:
				r -= 1
			for i in range(len(right) - 1, 0, -1):
				if right[i] == 1:
					res = pset[i - 1] + covered
					if res >= l:
						temp = (x & res) * (y & res)
						if temp > maxi:
							maxi = temp
							op = res
					covered += power[i]
			temp = (x & r) * (y & r)
			if temp > maxi:
				maxi = temp
				op = r
			e = binary(op)
			e = list(map(int, e))
			e.reverse()
			for i in range(min(len(e), len(c))):
				if e[i] == 1 and c[i] == 0:
					if op - power[i] >= l:
						e[i] = 0
						op -= power[i]
			print(max(op, l))
		else:
			for i in range(len(c)):
				if d[i] == 1 and c[i] == 0:
					if res - power[i] >= l:
						d[i] = 0
						res -= power[i]
			print(res)


def set_bit(value, bit):
	return value | 1 << bit

def bit(value, bit):
	return value & 1 << bit != 0

def clrandreset(value, bit):
	return value & ~(1 << bit) | (1 << i) - 1
test = int(input())
for _ in range(test):
	(x, y, l, ans) = map(int, input().split())
	r = ans
	v = (x & r) * (y & r)
	for i in range(39, -1, -1):
		if bit(r, i):
			s = clrandreset(r, i)
			if (x & s) * (y & s) > v and s >= l:
				ans = s
				v = (x & ans) * (y & ans)
	for i in range(39, -1, -1):
		if bit(ans, i):
			s = clrandreset(ans, i)
			if s >= l and (x & s) * (y & s) == v:
				ans = s
	print(ans)


MI = lambda : map(int, input().split())
II = lambda : int(input())

def convert(listt):
	value = 0
	ll = len(listt)
	for i in range(len(listt)):
		digit = listt[i]
		if digit == 1:
			value = value + pow(2, ll - i - 1)
	return value

def binary(alpha):
	temp = list(bin(alpha))
	ans = []
	for item in temp[2:]:
		ans.append(int(item))
	return ans
for _ in range(II()):
	(x, y, l, r) = MI()
	z = x | y
	Zbin = binary(z)
	Rbin = binary(r)
	Lbin = binary(l)
	Zlen = len(Zbin)
	Rlen = len(Rbin)
	poss = set()
	if x == 0 or y == 0:
		print(l)
		continue
	if z >= l and z <= r:
		if (x & z) * (y & z) == 0:
			print(l)
			continue
		print(z)
		continue
	if l == 0:
		templist = Zbin[Zlen - Rlen:]
		z = convert(templist)
		zbin3 = templist[:]
		z = convert(zbin3)
		alpha = 0
		result = l
		lenn = len(zbin3)
		for i in range(lenn):
			if zbin3[i] == 0 and Rbin[i] == 1:
				break
			if zbin3[i] == 1 and Rbin[i] == 0:
				zbin3[i] = 0
				z -= pow(2, lenn - i - 1)
			if zbin3[i] == 1 and Rbin[i] == 1:
				z -= pow(2, lenn - i - 1)
				if z >= l:
					if (x & z) * (y & z) > alpha:
						result = z
						alpha = (x & z) * (y & z)
				z += pow(2, lenn - i - 1)
				continue
		if (x & z) * (y & z) > alpha and z >= l:
			result = z
		print(result)
		continue
	if z > r:
		templist = Zbin[Zlen - Rlen:]
		z = convert(templist)
		if z < l:
			while len(templist) != len(Rbin):
				templist.insert(0, 0)
			while len(Lbin) != len(Rbin):
				Lbin.insert(0, 0)
			ans = -1
			find = z
			lenn = max(len(Lbin), len(Rbin))
			gg = 0
			for j in range(lenn):
				if templist[j] == 0 and Lbin[j] == 1:
					templist[j] = 1
					find += pow(2, lenn - j - 1)
					continue
				if templist[j] == 1 and Lbin[j] == 0:
					if find > r:
						gg = 1
						break
					else:
						break
			if gg == 1:
				while len(Lbin) != len(Rbin):
					Lbin.insert(0, 0)
				result = l
				alpha = 0
				lenn = len(templist)
				flag = 0
				z = find
				for i in range(lenn):
					if z <= r and z >= l:
						poss.add(z)
					if flag == 1:
						if Rbin[i] == 0 and templist[i] == 1:
							templist[i] = 0
							z -= pow(2, lenn - i - 1)
							if z >= l and z <= r:
								poss.add(z)
							continue
						if Rbin[i] == 1 and templist[i] == 1:
							z -= pow(2, lenn - i - 1)
							poss.add(z)
							z += pow(2, lenn - i - 1)
							continue
					if flag == 0 and Lbin[i] - templist[i] == 0:
						continue
					if Lbin[i] == 0 and templist[i] == 1:
						if Rbin[i] == 0:
							templist[i] = 0
							z -= pow(2, lenn - i - 1)
							flag = 0
							if z <= r and z >= l:
								poss.add(z)
							continue
						else:
							z -= pow(2, lenn - i - 1)
							templist[i] = 0
							vc = templist[:]
							cv = z
							if z >= l:
								poss.add(z)
							if z < l:
								for joint in range(i + 1, lenn):
									if Lbin[joint] == 0 and templist[joint] == 1:
										poss.add(z)
										break
									if Lbin[joint] == 1 and templist[joint] == 0:
										templist[joint] = 1
										z += pow(2, lenn - joint - 1)
										if z >= l:
											poss.add(z)
										continue
									if Lbin[joint] == 0 and templist[joint] == 0:
										z += pow(2, lenn - joint - 1)
										poss.add(z)
										z -= pow(2, lenn - joint - 1)
							z = cv
							templist = vc
							z += pow(2, lenn - i - 1)
							templist[i] = 1
							flag = 1
							continue
					if flag == 0 and Lbin[i] == 1 and (templist[i] == 0):
						templist[i] = 1
						z += pow(2, lenn - i - 1)
						if z >= l and z <= r:
							poss.add(z)
						continue
				poss.add(z)
			else:
				poss.add(find)
		else:
			while len(Lbin) != len(Rbin):
				Lbin.insert(0, 0)
			result = l
			alpha = 0
			lenn = len(templist)
			flag = 0
			for i in range(lenn):
				if z <= r and z >= l:
					poss.add(z)
				if flag == 1:
					if Rbin[i] == 0 and templist[i] == 1:
						templist[i] = 0
						z -= pow(2, lenn - i - 1)
						if z >= l and z <= r:
							poss.add(z)
						continue
					if Rbin[i] == 1 and templist[i] == 1:
						z -= pow(2, lenn - i - 1)
						poss.add(z)
						z += pow(2, lenn - i - 1)
						continue
				if flag == 0 and Lbin[i] - templist[i] == 0:
					continue
				if Lbin[i] == 0 and templist[i] == 1:
					if Rbin[i] == 0:
						templist[i] = 0
						z -= pow(2, lenn - i - 1)
						flag = 0
						if z <= r and z >= l:
							poss.add(z)
						continue
					else:
						z -= pow(2, lenn - i - 1)
						templist[i] = 0
						vc = templist[:]
						cv = z
						if z >= l:
							poss.add(z)
						if z < l:
							for joint in range(i + 1, lenn):
								if Lbin[joint] == 0 and templist[joint] == 1:
									poss.add(z)
									break
								if Lbin[joint] == 1 and templist[joint] == 0:
									templist[joint] = 1
									z += pow(2, lenn - joint - 1)
									if z >= l:
										poss.add(z)
									continue
								if Lbin[joint] == 0 and templist[joint] == 0:
									z += pow(2, lenn - joint - 1)
									poss.add(z)
									z -= pow(2, lenn - joint - 1)
						z = cv
						templist = vc
						z += pow(2, lenn - i - 1)
						templist[i] = 1
						flag = 1
						continue
				if flag == 0 and Lbin[i] == 1 and (templist[i] == 0):
					templist[i] = 1
					z += pow(2, lenn - i - 1)
					if z >= l and z <= r:
						poss.add(z)
					continue
			poss.add(z)
	else:
		templist = Zbin
		while len(templist) != len(Rbin):
			templist.insert(0, 0)
		while len(Lbin) != len(Rbin):
			Lbin.insert(0, 0)
		ans = -1
		find = z
		lenn = len(Rbin)
		gg = 0
		for j in range(lenn):
			if templist[j] == 0 and Lbin[j] == 1:
				templist[j] = 1
				find += pow(2, lenn - j - 1)
				continue
			if templist[j] == 1 and Lbin[j] == 0:
				if find > r:
					gg = 1
					break
				else:
					break
		if gg == 1:
			while len(Lbin) != len(Rbin):
				Lbin.insert(0, 0)
			result = l
			alpha = 0
			lenn = len(templist)
			flag = 0
			z = find
			for i in range(lenn):
				if z <= r and z >= l:
					poss.add(z)
				if flag == 1:
					if Rbin[i] == 0 and templist[i] == 1:
						templist[i] = 0
						z -= pow(2, lenn - i - 1)
						if z >= l and z <= r:
							poss.add(z)
						continue
					if Rbin[i] == 1 and templist[i] == 1:
						z -= pow(2, lenn - i - 1)
						poss.add(z)
						z += pow(2, lenn - i - 1)
						continue
				if flag == 0 and Lbin[i] - templist[i] == 0:
					continue
				if Lbin[i] == 0 and templist[i] == 1:
					if Rbin[i] == 0:
						templist[i] = 0
						z -= pow(2, lenn - i - 1)
						flag = 0
						if z <= r and z >= l:
							poss.add(z)
						continue
					else:
						z -= pow(2, lenn - i - 1)
						templist[i] = 0
						vc = templist[:]
						cv = z
						if z >= l:
							poss.add(z)
						if z < l:
							for joint in range(i + 1, lenn):
								if Lbin[joint] == 0 and templist[joint] == 1:
									poss.add(z)
									break
								if Lbin[joint] == 1 and templist[joint] == 0:
									templist[joint] = 1
									z += pow(2, lenn - joint - 1)
									if z >= l:
										poss.add(z)
									continue
								if Lbin[joint] == 0 and templist[joint] == 0:
									z += pow(2, lenn - joint - 1)
									poss.add(z)
									z -= pow(2, lenn - joint - 1)
						z = cv
						templist = vc
						z += pow(2, lenn - i - 1)
						templist[i] = 1
						flag = 1
						continue
				if flag == 0 and Lbin[i] == 1 and (templist[i] == 0):
					templist[i] = 1
					z += pow(2, lenn - i - 1)
					if z >= l and z <= r:
						poss.add(z)
					continue
			poss.add(z)
		else:
			poss.add(find)
	ans = l
	prod = 0
	poss = sorted(poss)
	for i in poss:
		if (x & i) * (y & i) > prod:
			prod = (x & i) * (y & i)
			ans = i
	print(ans)


def decimalToBinary(n):
	return bin(n).replace('0b', '')
testcase = int(input())
for i in range(testcase):
	(X, Y, L, R) = input().split()
	(X, Y, L, R) = (int(X), int(Y), int(L), int(R))
	K = 0
	F_max = 0
	probable_answer = X | Y
	p_answer = R + 1
	R_binary = decimalToBinary(R)
	L_binary = decimalToBinary(L)
	F_atL = (X & L) * (Y & L)
	if X == 0 or Y == 0 or R == 0 or (R == L):
		K = L
		F_max = F_atL
	else:
		if probable_answer <= R:
			if probable_answer >= L:
				K = probable_answer
				F_max = (X & probable_answer) * (Y & probable_answer)
			else:
				L_binary_length = len(L_binary)
				probable_answer_binary = decimalToBinary(probable_answer)
				probable_answer_binary_list = list(probable_answer_binary)
				L_binary_list = list(L_binary)
				start_index = L_binary_length - len(probable_answer_binary)
				F_max = (X & L) * (Y & L)
				K = L
				for j in range(len(probable_answer_binary)):
					if L_binary_list[start_index + j] == '0':
						p_answer = L_binary_list[:start_index + j] + ['1'] + probable_answer_binary_list[j + 1:]
						p_answer = ''.join(p_answer)
						p_answer = int(p_answer, 2)
						F_p = (X & p_answer) * (Y & p_answer)
						if F_p == F_max and K > p_answer and (p_answer >= L) and (p_answer <= R):
							F_max = F_p
							K = p_answer
						elif F_p > F_max and p_answer >= L and (p_answer <= R):
							F_max = F_p
							K = p_answer
		else:
			R_binary_length = len(R_binary)
			ones_R = 2 ** R_binary_length - 1
			probable_answer_short = probable_answer & ones_R
			if probable_answer_short <= R:
				if probable_answer_short >= L:
					K = probable_answer_short
					F_max = (X & probable_answer_short) * (Y & probable_answer_short)
				else:
					L_binary_length = len(L_binary)
					probable_answer_binary = decimalToBinary(probable_answer_short)
					probable_answer_binary_list = list(probable_answer_binary)
					L_binary_list = list(L_binary)
					start_index = L_binary_length - len(probable_answer_binary)
					F_max = (X & L) * (Y & L)
					K = L
					for j in range(len(probable_answer_binary)):
						if L_binary_list[start_index + j] == '0':
							p_answer = L_binary_list[:start_index + j] + ['1'] + probable_answer_binary_list[j + 1:]
							p_answer = ''.join(p_answer)
							p_answer = int(p_answer, 2)
							F_p = (X & p_answer) * (Y & p_answer)
							if F_p == F_max and K > p_answer and (p_answer >= L) and (p_answer <= R):
								F_max = F_p
								K = p_answer
							elif F_p > F_max and p_answer >= L and (p_answer <= R):
								F_max = F_p
								K = p_answer
			else:
				probable_answer_binary = decimalToBinary(probable_answer_short)
				probable_answer_binary_list = list(probable_answer_binary)
				R_binary_list = list(R_binary)
				F_max = (X & R) * (Y & R)
				K = R
				for j in range(R_binary_length):
					if R_binary_list[j] == '1':
						p_answer = R_binary_list[:j] + ['0'] + probable_answer_binary_list[j + 1:]
						p_answer = ''.join(p_answer)
						p_answer = int(p_answer, 2)
						F_p = (X & p_answer) * (Y & p_answer)
						if F_p == F_max and K > p_answer and (p_answer >= L) and (p_answer <= R):
							F_max = F_p
							K = p_answer
						elif F_p > F_max and p_answer >= L and (p_answer <= R):
							F_max = F_p
							K = p_answer
		L_binary_length = len(L_binary)
		ones_L = 2 ** L_binary_length - 1
		probable_answer_short = probable_answer & ones_L
		probable_answer_binary_short = decimalToBinary(probable_answer_short)
		probable_answer_binary_list = list(probable_answer_binary_short)
		L_binary_list = list(L_binary)
		start_index = L_binary_length - len(probable_answer_binary_short)
		for j in range(len(probable_answer_binary_short)):
			if L_binary_list[start_index + j] == '0':
				p_answer = L_binary_list[:start_index + j] + ['1'] + probable_answer_binary_list[j + 1:]
				p_answer = ''.join(p_answer)
				p_answer = int(p_answer, 2)
				F_p = (X & p_answer) * (Y & p_answer)
				if F_p == F_max and K > p_answer and (p_answer >= L) and (p_answer <= R):
					F_max = F_p
					K = p_answer
				elif F_p > F_max and p_answer >= L and (p_answer <= R):
					F_max = F_p
					K = p_answer
		R_binary_length = len(R_binary)
		ones_R = 2 ** R_binary_length - 1
		probable_answer_short = probable_answer & ones_R
		probable_answer_binary_short = decimalToBinary(probable_answer_short)
		probable_answer_binary_list = list(probable_answer_binary_short)
		R_binary_list = list(R_binary)
		start_index = R_binary_length - len(probable_answer_binary_short)
		for j in range(len(probable_answer_binary_short)):
			if R_binary_list[start_index + j] == '1':
				p_answer = R_binary_list[:start_index + j] + ['0'] + probable_answer_binary_list[j + 1:]
				p_answer = ''.join(p_answer)
				p_answer = int(p_answer, 2)
				F_p = (X & p_answer) * (Y & p_answer)
				if F_p == F_max and K > p_answer and (p_answer >= L) and (p_answer <= R):
					F_max = F_p
					K = p_answer
				elif F_p > F_max and p_answer >= L and (p_answer <= R):
					F_max = F_p
					K = p_answer
	F_atR = (X & R) * (Y & R)
	if F_atL >= F_max:
		K = L
		F_max = F_atL
	if F_atR > F_max:
		K = R
		F_max = F_atR
	print(K)


import bisect
import itertools

def f(x, y, z):
	return (x & z) * (y & z)

def solve(x, y, l, r):
	z = x | y
	sr = bin(r)[2:]
	lr = len(sr)
	sx = bin(x)[2:].zfill(40)[40 - lr:]
	sy = bin(y)[2:].zfill(40)[40 - lr:]
	x = int(sx, 2)
	y = int(sy, 2)
	if x == 0 or y == 0 or r == 0 or (r == l):
		k = l
	elif l <= z and r >= z:
		k = z
	else:
		k = r
		p = f(x, y, k)
		for i in range(lr - 1, -1, -1):
			if sr[i] == '1' and (sx[i] == '0' or sy[i] == '0'):
				sk = sr[:i] + '0' + '1' * (lr - i - 1)
				k = int(sk, 2)
				np = f(x, y, k)
				if np >= p and k >= l:
					(p, sr) = (np, sk)
		sk = sr
		for i in range(lr):
			if sr[i] == '1':
				if sx[i] == '0' and sy[i] == '0':
					sk = sk[:i] + '0' + sk[i + 1:]
				else:
					sr = sk[:i] + '0' + sk[i + 1:]
					k = int(sr, 2)
					np = f(x, y, k)
					if np >= p:
						sk = sr
		k = int(sk, 2)
		if k < l:
			li1 = [1 << i for i in range(lr) if sk[lr - 1 - i] == '0']
			li2 = list(itertools.accumulate(li1))
			d1 = l - k
			while d1 > 0:
				p1 = bisect.bisect_left(li2, d1)
				v1 = li1[p1]
				k += v1
				d1 -= v1
	return k
t = int(input())
for j in range(t):
	(x, y, l, r) = map(int, input().split(' '))
	z = solve(x, y, l, r)
	print(z)


def solve(X, Y, L, R):
	(mx, mxz) = (-1, -1)

	def search(i, Z, freeL, freeR):
		nonlocal mx, mxz
		if i < 0:
			res = (X & Z) * (Y & Z)
			if res > mx or (res == mx and Z < mxz):
				(mx, mxz) = (res, Z)
			return
		if freeL and freeR:
			Z = Z << i + 1 | (X | Y) & (1 << i + 1) - 1
			res = (X & Z) * (Y & Z)
			if res > mx or (res == mx and Z < mxz):
				(mx, mxz) = (res, Z)
			return
		(x, y, l, r) = (X >> i & 1, Y >> i & 1, L >> i & 1, R >> i & 1)
		Z <<= 1
		if freeL:
			if not r:
				search(i - 1, Z, True, False)
			else:
				search(i - 1, Z + 1, True, False)
				search(i - 1, Z, True, True)
			return
		if freeR:
			if l:
				search(i - 1, Z + 1, False, True)
			else:
				search(i - 1, Z + 1, True, True)
				search(i - 1, Z, False, True)
			return
		if l == r:
			Z += r
			search(i - 1, Z, False, False)
			return
		assert l < r
		search(i - 1, Z, False, True)
		search(i - 1, Z + 1, True, False)
	search(42, 0, False, False)
	return mxz

def main():
	from sys import stdin
	T = int(stdin.readline().strip())
	for t in range(T):
		(X, Y, L, R) = list(map(int, stdin.readline().strip().split()))
		out = solve(X, Y, L, R)
		print(out)
main()


def f(x, y, z):
	return (x & z) * (y & z)

def jp(i, l, m):
	j = i & (i ^ m)
	while j > 0:
		maxb = h(j)
		if i - maxb >= l:
			i -= maxb
		j -= maxb
	return i

def f2(p):
	x = p ^ p + 1
	x = h(x)
	return p - x

def h(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n |= n >> 32
	n = n + 1
	return n >> 1
t = int(input())
for _ in range(t):
	for mkp in range(1):
		continue
	(x, y, l, r) = map(int, input().split())
	p = r
	ans = []
	while p >= l:
		ans.append([f(x, y, p), p])
		p = f2(p)
	temp = ans[0][1]
	maxm = ans[0][0]
	temp = []
	for i in ans:
		if i[0] > maxm:
			maxm = i[0]
			temp = [i[1]]
		elif i[0] == maxm:
			temp.append(i[1])
	if maxm == 0:
		print(l)
	else:
		m = x | y
		minm = temp[0]
		for i in temp:
			q = jp(i, l, m)
			if q < minm:
				minm = q
		print(minm)


def check(x, y, z):
	return (x & z) * (y & z)

def biggerThanL(v, l):
	vBin = list(format(v, 'b'))
	lBin = list(format(l, 'b'))
	vLen = len(vBin)
	lLen = len(lBin)
	leftovers = ['0' for i in range(lLen - vLen)]
	vBin = leftovers + vBin
	for i in range(lLen):
		if vBin[i] != lBin[i]:
			if vBin[i] == '0':
				vBin[i] = '1'
			else:
				break
	return int(''.join(vBin), 2)

def tryOther(v, l, r):
	vBin = list(format(v, 'b'))
	rBin = list(format(r, 'b'))
	maxProd = 0
	ans = l
	for i in range(len(vBin)):
		if vBin[i] == '1' and rBin[i] == '1':
			tv = int(''.join(vBin), 2)
			if tv < l:
				tv = biggerThanL(tv, l)
			if tv <= r:
				newProd = check(x, y, tv)
				if newProd == maxProd:
					ans = min(tv, ans)
				if newProd > maxProd:
					maxProd = newProd
					ans = tv
			temp = vBin.copy()
			temp[i] = '0'
			tv = int(''.join(temp), 2)
			if tv < l:
				tv = biggerThanL(tv, l)
			if tv <= r:
				newProd = check(x, y, tv)
				if newProd == maxProd:
					ans = min(tv, ans)
				if newProd > maxProd:
					maxProd = newProd
					ans = tv
		if vBin[i] == '1' and rBin[i] == '0':
			vBin[i] = '0'
			temp = vBin.copy()
			tv = int(''.join(temp), 2)
			if tv < l:
				tv = biggerThanL(tv, l)
			if tv <= r:
				newProd = check(x, y, tv)
				if newProd == maxProd:
					ans = min(tv, ans)
				if newProd > maxProd:
					maxProd = newProd
					ans = tv
	return ans
t = int(input())
for z in range(t):
	(x, y, l, r) = map(int, input().split())
	ans = x | y
	if ans > r:
		ans = int(''.join(list(format(ans, 'b')[-len(list(format(r, 'b'))):])), 2)
	if ans < l:
		ans = biggerThanL(ans, l)
	if ans > r:
		ans = tryOther(ans, l, r)
	if check(x, y, ans) == 0:
		ans = l
	print(ans)


import math
for _ in range(int(input())):
	(x, y, l, r) = [int(x) for x in input().split()]
	if min(x, y) == 0 or r == 0:
		print(l)
		continue
	else:
		b = bin(r)
		br = b[2:]
		le = len(br)
		A = []
		arr = []
		sum1 = 0
		for i in range(le):
			if br[i] == '0':
				continue
			isum = 2 ** (le - i - 1)
			sum1 += isum
			v1 = sum1
			v2 = sum1 - 1
			a1 = v1 & x
			b1 = v1 & y
			ans = a1 | b1
			if ans >= l and ans <= r:
				arr.append(ans)
			else:
				r1 = l | ans
				k = int(r1 - l)
				if k != 0:
					k = int(math.log2(k))
				k = int(2 ** k) - 1
				kandl = k & l
				r1 = r1 - kandl
				r1 = r1 | ans
				if r1 >= l and r1 <= r:
					arr.append(r1)
			a1 = v2 & x
			b1 = v2 & y
			ans = a1 | b1
			if ans >= l and ans <= r:
				arr.append(ans)
			else:
				r1 = l | ans
				r2 = int(r1 - l)
				if r2 != 0:
					r2 = int(math.log2(r2))
				r2 = int(2 ** r2) - 1
				r2andl = r2 & l
				r1 = r1 - r2andl
				r1 = r1 | ans
				if r1 >= l and r1 <= r:
					arr.append(r1)
		tup = set(arr.copy())
		arr = list(tup)
		for i in range(len(arr)):
			v = arr[i]
			p1 = v & x
			p2 = v & y
			A.append([v, p1 * p2])
		mi = l
		pro = 0
		for i in range(0, len(A)):
			ans1 = A[i][0]
			pro1 = A[i][1]
			if pro == pro1 and ans1 <= r:
				mi = min(mi, ans1)
			elif pro1 > pro and ans1 <= r:
				mi = ans1
				pro = pro1
		print(mi)


import math
t = int(input())
while True:
	try:
		(x, y, l, r) = map(int, input().strip().split())
		if r == 0:
			print(0)
			continue
		binary = bin(r)[2:]
		k = len(binary)
		val = l ^ r
		if val == 0:
			pos = 0
		else:
			binary = bin(val)[2:]
			pos = len(binary)
		k = k - 1
		pos = pos - 1
		if pos == -1:
			print(l)
		else:
			z = 0
			for i in range(k, pos, -1):
				if r & 1 << i:
					z = z | 1 << i
			pl = (x & l) * (y & l)
			pr = (x & r) * (y & r)
			if pr > pl:
				max_pro = pr
				zmin = r
			else:
				max_pro = pl
				zmin = l
			z1 = z
			z1 = z1 | 1 << pos
			for i in range(pos - 1, -1, -1):
				if r & 1 << i:
					temp = z1
					temp += x & (1 << i) - 1 | y & (1 << i) - 1
					pro = (x & temp) * (y & temp)
					if pro > max_pro:
						max_pro = pro
						zmin = temp
					elif pro == max_pro:
						zmin = min(zmin, temp)
					z1 = z1 | 1 << i
			z2 = z
			for i in range(pos - 1, -1, -1):
				if l & 1 << i:
					z2 = z2 | 1 << i
				else:
					temp = z2
					temp = z2 | 1 << i
					temp += x & (1 << i) - 1 | y & (1 << i) - 1
					pro = (x & temp) * (y & temp)
					if pro > max_pro:
						max_pro = pro
						zmin = temp
					elif pro == max_pro:
						zmin = min(zmin, temp)
			print(zmin)
			t = t - 1
	except EOFError as e:
		break


mxB = 1 << 40

def f(x, y, z):
	return (z & x) * (z & y)

def lcomp(a, b):
	return a & -b - 1

def find(x, y, l, r, b=mxB):
	z = x | y
	w = x & y
	ans = z
	lans = lcomp(z, l)
	while b > 0 and r & b == 0:
		b >>= 1
	z = 0 if b == 0 else z & 2 * b - 1
	rb = b
	pv = 0
	while b > 0 and (r < z or z < l):
		if z < l and l & b:
			z |= b
		if z & b:
			if r < z and r & b == 0:
				z -= b
			if r & b and (l & b == 0 or z - b > l):
				v = f(x, y, z - b)
				lz = lcomp(z - b, l)
				if v > pv or (v == pv and lz < lans):
					ans = z - b
					lans = lz
					pv = v
		b >>= 1
	v = f(x, y, z)
	lz = lcomp(z, l)
	if v > pv or (v == pv and lz < lans):
		ans = z
		pv = v
	b = rb
	while b > 0 and ans < l:
		if l & b:
			ans |= b
		b >>= 1
	if pv == 0:
		ans = l
	return ans
for t in range(int(input().strip())):
	(x, y, l, r) = tuple(map(int, input().strip().split()))
	ans = 0
	z = x | y
	ans = find(x, y, l, r)
	print(ans)


import math
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	if x == 0 or y == 0:
		print(l)
		continue
	val = x | y
	z = []
	if l == 0:
		if val > r:
			for i in range(63, -1, -1):
				if val <= r:
					break
				tmp = 1 << i
				if val & tmp:
					if val & ~(1 << i) > r:
						val = val & ~(1 << i)
					else:
						z.append(val & ~(1 << i))
		if val < r:
			print(val)
		else:
			(ans, mx) = (l, 0)
			for i in z:
				if (x & i) * (y & i) > mx:
					mx = (x & i) * (y & i)
					ans = i
			print(ans)
	else:
		tmp = bin(val)[2:].zfill(63)
		r = bin(r)[2:].zfill(63)
		l = bin(l)[2:].zfill(63)
		final = ''
		for i in range(63):
			if l[i] == '1':
				final += '1'
			elif l[i] == '0' and tmp[i] == '1':
				final += tmp[i:]
				break
			else:
				final += '0'
		if int(final, 2) <= int(r, 2):
			print(int(final, 2))
		else:
			a = ''
			for i in range(63):
				if r[i] == '1':
					if int(a[:i], 2) > int(l[:i], 2):
						z.append(a + '0' + final[i + 1:])
					else:
						temp = ''
						for j in range(i + 1, 63):
							if l[j] == '1':
								temp += '1'
							elif l[j] == '0' and final[j] == '1':
								temp += tmp[j:]
								break
							else:
								temp += '0'
						z.append(a + '0' + temp)
					a += '1'
				elif r[i] == '0':
					a += '0'
			z.append(a)
			z = [int(i, 2) for i in z if int(i, 2) > int(l, 2)]
			mx = (x & int(l, 2)) * (y & int(l, 2))
			final = int(l, 2)
			for i in z:
				tmp = (x & i) * (y & i)
				if tmp > mx:
					mx = tmp
					final = i
			print(final)


shifts = [2 ** p for p in range(62, -1, -1)]

def solve(x, y, l, r):
	if l == 0 and r >= 2 * max(x, y):
		if x * y == 0:
			return 0
		return x | y
	result = l
	current = l
	best = (x & current) * (y & current)
	while True:
		last0 = ~current
		last0 = last0 & -last0
		current += last0
		if current > r:
			current -= last0 - 1
		if current > r:
			break
		val = (x & current) * (y & current)
		if val > best:
			best = val
			result = current
	for shift in shifts:
		tmp = result ^ shift
		if tmp > result or tmp < l or (tmp & x) * (tmp & y) != best:
			continue
		result = tmp
	return result
tests = int(input())
for t in range(tests):
	(x, y, l, r) = map(int, input().split())
	print(solve(x, y, l, r))


def rcom(no, x, y):
	orr = x | y
	return no & orr

def samelen(b, l):
	m = max(len(b), len(l))
	b = '0' * (m - len(b)) + b
	l = '0' * (m - len(l)) + l
	return (b, l)

def round1(x, y, l, r):
	x = bin(x)[2:]
	y = bin(y)[2:]
	l = bin(l)[2:]
	r = bin(r)[2:]
	rini = r
	mv = (int(x, 2) & int(r, 2)) * (int(y, 2) & int(r, 2))
	no = int(r, 2)
	c = 0
	for i in range(1, len(r)):
		if r[i] == '1' and int(r[0:i] + '0' + '1' * (len(r) - i - 1), 2) >= int(l, 2):
			new = r[0:i] + '0' + '1' * (len(r) - i - 1)
			fv = (int(x, 2) & int(new, 2)) * (int(y, 2) & int(new, 2))
			if fv >= mv and c == 0:
				c = 1
				mv = fv
				no = int(new, 2)
			elif fv > mv:
				mv = fv
				no = int(new, 2)
	r = rini
	for i in range(len(r)):
		if r[i] == '1' and int(r[0:i] + '0' + '1' * (len(r) - i - 1), 2) >= int(l, 2):
			new = r[0:i] + '0' + '1' * (len(r) - i - 1)
			fv = (int(x, 2) & int(new, 2)) * (int(y, 2) & int(new, 2))
			if fv >= mv:
				mv = fv
				no = int(new, 2)
			else:
				break
			r = new
	no = rcom(no, int(x, 2), int(y, 2))
	if no < int(l, 2):
		b = bin(no)[2:]
		(b, l) = samelen(b, l)
		for i in range(len(b)):
			if b[i] == '1' and l[i] == '0':
				return int(b, 2)
			if b[i] == '0' and l[i] == '1':
				b = b[:i] + '1' + b[i + 1:]
		return int(b, 2)
	return no
t = int(input())
for _ in range(t):
	st = input()
	(x, y, l, r) = st.split()
	print(round1(int(x), int(y), int(l), int(r)))


def f(q):
	fi = []
	ch = False
	for i in range(q, 0, -1):
		if b[-i] == a[-i]:
			fi.append(a[-i])
		elif b[-i] == '1':
			if ch:
				fi.append('0')
			else:
				fi.append('1')
		elif b[-i] == '0':
			ch = True
			fi.append('1')
	return ''.join(fi)
for _ in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	z = x | y
	a = bin(z)[2:]
	b = bin(l)[2:]
	c = bin(r)[2:]
	q = len(a)
	if x == 0 or y == 0:
		print(l)
		continue
	if r - l <= 1:
		if (x & l) * (y & l) >= (x & r) * (y & r):
			print(l)
		else:
			print(r)
		continue
	elif l <= z <= r:
		print(z)
		continue
	elif l > z:
		t = int(b[:-q] + a, 2)
		t1 = int(b[-q:], 2)
		if t1 <= z and r >= t:
			print(t)
			continue
		elif t1 > z:
			t = int(b[:-q] + f(q), 2)
			if t <= r:
				print(t)
				continue
	b = b.rjust(q, '0')
	c = c.rjust(q, '0')
	for i in range(q, 1, -1):
		if b[-i] != c[-i]:
			break
	fi = b[:-i + 1] + f(i - 1)
	t = int(fi, 2)
	m = (x & t) * (y & t)
	if a[-i] == '0' and m != 0:
		print(t)
	else:
		for j in range(i - 1, 0, -1):
			if a[-j] == '0' and c[-j] == '1':
				t1 = int(c[:-j] + a[-j:], 2)
				m1 = (x & t1) * (y & t1)
				if m1 > m:
					m = m1
					t = t1
				break
			elif a[-j] == '1' and c[-j] == '1' and (j != 1):
				t1 = int(c[:-j] + '0' + a[-j + 1:], 2)
				m1 = (x & t1) * (y & t1)
				if m1 > m:
					m = m1
					t = t1
			else:
				continue
		if (x & r) * (y & r) > m:
			print(r)
		elif m == 0:
			print(l)
		else:
			print(t)


import math

def ctb(p):
	bl = list(bin(p))
	bl = list(map(int, bl[2:len(bl)]))
	return bl

def ctd(binl):
	s = ''.join(map(str, binl))
	return int(s, 2)
t = int(input())
for _ in range(t):
	dg = 0
	(x, y, l, r) = map(int, input().split())
	xb = ctb(x)
	yb = ctb(y)
	lb = ctb(l)
	rb = ctb(r)
	nx = len(xb)
	ny = len(yb)
	nr = len(rb)
	nl = len(lb)
	ansb = rb[0:nr]
	if nx < nr:
		dx = nr - nx
		xb = [0] * dx + xb
	elif nx > nr:
		dx = nx - nr
		xb = xb[dx:nx]
	if ny < nr:
		dy = nr - ny
		yb = [0] * dy + yb
	elif ny > nr:
		dy = ny - nr
		yb = yb[dy:ny]
	if nl < nr:
		dl = nr - nl
		lb = [0] * dl + lb
	change = 0
	for i in range(nr):
		if xb[i] == 0 and yb[i] == 0:
			if lb[i] == 0 or dg > 0:
				if ansb[i] == 1:
					change = 1
				ansb[i] = 0
			else:
				ansb[i] = 1
		if xb[i] == 1 and yb[i] == 1:
			if change > 0:
				ansb[i] = 1
			if ansb[i] == 1 and lb[i] == 0:
				dg = 1
		if xb[i] * yb[i] == 0 and xb[i] + yb[i] > 0:
			if change == 0:
				if ansb[i] == 1 and i < nr - 1 and (dg > 0 or lb[i] == 0):
					zb = ansb[0:nr]
					zb[i] = 0
					for j in range(i + 1, nr):
						zb[j] = 1
					z = ctd(zb)
					na = (x & z) * (y & z)
					zb = ansb[0:nr]
					z = ctd(zb)
					ca = (z & x) * (z & y)
					if ca > na:
						if lb[i] == 0:
							dg = 1
						continue
					for ci in range(i + 1, nr):
						if ansb[ci] == 1:
							if xb[ci] == 0 and yb[ci] == 0 and (dg > 0 or lb[ci] == 0):
								xyb = ansb[0:ci + 1]
								xyb[ci] = 0
								xyb.extend([1] * (nr - 1 - ci))
								xy = ctd(xyb)
								ca = (xy & x) * (xy & y)
								break
							elif xb[ci] == 1 and yb[ci] == 1:
								continue
							xyb = ansb[0:ci + 1]
							xyb[ci] = 0
							xyb.extend([1] * (nr - 1 - ci))
							xy = ctd(xyb)
							temp = (xy & x) * (xy & y)
							if temp > ca:
								ca = temp
							if ca > na:
								break
					if na >= ca and (dg > 0 or lb[i] == 0):
						ansb[i] = 0
						change = 1
					elif lb[i] == 0:
						dg = 1
			else:
				ansb[i] = 1
				if lb[i] == 0:
					dg = 1
	ans = ctd(ansb)
	z = (x & ans) * (y & ans)
	if z == 0:
		ans = l
	print(int(ans))


def solve(ind, restriction, ans):
	sol = []
	flag = 0
	for i in range(ind, m):
		if x[i] == '1' or y[i] == '1':
			if not restriction:
				ans += '1'
			elif r[i] == '0':
				ans += '0'
			else:
				sol.extend(solve(i + 1, 1, ans + '1'))
				sol.extend(solve(i + 1, 0, ans + '0'))
				flag = 1
				break
		else:
			if l[0:len(ans)] == ans and l[i] == '1':
				ans += '1'
				continue
			ans += '0'
			if r[i] == '1':
				restriction = 0
	if not flag:
		sol.append(ans)
	return sol
for nt in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	if True:
		if True:
			if True:
				if x == 0 or y == 0:
					print(l)
					continue
				(x, y) = (bin(x)[2:], bin(y)[2:])
				(l, r) = (bin(l)[2:], bin(r)[2:])
				m = max(len(x), len(y), len(l), len(r))
				x = '0' * (m - len(x)) + x
				y = '0' * (m - len(y)) + y
				l = '0' * (m - len(l)) + l
				r = '0' * (m - len(r)) + r
				answer = solve(0, 1, '')
				ans = 0
				num = int(l, 2)
				for i in answer:
					if (int(x, 2) & int(i, 2)) * (int(y, 2) & int(i, 2)) > ans and int(i, 2) >= int(l, 2):
						ans = (int(x, 2) & int(i, 2)) * (int(y, 2) & int(i, 2))
						num = int(i, 2)
					elif (int(x, 2) & int(i, 2)) * (int(y, 2) & int(i, 2)) == ans and int(i, 2) >= int(l, 2):
						num = min(int(i, 2), num)
				print(num)


def dec(x):
	n = len(x)
	ans = 0
	for i in range(n):
		if x[n - 1 - i] == '1':
			ans += arr[i]
	return ans

def maker(tmp, r, pos):
	for i in range(len(tmp)):
		if pos == i:
			continue
		if tmp[i] == '1' and r[i] == '0':
			return tmp
		if tmp[i] == '0' and r[i] == '1':
			tmp[i] = '1'
	return tmp
arr = []
tmp = 1
for i in range(45):
	arr.append(tmp)
	tmp *= 2
t = int(input())
for _ in range(t):
	(x, y, l, r) = map(int, input().split())
	if x == 0 or y == 0:
		print(l)
	else:
		k = x | y
		if k <= r and k >= l:
			print(k)
			continue
		if r - l <= 1000:
			mans = 0
			ans = l
			for i in range(l, r + 1):
				mk = (i & x) * (i & y)
				if mans < mk:
					mans = mk
					ans = i
			print(ans)
			continue
		t1 = bin(r).replace('0b', '')
		t2 = bin(k).replace('0b', '')
		t3 = bin(l).replace('0b', '')
		n = len(t1)
		if len(t2) > n:
			t2 = t2[-n:]
		else:
			t2 = '0' * (n - len(t2)) + t2
		t3 = '0' * (n - len(t3)) + t3
		ans = int(t2, 2)
		if ans <= r and ans >= l:
			if ans & x and ans & y:
				print(ans)
			else:
				print(l)
			continue
		l1 = list(t1)
		l2 = list(t2)
		l3 = list(t3)
		if ans < l:
			l2 = maker(l2, l3, -1)
		ma = 0
		mans = 0
		for i in range(n):
			if l1[i] == '1' and l2[i] == '1':
				tmp = l2[:]
				tmp[i] = 0
				ans = dec(tmp)
				if ans >= l:
					mk = (ans & x) * (ans & y)
					if ma < mk:
						ma = mk
						mans = ans
				else:
					tmp = maker(tmp, l3, i)
					ans = dec(tmp)
					mk = (ans & x) * (ans & y)
					if ma < mk and ans >= l:
						ma = mk
						mans = ans
			if l1[i] == '0' and l2[i] == '1':
				l2[i] = '0'
			if l1[i] == '1' and l2[i] == '0':
				ans = dec(l2)
				if ans >= l:
					mk = (ans & x) * (ans & y)
					if ma < mk:
						ma = mk
						mans = ans
					break
				else:
					l2 = maker(l2, l3, -1)
					ans = dec(l2)
					mk = (ans & x) * (ans & y)
					if ma < mk and ans >= l and (ans <= r):
						ma = mk
						mans = ans
		ans = dec(l2)
		if ans < l:
			l2 = maker(l2, l3, -1)
			ans = dec(l2)
		if ma < (ans & x) * (ans & y) and ans >= l:
			mans = ans
		if mans & x and mans & y and (mans >= l):
			print(mans)
		else:
			print(l)


import math
import collections
from sys import stdin, stdout
T = int(stdin.readline())

def or_s(x, y):
	s = ''
	for i in range(len(x)):
		if x[i] == '1' or y[i] == '1':
			s = s + '1'
		else:
			s = s + '0'
	return s

def makegreater(x, y):
	s = ''
	for i in range(len(x)):
		if x[i] == '1' and y[i] == '0':
			s = s + x[i:]
			return s
		if x[i] == '0' and y[i] == '1':
			s = s + '1'
			continue
		s = s + x[i]
	return s
for _ in range(T):
	(x, y, l, r) = list(map(int, stdin.readline().split()))
	if x == 0 or y == 0 or l == r:
		print(l)
		continue
	if x | y >= l and x | y <= r:
		print(x | y)
		continue
	ml = len(bin(max(x, y, l, r))) - 2
	l = bin(l)[2:]
	r = bin(r)[2:]
	x = bin(x)[2:]
	y = bin(y)[2:]
	fun = lambda l: '0' * (ml - len(l)) + l
	l = fun(l)
	r = fun(r)
	x = fun(x)
	y = fun(y)
	req = or_s(x, y)
	for i in range(ml):
		if l[i] != r[i]:
			ind = i
			break
	pos = [l]
	ans = l[:ind]
	for i in range(ind, ml - 1):
		if r[i] == '0':
			ans = ans + '0'
			continue
		if i == ind:
			wo = makegreater(req[i + 1:], l[i + 1:])
			pos.append(ans + '0' + wo)
		else:
			wo = req[i + 1:]
			pos.append(ans + '0' + wo)
			pos.append(ans + '0' * (ml - i))
		ans = ans + '1'
	pos.append(r)
	pos.append(r[:-1] + '0')
	d = {}
	for po in pos:
		tmp = int('0b' + po, 2)
		d[tmp] = True
	x = int('0b' + x, 2)
	y = int('0b' + y, 2)
	ans = int('0b' + l, 2)
	mx = (x & ans) * (y & ans)
	for k in d:
		tmp = (x & k) * (y & k)
		if tmp > mx:
			mx = tmp
			ans = k
		if tmp == mx:
			if k < ans:
				ans = k
	print(ans)


def ctn(s):
	final = 0
	s = s[::-1]
	for i in range(len(s)):
		if s[i] == '1':
			final += 2 ** i
	return final

def fun(a, b, c):
	return (a & c) * (b & c)
t = int(input())
while t:
	t -= 1
	(x, y, l, r) = [int(i) for i in input().split()]

	def solve():
		if x == 0 or y == 0:
			return 0
		else:
			ans = x | y
			br = bin(r)[2:]
			bans = bin(ans)[2:]
			if len(br) > len(bans):
				return ans
			else:
				bans = bans[len(bans) - len(br):]
				st = ''
				temp = len(br)
				for i in range(len(br)):
					if br[i] == '1' and bans[i] == '0':
						temp = i
						break
				for i in range(temp):
					if br[i] == '1' and bans[i] == '1':
						st += '1'
					else:
						st += '0'
				for i in range(temp, len(br)):
					if bans[i] == '1':
						st += '1'
					else:
						st += '0'
				check1 = ctn(st)
				if br.count('0') == 0:
					if fun(x, y, check1) == 0:
						return 0
					else:
						return check1
				else:
					bx = bin(x)[2:]
					by = bin(y)[2:]
					if len(bx) > len(br):
						bx = bx[len(bx) - len(br):]
					while len(bx) < len(br):
						bx = '0' + bx
					if len(by) > len(br):
						by = by[len(by) - len(br):]
					while len(by) < len(br):
						by = '0' + by
					maxi = fun(x, y, check1)
					maxians = check1
					flag = -1
					for i in range(len(br)):
						if br[i] == '1' and (bx[i] == '0' and by[i] == '1' or (bx[i] == '1' and by[i] == '0')):
							flag = i
							st2 = st[:flag] + '0' + bans[flag + 1:]
							check2 = int('0b' + st2, 2)
							if maxi < fun(x, y, check2):
								maxi = fun(x, y, check2)
								maxians = check2
							elif maxi == fun(x, y, check2):
								maxians = min(check1, check2)
					if maxi == 0:
						return 0
					else:
						return maxians

	def solve2():
		maxi = -1
		ans = -1
		for i in range(l, r + 1):
			if fun(x, y, i) > maxi:
				ans = i
				maxi = fun(x, y, i)
		return ans

	def final():
		ans = solve()
		if fun(x, y, ans) == 0:
			return l
		if ans >= l:
			return ans
		else:
			bl = bin(l)[2:]
			br = bin(r)[2:]
			bx = bin(x)[2:]
			by = bin(y)[2:]
			if len(bx) > len(br):
				bx = bx[len(bx) - len(br):]
			while len(bx) < len(br):
				bx = '0' + bx
			if len(by) > len(br):
				by = by[len(by) - len(br):]
			while len(by) < len(br):
				by = '0' + by
			bans = bin(ans)[2:]
			if len(br) > len(bl):
				if len(bl) > len(bans):
					bans = bl[:len(bl) - len(bans)] + bans
				for i in range(len(bl)):
					if bl[i] == '1' and bans[i] == '0':
						bans = bans[:i] + '1' + bans[i + 1:]
					if bl[i] == '0' and bans[i] == '1':
						break
				ans = int('0b' + bans, 2)
				if fun(x, y, ans) == 0:
					return l
				else:
					return ans
			else:
				if len(bl) > len(bans):
					bans = bl[:len(bl) - len(bans)] + bans
				st1 = bans[:]
				check1 = int('0b' + st1, 2)
				while check1 > r or check1 < l:
					for i in range(len(bans)):
						if br[i] == '1' and st1[i] == '0':
							break
						if br[i] == '0' and st1[i] == '1':
							st1 = st1[:i] + '0' + st1[i + 1:]
					for i in range(len(bl)):
						if bl[i] == '1' and st1[i] == '0':
							st1 = st1[:i] + '1' + st1[i + 1:]
						if bl[i] == '0' and st1[i] == '1':
							break
					check1 = int('0b' + st1, 2)
				check1 = int('0b' + st1, 2)
				maxi = fun(x, y, check1)
				maxians = check1
				flag = -1
				for i in range(len(br)):
					if br[i] == '1' and (bx[i] == '0' and by[i] == '1' or (bx[i] == '1' and by[i] == '0')):
						flag = i
						st2 = st1[:flag] + '0' + bans[flag + 1:]
						check2 = int('0b' + st2, 2)
						if check2 < l:
							for i in range(i + 1, len(bl)):
								if bl[i] == '1' and st2[i] == '0':
									st2 = st2[:i] + '1' + st2[i + 1:]
								if bl[i] == '0' and st2[i] == '1':
									break
						check2 = int('0b' + st2, 2)
						if check2 < l:
							continue
						if maxi < fun(x, y, check2):
							maxi = fun(x, y, check2)
							maxians = check2
						elif maxi == fun(x, y, check2):
							maxians = min(check1, check2)
				maxians = int(maxians)
				if fun(x, y, maxians) == 0:
					return l
				else:
					return maxians
	print(final())


def checkset(n, k):
	if n & 1 << k - 1:
		return 1
	else:
		return 0

def setKthBit(n, k):
	return 1 << k | n

def correct(a, b, l, r):
	ans = -1
	cur = 0
	for i in range(l, r + 1):
		if (a & i) * (b & i) > ans:
			ans = (a & i) * (b & i)
			cur = i
		elif (a & i) * (b & i) == ans:
			if i < cur:
				cur = i
	return cur

def myfunc(a, b, pos, precompute):
	ans = 0
	for i in range(pos, 0, -1):
		x = checkset(a, i)
		y = checkset(b, i)
		if x == 1 and y == 0:
			ans += precompute[i + 1]
			break
		if x == 0 and y == 1:
			ans = setKthBit(ans, i - 1)
		if x == 1 and y == 1:
			ans = setKthBit(ans, i - 1)
	return ans

def func(x, y, l, r):
	temp = x | y
	if x == 0 or y == 0:
		return l
	if temp >= l and temp <= r:
		return temp
	precompute = [0, 0]
	prel = [0, 0]
	val = 0
	lval = 0
	curr = 1
	for i in range(1, 44):
		if checkset(temp, i):
			val += curr
		precompute.append(val)
		if checkset(l, i):
			lval += curr
		prel.append(lval)
		curr *= 2
	res = 0
	myarr = [l]
	for i in range(44, 0, -1):
		a = checkset(x, i)
		b = checkset(y, i)
		c = checkset(r, i)
		if c == 0:
			continue
		elif a == 0 and b == 0:
			val = res + precompute[i]
			if val < l:
				temp = res + (precompute[i] | prel[i])
				if temp < l:
					res = setKthBit(res, i - 1)
					continue
				else:
					val = res + myfunc(precompute[i], prel[i], i, precompute)
			myarr.append(val)
			res = 0
			break
		elif a == 1 and b == 1:
			res = setKthBit(res, i - 1)
		else:
			val = res + precompute[i]
			if val < l:
				temp = res + (precompute[i] | prel[i])
				if temp < l:
					res = setKthBit(res, i - 1)
					continue
				val = res + myfunc(precompute[i], prel[i], i, precompute)
			myarr.append(val)
			res = setKthBit(res, i - 1)
	myarr.append(res)
	ans = -1
	res1 = l
	for val in myarr:
		if val >= l and val <= r:
			a = x & val
			b = y & val
			cur = a * b
			if cur > ans:
				ans = cur
				res1 = val
			if cur == ans and val < res1:
				res1 = val
	return res1
t = int(input())
while t > 0:
	x = input().split(' ')
	a = int(x[0])
	b = int(x[1])
	l = int(x[2])
	r = int(x[3])
	print(func(a, b, l, r))
	t -= 1