#include<cstdio>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1,y=0;
return a;
}
ll ret=exgcd(b,a%b,y,x);
y-=a/b*x;
return ret;
}
int main()
{
ll a,b,x,y,m;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%lld%lld%lld",&a,&b,&m);
ll d=exgcd(a,m,x,y);
int mod=m/d;
if(b%d)
puts("impossible");
else
printf("%lld\n",(x*b/d%mod));
}
return 0;
}