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)

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int f(int t) {
    int maxi = 0;
    int ret = 0;
    rep(i, 128) {
        int s = i + (t ^ i);
        if (maxi < s) {
            ret = i;
            maxi = s;
        }
    }
    return ret;
}
int N, D;
void solve() {
    cin >> N >> D;
    int A[128] = {};
    rep(i, 128) A[i] = INF;
    A[D] = 1;
    int sum = 0;
    rep(i, N - 1) {
        int idx = min_element(A, A + 128) - A;

        // cout << idx << endl;
        A[idx]--;
        if (A[idx] == 0) A[idx] = INF;
        int t = f(idx);
        if (A[t] == INF)
            A[t] = 1;
        else
            A[t]++;
        if (A[t ^ idx] == INF)
            A[t ^ idx] = 1;
        else
            A[t ^ idx]++;
    }
    rep(i, 128) {
        if (A[i] != INF) sum += i * A[i];
    }
    cout << sum << endl;
}

int main() {
    int T;
    cin >> T;
    rep(i, T) solve();
    return 0;
}