T = int(input()) for _ in range(T): x, y = map(int, input().split()) B = [] while x > 0: B.append(x % 2) x //= 2 NB = len(B) ans = 0 while y < NB and B[y] == 1: y += 1 print((1 << y) - 1)