#include <bits/stdc++.h>

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template<typename P, typename Q>
ostream& operator << (ostream& os, pair<P, Q> p)
{
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

string n2bit(int n)
{
  string s;
  while (s.size() < 8 && n) {
    s += '0' + n % 2;
    n /= 2;
  }
  reverse(s.begin(), s.end());
  return s;
}

int main(int argc, char *argv[])
{
  int tc;
  cin >> tc;
  while (tc--) {
    int n, d;
    cin >> n >> d;
    if (n == 1) {
      cout << d << endl;
      continue;
    }
  
    vector<int> v;
    const int mask = (1 << 7) - 1;
    d = mask ^ d;
    --n;
    v.push_back(mask);
    while (v.size() < n) {
      v.push_back(mask);
      d = mask ^ d;
    }
    v.push_back(d);
    
    int sum = 0;
    each (i, v) sum += i;
    cout << sum << endl;
    // each (i, v) cout << n2bit(i) << endl; cout << endl;

    
  }
  return 0;
}
