#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
using ll = unsigned long long;
ll a, b;
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;
// cout << a;
ll x = 1;
int iters = 0;
while (true) {
if (iters % 1000 == 0) {
x = (ll)( ((ll)rand() << 48) | ((ll)rand() << 32)| ((ll)rand() << 16)| (ll)rand());
}
// cout << x << endl;
iters += 1;
ll r = f(x);
// cout << r << endl;
if (r == x || iters > 2000) {
break;
} else {
x = r;
}
}
if (iters > 2000) {
cout << "-\n";
} else {
cout << x << "\n";
}
}
return 0;
}