#include"bits/stdc++.h"
using namespace std;
using ll = int64_t;

int main() {
    ll T;
    cin >> T;
    vector<ll> N(T), D(T);
    for (ll i = 0; i < T; i++) {
        cin >> N[i] >> D[i];
        cout << 127 * (N[i] - 1) + (N[i] % 2 ? D[i] : 127 - D[i]) << endl;
    }
}