#include<iostream>
using namespace std;
typedef long long LL;
int exgcd(int a, int b, int &x, int &y)
{
int d;
if(b == 0) { x = 1; return a; }
else
{
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 = 0, y = 0;
int d = exgcd(a, m, x, y);
if(b % d) printf("impossible\n");
else
{
LL res = (x * (LL)b / d) % m;//输出应该是一个int
printf("%lld\n", res);
}
}
}