#include<iostream>
using namespace std;
typedef long long LL;
LL qmi(int a,int b,int p)
{
LL res=1%p;
while(b)
{
if(b&1) res=res*a%p;
a=(LL)a*a%p;
b>>=1;
}
return res;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int a,p;
scanf("%d%d",&a,&p);
if(a%p==0) puts("impossible");//a和b要互质
else printf("%d\n",qmi(a,p-2,p));//p要为质数
}
return 0;
}