#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 100005
#define maxm 6000005
#define mod 10007
#define INF 1000000007
#define EPS 1e-7
#define PI 3.1415926535898
#define N 4294967296
using namespace std;
//-------------------------CHC------------------------------//
int main() {
int T;
cin >> T;
while (T--) {
int D, I;
cin >> D >> I;
int t = 1;
for (int i = 1; i < D; ++i) {
if (I & 1) t <<= 1, I = (I + 1) >> 1;
else t = t << 1 | 1, I >>= 1;
}
printf("%d\n", t);
}
return 0;
}
bool vis[1 << 20];
int main() {
int T;
scanf("%d", &T);
while (T--) {
CLEAR(vis, 0);
int D, I;
scanf("%d%d", &D, &I);
int id = 1 << (D - 1), ans;
for (int i = 1; i <= I; ++i) {
int s = 1;
while (1) {
int t = s;
s = vis[s] ? (s << 1 | 1) : (s << 1);
vis[t] = !vis[t];
if (s >= id) break;
}
if (i == I) ans = s;
}
printf("%d\n", ans);
}
return 0;
}