#include<cstdio>
#include<iostream>
#include<cstring>
#define RG register
using namespace std;
int n,d,T;
template<typename I> inline void read(I &ot){
	I ch=getchar(), x=0, f=1;
	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}
	ot=x*f;}
template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}
int main()
{
	//freopen("Cookie Breeding Machine.in","r",stdin);
	//freopen(".out","w",stdout);
	read(T);
	while(T--)
	{
		read(n,d);
		if((n-1)%2==0) printf("%d\n",127*(n-1)+d);
		else printf("%d\n",127*(n-1)+(127^d));
	}
	//fclose(stdin); fclose(stdout);
	return 0;
}