#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>

#define DIV 1000000007

using namespace std;

long long T;

long long N[1005];
long long D[1005];



int main(){
	cin >> T;
	long origin = 127;
	for(int i = 0; i < T; i++){
		cin >> N[i] >> D[i];
	}

	for(int i = 0; i < T; i++){
		N[i]--;
		if(N[i]%2 == 1){
			cout << 127 * N[i] + (127 ^ D[i]) << endl;
		}else{
			cout << 127 * N[i] + D[i] << endl;
		}
	}
}