#include <iostream>
#include <vector>

using namespace std;

int main(){
	int T, N, D;
	cin>>T;
	for(int i=0; i<T; i++){
		cin>>N>>D;
		cout<<127*(N-1)+((N-1)%2 ? (int)(unsigned char)(~D)-128 : D)<<endl;
	}

	return 0;
}