import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random #sys.setrecursionlimit(10**9) t = int(input()) for i in range(t): x,y = map(int,input().split()) for j in range(60): if j < y and ((x >> j) & 1) == 0: x += 2**j temp = 0 for j in range(60): if ((x >> j) & 1) == 1: temp += 2**j else: break print(temp)