#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	int n,d;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&d);
		if(n&1)
			printf("%d\n",(n-1)*127+d);
		else
			printf("%d\n",(n-1)*127+(d^127));
	}
	return 0;
}