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;
template <typename T>
T pow(T a, T e, T mod) {
  if (!e) return 1;
  T x = pow(a, e / 2, mod);
  x = x * x % mod;
  return (e & 1) ? x * a % mod : x;
}
template <typename T, typename exp_t, T (*multiply)(T, T)>
T pow(T a, exp_t e) {
  if (!e) return 1;
  T x = pow<T, exp_t, multiply>(a, e / 2);
  x = multiply(x, x);
  return (e & 1) ? multiply(x, a) : x;
}
template <typename CRT_t>
CRT_t CRT(const CRT_t* rem, const CRT_t* p_v, const int* p_e, const int p_n) {
  CRT_t ret = 0, mod = 1;
  for (int i = 0; i < p_n; i++)
    for (int j = 0; j < p_e[i]; j++) mod *= p_v[i];
  for (int i = 0; i < p_n; i++) {
    CRT_t pr = mod, pw = 1;
    for (int j = 0; j < p_e[i]; j++) pr /= p_v[i], pw *= p_v[i];
    CRT_t inv = pow(pr % pw, pw - pw / p_v[i] - 1, pw);
    CRT_t add = inv * rem[i] % pw * pr;
    if (add >= mod - ret)
      ret += add - mod;
    else
      ret += add;
  }
  return ret;
}
template <typename BSGS_t, typename exp_t, BSGS_t (*multiply)(BSGS_t, BSGS_t)>
exp_t BSGS(const BSGS_t a, const BSGS_t b, const exp_t order) {
  exp_t sqrt = 1;
  while (sqrt * sqrt < order) sqrt++;
  static pair<BSGS_t, exp_t> try_small[20000];
  static pair<BSGS_t, exp_t> try_large[20000];
  BSGS_t cur = 1;
  for (BSGS_t i = 0; i < sqrt; i++) {
    try_small[i] = {cur, i};
    cur = multiply(cur, a);
  }
  cur = b;
  BSGS_t a_pw = pow<BSGS_t, exp_t, multiply>(a, order - sqrt);
  for (exp_t i = 0; i < sqrt; i++) {
    try_large[i] = {cur, i};
    cur = multiply(cur, a_pw);
  }
  sort(try_small, try_small + sqrt);
  sort(try_large, try_large + sqrt);
  for (exp_t i = 0, j = 0; i < sqrt; i++) {
    while (j < sqrt && try_large[j].first < try_small[i].first) j++;
    if (j == sqrt) return exp_t(-1);
    if (try_large[j].first == try_small[i].first)
      return try_large[j].second * sqrt + try_small[i].second;
  }
  return exp_t(-1);
}
template <typename PH_t, typename exp_t, PH_t (*multiply)(PH_t, PH_t)>
exp_t PohligHellman_pw(const PH_t g, const PH_t a, const exp_t p_v,
                       const int p_e) {
  exp_t ret = 0, pw_down = 1, pw_up = 1;
  for (int k = 0; k < p_e - 1; k++) pw_up *= p_v;
  exp_t pw = pw_up * p_v;
  PH_t g_pw = pow<PH_t, exp_t, multiply>(g, pw_up);
  for (int k = 0; k < p_e; k++) {
    PH_t h = pow<PH_t, exp_t, multiply>(
        multiply(pow<PH_t, exp_t, multiply>(g, pw - ret), a), pw_up);
    exp_t d = BSGS<PH_t, exp_t, multiply>(g_pw, h, p_v);
    if (d == exp_t(-1)) return exp_t(-1);
    ret += pw_down * d;
    pw_down *= p_v, pw_up /= p_v;
  }
  return ret;
}
template <typename PH_t, typename exp_t, PH_t (*multiply)(PH_t, PH_t)>
exp_t PohligHellman(const PH_t g, const PH_t a, const exp_t* p_v,
                    const int* p_e, const int p_n) {
  static exp_t e_rem[100];
  exp_t order = 1;
  for (int i = 0; i < p_n; i++)
    for (int j = 0; j < p_e[i]; j++) order *= p_v[i];
  for (int i = 0; i < p_n; i++) {
    exp_t pw = 1;
    for (int j = 0; j < p_e[i]; j++) pw *= p_v[i];
    PH_t g_pow = pow<PH_t, exp_t, multiply>(g, order / pw);
    PH_t a_pow = pow<PH_t, exp_t, multiply>(a, order / pw);
    e_rem[i] =
        PohligHellman_pw<PH_t, exp_t, multiply>(g_pow, a_pow, p_v[i], p_e[i]);
    if (e_rem[i] == exp_t(-1)) return exp_t(-1);
  }
  return CRT(e_rem, p_v, p_e, p_n);
}
template <typename PH_t, typename exp_t, PH_t (*multiply)(PH_t, PH_t)>
PH_t DiscreteLog(const PH_t a, const PH_t b, const exp_t order) {
  static exp_t p_v[100];
  static int p_e[100];
  int p_n = 0;
  exp_t o = order;
  for (exp_t i = 2; i * i <= o; i++)
    if (o % i == 0) {
      p_v[p_n] = i;
      p_e[p_n] = 0;
      while (o % i == 0) p_e[p_n]++, o /= i;
      p_n++;
    }
  if (o > 1) {
    p_v[p_n] = o;
    p_e[p_n] = 1;
    p_n++;
  }
  mt19937_64 rng(a * b);
  PH_t g = 0;
  while (true) {
    PH_t try_g = g ? g : (rng() % order);
    if (!try_g) continue;
    exp_t e_a = PohligHellman<PH_t, exp_t, multiply>(try_g, a, p_v, p_e, p_n);
    if (e_a == exp_t(-1)) {
      try_g = 0;
      continue;
    }
    exp_t e_b = PohligHellman<PH_t, exp_t, multiply>(try_g, b, p_v, p_e, p_n);
    if (e_b == exp_t(-1)) {
      try_g = 0;
      continue;
    }
    g = try_g;
    static exp_t e_rem[100];
    for (int i = 0; i < p_n; i++) {
      exp_t pw = 1;
      for (int j = 0; j < p_e[i]; j++) pw *= p_v[i];
      exp_t e_a_rem = e_a % pw, e_b_rem = e_b % pw;
      while (pw > 1 && e_a_rem % p_v[i] == 0) {
        if (e_b_rem % p_v[i] != 0) return -1;
        pw /= p_v[i], e_a_rem /= p_v[i], e_b_rem /= p_v[i];
      }
      e_rem[i] =
          (pw != 1) ? e_b_rem * pow(e_a_rem, pw - pw / p_v[i] - 1, pw) % pw : 0;
    }
    return CRT(e_rem, p_v, p_e, p_n);
  }
}
using cat = unsigned long long;
cat multiply_nocache(cat a, cat b);
cat cache[64][64];
char cache_used[64][64];
cat multiply_pw2(int a, int b) {
  a &= 63, b &= 63;
  if (cache_used[a][b]) return cache[a][b];
  if (a == 0 || b == 0) {
    cache_used[a][b] = 1;
    cache[a][b] = (1ULL << a) * (1ULL << b);
    return (1ULL << a) * (1ULL << b);
  }
  int n = 0;
  while (n + 1 < 6 && ((1 << (n + 1)) <= a || (1 << (n + 1)) <= b)) n++;
  int pw = 1 << n;
  if (a >= pw && b >= pw) {
    cache_used[a][b] = 1;
    cache[a][b] =
        multiply_nocache(3ULL << (pw - 1), multiply_pw2(a ^ pw, b ^ pw));
    return cache[a][b];
  }
  if (a >= pw) {
    cache_used[a][b] = 1;
    cache[a][b] = multiply_pw2(a ^ pw, b) << pw;
    return cache[a][b];
  }
  if (b >= pw) {
    cache_used[a][b] = 1;
    cache[a][b] = multiply_pw2(a, b ^ pw) << pw;
    return cache[a][b];
  }
  assert(false);
  return -1;
}
cat multiply_nocache(cat a, cat b) {
  cat ret = 0;
  for (int i = 0; a; i++, a /= 2)
    if (a & 1) {
      cat b_c = b;
      for (int j = 0; b_c; j++, b_c /= 2)
        if (b_c & 1) ret ^= multiply_pw2(i, j);
    }
  return ret;
}
cat __attribute__((optimize("-Ofast,-funroll-loops")))
__attribute__((target("arch=core-avx2"))) multiply(cat a, cat b) {
  cat ret = 0;
  for (int i = 0; a; i++, a /= 2)
    if (a & 1) {
      cat b_c = b;
      for (int j = 0; j != 64; j++, b_c /= 2) ret ^= cache[i][j] & (-(b_c & 1));
    }
  return ret;
}
int main() {
  for (int i = 0; i < 64; i++)
    for (int j = 0; j < 64; j++) multiply_nocache(1ULL << i, 1ULL << j);
  int T;
  cin >> T;
  while (T--) {
    cat a, b;
    cin >> a >> b;
    cat ans = DiscreteLog<cat, cat, &multiply>(a, b, 0xFFFFFFFFFFFFFFFF);
    if (ans + 1 == 0)
      cout << "-1\n";
    else
      cout << ans << "\n";
  }
}