def solve(): x, y = map(int, input().split()) while ((1 << y) & x) != 0: y += 1 print( (1 << y) - 1) T = int(input()) for _ in range(T): solve()