#include<bits/stdc++.h> usingnamespace std; intqkm(int a,int b,int p) { int res =1; while(b) { if(b&1) res =(longlong)res*a%p; b>>=1; a=(longlong)a*a%p; } return res;
} intmain(void) { int n; scanf("%d",&n); while(n--) { int a,p; scanf("%d%d",&a,&p); int res = qkm(a,p-2,p); if(a%p!=0) printf("%d\n",res); elseprintf("impossible\n"); } }