Problem:
Score : 100 points

Problem Statement

A professor invented Cookie Breeding Machine for his students who like cookies very much.

When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine,
it consumes the cookie and generates two cookies with the taste of y and (x XOR y).

Here, XOR represents Bitwise Exclusive OR.

At first, there is only one cookie and the taste of it is D .

Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times.

Put one of the cookies into the machine.

Input a non-negative integer less than or equal to 127 on the machine.

Constraints

1 \leq T \leq 1000

1 \leq N_t \leq 1000 (1 \leq t \leq T)

1 \leq D_t \leq 127 (1 \leq t \leq T)

Input

The input is given from Standard Input in the following format:

T
N_1 D_1
:
N_T D_T

The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1.

Each test case is given on the next T lines.

In the t-th test case ( 1 \leq t \leq T ),
N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space.

Output

For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line.

Sample Input 1

3
3 1
4 108
1 10

Sample Output 1

255
400
10

On the 1st test case, if the machine is used as follows, three cookies with the taste of 61, 95 and 99 are generated.
Since the sum of these values is maximum among all possible ways, the answer is 255.

Put the cookie with the taste of 1 and input an integer 60 on the machine, lose the cookie and get two cookies with the taste of 60 and 61.

Put the cookie with the taste of 60 and input an integer 99 on the machine, lose the cookie and get two cookies with the taste of 99 and 95.

On the 3rd test case, the machine may not be used.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

/*************************************************************************
     File Name: C.cpp
     ID: obsoles1
     BLOG: blog.csdn.net/obsolescence
     LANG: C++ 
     Mail: 384099319@qq.com 
     Created Time: 日 10/ 2 12:25:36 2016
 ************************************************************************/
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
#define ABS(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))
#define MEM0(x) memset(x,0,sizeof(x))
#define MEM1(x) memset(x,-1,sizeof(x))
#define MEMX(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int sum;

int solve(int i,int x) {
  int bit=7-i,xx=x,y=0,cnt=0,b[10],j;
  MEM0(b);
  for (; xx; xx>>=1) b[cnt++]=xx&1;
  int m=cnt;
  //cout<<"m="<<m<<" bit="<<bit<<endl;
  for (j=0; j<MIN(m,bit); ++j) y+=((!b[j])<<j);
  for (j=MIN(m,bit); j<bit; ++j) y+=((!b[j])<<j);
  //for (j=bit; j<m; ++j) y+=(b[j]<<j);
  //for (j=0; j<m; ++j) cout<<b[j]<<" \n"[j==m-1];
  //cout<<"y="<<y<<endl;
  //cout<<"cnt="<<cnt<<endl;
  sum+=(x^y);
  //cout<<"x^y="<<(x^y)<<endl;
  //cout<<"sum="<<sum<<endl;
  return y;
}

int main() {
  int t,n,x,i;
  cin>>t;
  for (int nc=1; nc<=t; ++nc) {
    cin>>n>>x;
    int xx=x;
    //cout<<"n="<<n<<" x="<<x<<endl;
    sum=0;
    //for (i=n-2; i>=0; --i) {
    if (n>2) x=solve(1,x);
    for (i=0; i<n-3; ++i) x=solve(1,x);
    if (n>1) x=solve(0,x);
    //}
    sum+=x;
    int sum1=sum;
    sum=0;
    x=xx;
    //cout<<"--------"<<endl;
    if (n>2) x=solve(0,x);
    for (i=0; i<n-3; ++i) x=solve(0,x);
    if (n>1) x=solve(0,x);
    //cout<<"x="<<x<<endl;
    sum+=x;
    //cout<<"sum="<<sum<<" sum1="<<sum1<<endl;
    sum=MAX(sum,sum1);
    cout<<sum<<endl;
  }
}