Problem: In modern cryptography much is tied to the algorithmic complexity of solving several problems. One of such problems is a discrete logarithm problem. It is formulated as follows: 

Let's fix a [finite field](https://en.wikipedia.org/wiki/Finite_field) and two it's elements a and b. One need to fun such x that a^x = b or detect there is no such x. 

It is most likely that modern mankind cannot solve the problem of discrete logarithm for a sufficiently large field size. For example, for a field of residues modulo prime number, primes of 1024 or 2048 bits are considered to be safe. However, calculations with such large numbers can place a significant load on servers that perform cryptographic operations. For this reason, instead of a simple module residue field, more complex fields are often used. For such field no fast algorithms that use a field structure are known, smaller fields can be used and operations can be properly optimized. 

Developer Nikolai does not trust the generally accepted methods, so he wants to invent his own. Recently, he read about a very strange field — [nimbers](https://en.wikipedia.org/wiki/Nimber), and thinks it's a great fit for the purpose. 

The field of nimbers is defined on a set of integers from 0 to 2^{2^k} - 1 for some positive integer k . Bitwise exclusive or (⊕) operation is used as addition. One of ways to define multiplication operation (⊙) is following properties: 

  * 0 ⊙ a = a ⊙ 0 = 0 
  * 1 ⊙ a = a ⊙ 1 = a 
  * a ⊙ b = b ⊙ a 
  * a ⊙ (b ⊙ c)= (a ⊙ b) ⊙ c 
  * a ⊙ (b ⊕ c) = (a ⊙ b) ⊕ (a ⊙ c) 
  * If a = 2^{2^n} for some integer n > 0, and b < a, then a ⊙ b = a ⋅ b. 
  * If a = 2^{2^n} for some integer n > 0, then a ⊙ a = 3/2⋅ a. 



For example: 

  *  4 ⊙ 4 = 6 
  *  8 ⊙ 8 = 4 ⊙ 2 ⊙ 4 ⊙ 2 = 4 ⊙ 4 ⊙ 2 ⊙ 2 = 6 ⊙ 3 = (4 ⊕ 2) ⊙ 3 = (4 ⊙ 3) ⊕ (2 ⊙ (2 ⊕ 1)) = (4 ⊙ 3) ⊕ (2 ⊙ 2) ⊕ (2 ⊙ 1) = 12 ⊕ 3 ⊕ 2 = 13. 
  * 32 ⊙ 64 = (16 ⊙ 2) ⊙ (16 ⊙ 4) = (16 ⊙ 16) ⊙ (2 ⊙ 4) = 24 ⊙ 8 = (16 ⊕ 8) ⊙ 8 = (16 ⊙ 8) ⊕ (8 ⊙ 8) = 128 ⊕ 13 = 141 
  * 5 ⊙ 6 = (4 ⊕ 1) ⊙ (4 ⊕ 2) = (4⊙ 4) ⊕ (4 ⊙ 2) ⊕ (4 ⊙ 1) ⊕ (1 ⊙ 2) = 6 ⊕ 8 ⊕ 4 ⊕ 2 = 8 



Formally, this algorithm can be described by following pseudo-code. 
    
    
    multiply(a, b) {  
       ans = 0  
       for p1 in bits(a)       // numbers of bits of a equal to one  
           for p2 in bits(b)   // numbers of bits of b equal to one  
              ans = ans xor multiply_powers_of_2(1 << p1, 1 << p2)  
       return ans;  
    }  
    multiply_powers_of_2(a, b) {  
        if (a == 1 or b == 1) return a * b  
        n = maximal value, such 2^{2^{n}} <= max(a, b)  
        power = 2^{2^{n}};  
        if (a >= power and 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  
        }  
    }  
    

It can be shown, that this operations really forms a field. Moreover, than can make sense as game theory operations, but that's not related to problem much. With the help of appropriate caching and grouping of operations, it is possible to calculate the product quickly enough, which is important to improve speed of the cryptoalgorithm. More formal definitions as well as additional properties can be clarified in the wikipedia article at [link](https://en.wikipedia.org/wiki/Nimber). The authors of the task hope that the properties listed in the statement should be enough for the solution. 

Powering for such muliplication is defined in same way, formally a^{⊙ k} = \underbrace{a ⊙ a ⊙ ⋅⋅⋅ ⊙ a}_{k~times}.

You need to analyze the proposed scheme strength. For pairs of numbers a and b you need to find such x, that a^{⊙ x} = b, or determine that it doesn't exist. 

Input

In the first line of input there is single integer t (1 ≤ t ≤ 100) — number of pairs, for which you need to find the discrete logarithm.

In each of next t line there is a pair of integers a b (1 ≤ a, b < 2^{64}). 

Output

For each pair you should print one integer x (0 ≤ x < 2^{64}), such that a^{⊙ x} = b, or -1 if no such x exists. It can be shown, that if any such x exists, there is one inside given bounds. If there are several good values, you can output any of them. 

Example

Input


7
2 2
1 1
2 3
8 10
8 2
321321321321 2
123214213213 4356903202345442785


Output


1
1
2
4
-1
6148914691236517205
68943624821423112

Solution (in CPP):

Solution:
#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];
  }
}
unordered_map<unsigned long long, int> mp;
void solve() {
  unsigned long long a, b;
  cin >> a >> b;
  vector<int> f, v;
  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);
    }
  }
  mp.max_load_factor(0.25);
  mp.reserve(1 << 7);
  int t = 1;
  cin >> t;
  while (t--) solve();
  42;
}