#include <bits/stdc++.h>
#define foru(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
int t;
int n,d;
int main() {
//   freopen("input.txt","r",stdin);
   cin >> t;
   while (t--){
      int n, d;
      cin>>n>>d;
      int ans = 0;
      for(int i=0;i<7;++i){
         int bit=(d>>i)&1;
         if(bit==1){
            if(n%2==1)ans+=n*(1<<i);
            else ans+=(n-1)*(1<<i);
         }
         else{
            if(n%2==0)ans+=n*(1<<i);
            else ans+=(n-1)*(1<<i);
         }
//         cout<<ans<<endl;
      }
      cout<<ans<<endl;
   }
   return 0;
}
