import java.util.;
import java.util.Arrays;
import java.math.BigInteger;
public class Main {
static HashSet set = new HashSet<>();
static boolean flag = false;
static long getCount(long n , long total , long i, long height)
{
if(i>total)
{
return 0;
}
if((i+1-(long)Math.pow(2,height)) == n-1)
{
flag = true;
return 1;
}
long ans2 = 0;
long ans1 = getCount(n, total, 2i+1,height + 1);
if(flag == false)
ans2 = getCount(n, total, 2*i+2, height +1);
return 1+ans1+ans2;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int h = sc.nextInt();
long n = sc.nextLong();
long i = 0;
long total = (long)Math.pow(2, h+1);
set.clear();
flag = false;
System.out.println(getCount(n,total,i,0));
t–;
}
}
}