#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int a, b;
using ll = long long;
ll f(ll x) {
return ((a * x) ^ b);
}
int main(int argc, char *argv[])
{
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> a >> b;
ll x = 1;
int iters = 0;
while (true) {
if (iters % 100 == 0) {
x = rand();
}
// cout << x << endl;
iters += 1;
ll r = f(x);
// cout << r << endl;
if (r == x || iters > 10000) {
break;
} else {
x = r;
}
}
if (iters > 10000) {
cout << "-\n";
} else {
cout << x << "\n";
}
}
return 0;
}