#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
// ax + my =b
//只要满足 b是d的倍数就行.
int exgcd(int a, int b, int& x, int& y)
{
if(!b)
{
x=1,y=0;
return a;
}
int d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b,m;
scanf("%d%d%d",&a,&b,&m);
int x,y;
int d=exgcd(a,m,x,y);
if(b % d) cout<<"impossible"<<endl;
else printf("%d\n",(LL)b / d * x % m);
}
return 0;
}