#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T; cin >> T;
    rep(i ,T) {
        int N, D;
        cin >> N >> D;
        if (N % 2 == 0) D ^= 127;
        cout << 127 * (N-1) + D << endl;
    }

    return 0;
}
