#include <stdio.h>
#include <bits/stdc++.h>

 
#include <vector>
#include <utility>


#include <algorithm>


int go(int n, int d) {
	int ans = 0;

	for (int bit = 64; bit > 0; bit >>= 1) {
		int least_odd = n;
		int least_even = n;

		if (n % 2 == 1) least_even--;
		else least_odd--;
		
		if (d & bit) ans += bit * least_odd;
		else ans += bit * least_even;
	}

	return ans;
}

void solve() {
	int t;
	for (scanf("%d", &t); t; t--) {
		int n, d;
		scanf("%d %d", &n, &d);
		printf("%d\n", go(n,d));
	}
}


int main() {
    solve();
}


