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: Accepted)

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i,a,n) assert(n < 0x80000000); for(int i = (a), i##_max = n; i < i##_max; ++i)
#define REM(i,a,n) assert(n < 0x80000000); for(int i = int(n) - 1, i##min = a; i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP {cin.tie(NULL); ios::sync_with_stdio(false);}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template<class T>
T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
template <class T>
T Next() { T buf; cin >> buf; return buf; }

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Union_Find.h"
#include "Graph.h"
#include "Range.h"
#include "Global.h"
#include "Flow_Solver.h"
#include "Tree.h"
#include "Suffix_Array.h"
#include "Geometry.h"
#include "Matrix.h"
#include "Segment_Tree.h"
#include "Rational.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
template<typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;	// コスト小を優先
template <class T>
auto Is(const T& value) { return [value](const auto& comparand) -> bool { return comparand == value; }; }
#endif

PP Extgcd(LL a, LL b) {
	if (b == 0) return PP(1, 0);
	const PP ref = Extgcd(b, a % b);
	const LL x = ref.first;
	const LL y = ref.second;
	return PP(y, x - (a / b) * y);
}

LL GCD(LL a, LL b) {
	if (b == 0) return a;
	return GCD(b, a % b);
}

LL LCM(LL a, LL b) {
	return a / GCD(a, b) * b;
}

map<LL, int> Factor(LL n) {
	map<LL, int> ret;
	for (LL i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			n /= i;
			++ret[i];
		}
	}
	if (n != 1) ++ret[n];
	return ret;
}

LL Pow(LL base, LL exp) {
	if (exp == 0) return 1;
	return base * Pow(base, exp - 1);
}

LL Carmichael(LL n) {
	auto factors = Factor(n);
	LL lcm = 1;
	for (auto p : factors) {
		int base = p.first;
		int exp = p.second;

		LL tmp;
		if (base == 2) {
			tmp = (exp <= 2 ? exp : 1 << (exp - 2));
		} else {
			tmp = Pow(base, exp - 1) * (base - 1);
		}
		lcm = LCM(lcm, tmp);
	}
	return lcm;
}

LL Powmod(LL base, LL exp, LL mod) {
	if (exp == 0) return 1;
	LL tmp = Powmod(base, exp / 2, mod);
	LL tmp2 = tmp * tmp % mod;
	if (exp % 2 == 0) {
		return tmp2;
	} else {
		return tmp2 * base % mod;
	} 
}

LL Sub(LL A, LL mod) {
	if (Powmod(A, INF, mod) == 0) {
		return mod;
	}
	LL carm = Carmichael(mod);

	LL G = GCD(carm, mod);
	LL x = -1, y = -1;
	if (G < 10000) {
		LL cnt = 0;
		LL prod = 1;
		while (cnt < 10000) {
			if ((prod - cnt) % G == 0) {
				y = cnt;
				x = prod;
				break;
			}
			prod = prod * A % mod;
			++cnt;
		}
	} else {
		y = Sub(A, G);
		x = Powmod(A, y, mod);
	}
	x = x * Powmod(A, carm, mod) % mod;

	if (y == -1) return -1;

	auto p = Extgcd(mod, carm);
	LL k = (x - y) / G * p.second % mod;
	LL ans = (k * carm + y) % (mod * carm);
	return ans >= 0 ? ans : ans + mod * carm;
}

int main() {
	int Q;
	cin >> Q;
	REP(i, 0, Q) {
		int a, b;
		cin >> a >> b;
		LL test = Sub(a, b);
		cout << test << endl;
	}

	return 0;
}