'''
  Title     : Day 29: Bitwise AND
  Domain    : Tutorials
  Author    : <NAME>
  Created   : 03 April 2019
'''
#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        nk = input().split()

        n = int(nk[0])

        k = int(nk[1])

        if (k | k - 1) <= n:
            print(k-1)
        else:
            print(k-2)

'''
  Title     : Day 29: Bitwise AND
  Domain    : Tutorials
  Author    : <NAME>
  Created   : 03 April 2019
'''
#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        nk = input().split()

        n = int(nk[0])

        k = int(nk[1])

        if (k | k - 1) <= n:
            print(k-1)
        else:
            print(k-2)

Title     : Day 29: Bitwise AND   Domain    : Tutorials   Author    : <NAME>   Created   : 03 April 2019 #!/bin/python3