输入样例:
3
4 3
8 5
6 3
输出样例:
1
2
impossible
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
LL qmi(int a, int b, int p){
LL res = 1 % p;
while(b){
if(b & 1)
res = res * a % p;
b >>= 1;
a = (LL) a * a % p;
}
return res;
}
int main(){
cin >> n;
while(n -- ){
int a, p;
cin >> a >> p;
if(a % p == 0) puts("impossible");
else
cout << qmi(a, p - 2, p) << endl;
}
return 0;
}