#include <bits/stdc++.h>
using namespace std;
const int inf_int = 1e9 + 100;
const long long inf_ll = 1e18;
const double pi = 3.1415926535898;
template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
  out << "( " << rhs.first << " , " << rhs.second << " )";
  return out;
}
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A>
string to_string(vector<vector<A>> v) {
  bool first = true;
  string res = "\n{";
  for (const auto &x : v) {
    if (!first) {
      res += ",\n ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}\n";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
bool debug = 0;
const int MAXN = 3000 + 100;
const int LOG = 20;
const int mod = 998244353;
const int MX = (1e7 + 100);
const unsigned long long one = 1;
unsigned long long multiply(unsigned long long a, unsigned long long b);
unsigned long long multiply_powers_of_2(unsigned long long a,
                                        unsigned long long b) {
  if (a == 1 || b == 1) return a * b;
  unsigned long long n = 5;
  while ((one << (one << n)) > max(a, b)) {
    --n;
  }
  unsigned long long power = (one << (one << n));
  if (a >= power && b >= power) {
    return multiply(power * 3 / 2, multiply_powers_of_2(a / power, b / power));
  } else if (a >= power) {
    return multiply_powers_of_2(a / power, b) * power;
  } else {
    return multiply_powers_of_2(a, b / power) * power;
  }
}
unsigned long long multiply(unsigned long long a, unsigned long long b) {
  unsigned long long ans = 0;
  for (int i = 0; i < 64; i++) {
    if (a & (one << i)) {
      for (int j = 0; j < 64; j++) {
        if (b & (one << j)) {
          ans ^= multiply_powers_of_2(one << i, one << j);
        }
      }
    }
  }
  return ans;
}
const unsigned long long M = ((one << 63) - 1) * 2 + 1;
vector<int> factors = {5, 641, 6700417, 65537, 257, 17, 3};
unsigned long long save[64][64];
unsigned long long my_mul(unsigned long long a, unsigned long long b) {
  unsigned long long res = 0;
  for (int i = 0; i < 64; ++i) {
    if ((a >> i) & 1) {
      for (int j = 0; j < 64; ++j) {
        if ((b >> j) & 1) {
          res ^= save[i][j];
        }
      }
    }
  }
  return res;
}
unsigned long long bin_pow(unsigned long long a, unsigned long long n) {
  unsigned long long res = 1;
  while (n) {
    if (n & 1) res = my_mul(a, res);
    a = my_mul(a, a);
    n >>= 1;
  }
  return res;
}
struct super_mul {
  unsigned long long mem[64];
  void init(unsigned long long a) {
    for (int i = 0; i < 64; ++i) {
      mem[i] = 0;
      for (int j = 0; j < 64; ++j) {
        if ((a >> j) & 1) {
          mem[i] ^= save[i][j];
        }
      }
    }
  }
  unsigned long long mult(unsigned long long a) {
    unsigned long long res = 0;
    for (int i = 0; i < 64; ++i) {
      if ((a >> i) & 1) res ^= mem[i];
    }
    return res;
  }
};
template <typename T>
void crt(const vector<int> &p, const vector<int> &a, T &res) {
  assert(p.size() == a.size());
  auto inverse = [&](int q, int m) {
    q %= m;
    if (q < 0) q += m;
    int b = m, u = 0, v = 1;
    while (q) {
      int t = b / q;
      b -= t * q;
      swap(q, b);
      u -= t * v;
      swap(u, v);
    }
    assert(b == 1);
    if (u < 0) u += m;
    return u;
  };
  vector<int> x(p.size());
  for (int i = 0; i < (int)p.size(); i++) {
    assert(0 <= a[i] && a[i] < p[i]);
    x[i] = a[i];
    for (int j = 0; j < i; j++) {
      x[i] = (int)((long long)(x[i] - x[j]) * inverse(p[j], p[i]) % p[i]);
      if (x[i] < 0) x[i] += p[i];
    }
  }
  res = 0;
  for (int i = (int)p.size() - 1; i >= 0; i--) {
    res = res * p[i] + x[i];
  }
}
void solve() {
  unsigned long long a, b;
  cin >> a >> b;
  vector<int> f, v;
  unordered_map<unsigned long long, int> mp;
  mp.max_load_factor(0.25);
  mp.reserve(1 << 18);
  for (int i = 0; i < (int(factors.size())); ++i) {
    unsigned long long A = bin_pow(a, M / factors[i]);
    unsigned long long B = bin_pow(b, M / factors[i]);
    42;
    if (A == B && A == 1) {
      continue;
    }
    if (A == B) {
      f.push_back(factors[i]);
      v.push_back(1);
      continue;
    }
    int n = sqrt(factors[i]) + 1;
    super_mul mul_step;
    mul_step.init(A);
    unsigned long long big_step = 1;
    for (int i = 0; i < n; ++i) {
      big_step = mul_step.mult(big_step);
    }
    42;
    super_mul mul_big;
    mul_big.init(big_step);
    unsigned long long val = big_step;
    mp.clear();
    for (int i = 1; i <= n; ++i) {
      if (!mp.count(val)) {
        mp[val] = i;
      }
      val = mul_big.mult(val);
    }
    42;
    int res = inf_int;
    if (false && A == B) {
      res = 1;
    } else {
      unsigned long long val = B;
      for (int i = 0; i <= n; ++i) {
        if (mp.count(val)) {
          res = min(mp[val] * n - i, res);
        }
        val = mul_step.mult(val);
      }
    }
    if (res == inf_int) {
      cout << -1 << "\n";
      return;
    }
    f.push_back(factors[i]);
    v.push_back(res);
    42;
  }
  if ((int(f.size())) == 0) {
    cout << 1 << "\n";
    return;
  }
  unsigned long long ans = 0;
  crt(f, v, ans);
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(15);
  for (int i = 0; i < 64; ++i) {
    for (int j = 0; j < 64; ++j) {
      save[i][j] = multiply_powers_of_2(one << i, one << j);
    }
  }
  int t = 1;
  cin >> t;
  while (t--) solve();
  42;
}
