import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 t = ii() for _ in range(t): x, y = mi() for bit in range(y, 60): if 1 & (x >> bit): y = bit else: break x = 2 ** (y + 1) - 1 ans = 0 for bit in range(0, 60): if 1 & (x >> bit): ans += 1 << bit print(ans)