#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<cmath>
#include<string>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 1005
#define M 128
#define seed 23333

using namespace std;
const int inf=(int)1e9;
int i,j,m,n,p,k,f[N][M],T,d,ans;
int main()
{
		scanf("%d",&T);
		for (;T--;)
		{
				scanf("%d%d",&n,&d);
				ans=0;
				for (i=6;i>=0;--i)
					if (((d&(1<<i))>0)^(n%2==0))
						ans+=n*(1<<i);
						else ans+=(n-1)*(1<<i);
				printf("%d\n",ans);
		}
}
