import sys sys.setrecursionlimit(5*10**5) input = sys.stdin.readline from collections import defaultdict, deque, Counter from heapq import heappop, heappush from bisect import bisect_left, bisect_right from math import gcd def sol(): x,y = map(int,input().split()) now = [] for i in range(65): now.append(x%2) x //= 2 while now[y+1] == 1: y += 1 print(pow(2,y+1)-1) return T = int(input()) for i in range(T): sol()