import std.stdio; import std.algorithm; import std.array; import std.math; bool[] notPrime; void main() { uint t; readf("%s\n", &t); const int PMAX = 5000000; notPrime = generateNotPrime(PMAX); foreach(i; 0..t) { ulong a; uint p; readf("%s %s\n", &a, &p); if (notPrime[p]) { writeln("-1"); continue; } a %= p; for (uint j = p; 1 < j; j--) { if (0 == a) break; a = pow(a, j) % p; } writeln(a); } } bool[] generateNotPrime(uint n) { bool[] notPrime = []; notPrime.length = n + 1; notPrime[1] = true; foreach (i; 2..(n+1)) { if (notPrime[i]) { continue; } for (uint j = i*2; j <= n; j += i) { notPrime[j] = true; } } return notPrime; }