#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

int main()
{
  int i,j;
  int t,n,d;

  cin>>t;
  rep(i,t){
    cin>>n>>d;
    int sum=127*(n-1);
    if(n%2)
      sum+=d;
    else
      sum+=127-d;  
    cout<<sum<<endl;
  }
}
