Problem:
Score : 1400 points

Problem Statement

Process the Q queries below.

You are given two integers A_i and M_i. Determine whether there exists a positive integer K_i not exceeding 2 × 10^{18} such that A_i^{K_i} ≡ K_i (mod M_i), and find one if it exists.

Constraints

1 \leq Q \leq 100

0 \leq A_i \leq 10^9(1 \leq i \leq Q)

1 \leq M_i \leq 10^9(1 \leq i \leq Q)

Inputs

Input is given from Standard Input in the following format:

Q
A_1 M_1
:
A_Q M_Q

Outputs

In the i-th line, print -1 if there is no integer K_i that satisfies the condition.
Otherwise, print an integer K_i not exceeding 2 × 10^{18} such that A_i^{K_i} ≡ K_i (mod M_i). If there are multiple solutions, any of them will be accepted.

Sample Input 1

4
2 4
3 8
9 6
10 7

Sample Output 1

4
11
9
2

It can be seen that the condition is satisfied: 2^4 = 16 ≡ 4 (mod 4), 3^{11} = 177147 ≡ 11 (mod 8), 9^9 = 387420489 ≡ 9 (mod 6) and 10^2 = 100 ≡ 2 (mod 7).

Sample Input 2

3
177 168
2028 88772
123456789 987654321

Sample Output 2

7953
234831584
471523108231963269

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

/**
 *    author:  tourist
 *    created: 30.09.2017 15:59:34       
**/
#include <bits/stdc++.h>

using namespace std;

const int md = -1;

inline unsigned fasterLLMod(unsigned long long x, unsigned y) {
  return x % y;
}

inline void add(int &a, int b, const int m = md) {
  a += b;
  if (a >= m) a -= m;
}

inline void sub(int &a, int b, const int m = md) {
  a -= b;
  if (a < 0) a += m;
}

inline int mul(int a, int b, const int m = md) {
  return fasterLLMod((long long) a * b, m);
}

inline int power(int a, long long b, const int m = md) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a, m);
    }
    a = mul(a, a, m);
    b >>= 1;
  }
  return res;
}

inline int inv(int a, const int m = md) {
  a %= m;
  if (a < 0) a += m;
  int b = m, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += m;
  return u;
}

int get_phi(int m) {
  int phi = m;
  int tmp = m;
  for (int i = 2; i * i <= tmp; i++) {
    if (tmp % i == 0) {
      phi /= i;
      phi *= (i - 1);
      while (tmp % i == 0) {
        tmp /= i;
      }
    }
  }
  if (tmp > 1) {
    phi /= tmp;
    phi *= tmp - 1;
  }
  return phi;
}

long long solve(int a, int m) {
  if (m == 1) {
    return 2;
  }
  int aa = 1;
  for (int i = 0; i < 33; i++) {
    aa = mul(aa, a, m);
  }
  int g = __gcd(aa, m);
  if (g == m) {
    return m;
  }
  int phi = get_phi(m / g);
/*  if (__gcd(a, m) != 1) {
    int new_a = power(a, g, m);
    cerr << "new_a = " << new_a << ", m = " << m << endl;
    cerr << "g = " << g << endl;
    long long k = solve(new_a, m / g);
    while (power(new_a, k, m) != k % m) {
      k += m / g;
    }
    return k * g;//solve(new_a, m / g) * g;
    // !!
//    return -1;
  }*/
  g = __gcd(m, phi);
  long long k = solve(a, g);
  int value = power(a, k, m);
  int x = value - k;
  if (x < 0) {
    x += m;
  }
  assert(x % g == 0);
  x /= g;
  x = mul(x, inv(phi / g, m / g), m / g);
  return k + (long long) (x + m / g) * phi;
}

int main() {
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    int a, m;
    scanf("%d %d", &a, &m);
    long long k = solve(a, m);
    cout << k << endl;
    assert(power(a, k, m) == k % m);
  }
  return 0;
}