for _ in range(int(input())):
    X, Y = map(int, input().split())

    while 1<<Y & X:
        Y += 1
    Y -= 1

    print(2**(Y+1)-1)