t=int(input()) def f(a,b): a=str(bin(a)) key=-b-1 while a[key]=="1": key-=1 key+=1 key*=-1 print(2**key-1) for i in range(t): a,b=map(int,input().split()) f(a,b)