```cpp
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int exgcd(int a, int b, int& x, int& y){
if(!b){
x = 1, y = 0;
return a;
}
int d = exgcd(b,a % b,x,y);
int tx = x,ty = y; //存一下当前层的数据,防止前面计算影响
x = ty;
y = tx - (a / b) * ty;
return d;
}
int main(){
int n;
cin >> n;
while(n --){
int a, b, m;
cin >> a >> b >> m;
int x, y;
int t = exgcd(a,m,x,y);
if(b % t){
puts("impossible");
}else{
cout << (long long) x * (b / t) % m << endl;
}
}
return 0;
}
```