#include <iostream>
using namespace std;
int f(int n, int k) {
if (!n) return 0;
int tmp = 1 << (n - 1);
if (k < tmp) return f(n - 1, k);
return tmp + f(n - 1, (1 << n) - 1 - k);
}
int main() {
int N, n, k;
cin >> N;
for (int i = N; i > 0; i--) {
cin >> n >> k;
cout << f(n,k) << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCmludCBmKGludCBuLCBpbnQgaykgewogICAgaWYgKCFuKSByZXR1cm4gMDsKICAgIGludCB0bXAgPSAxIDw8IChuIC0gMSk7CiAgICBpZiAoayA8IHRtcCkgcmV0dXJuIGYobiAtIDEsIGspOwogICAgcmV0dXJuIHRtcCArIGYobiAtIDEsICgxIDw8IG4pIC0gMSAtIGspOwp9CiAKaW50IG1haW4oKSB7CiAgICBpbnQgTiwgbiwgazsKICAgIGNpbiA+PiBOOwogICAgZm9yIChpbnQgaSA9IE47IGkgPiAwOyBpLS0pICB7CiAgICAgICAgY2luID4+IG4gPj4gazsKICAgICAgICBjb3V0IDw8IGYobixrKSA8PCBlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0=