#include<cstdio>
typedef long long ll;
int qmi(int b, int k, int p){
int res = 1;
while(k){
if(k & 1) res = (ll)res * b % p;
k >>= 1;
b = (ll)b * b % p;
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while(n -- ){
int b, p;
scanf("%d%d", &b, &p);
if(b % p) printf("%d\n", qmi(b, p - 2, p));//当b和p不是互为质数是,不可能同mod一个数余1, 就返回impossible
else puts("impossible");
}
return 0;
}