#include<bits/stdc++.h>
using namespace std;
#define ll long long
unordered_map<ll, ll> mp;
ll bsgs(ll a, ll b, ll m){
mp.clear();
ll s = sqrt(m) + 1, g = 1, gg = 1;
for (int i = 1; i <= s; i++)
mp[b* (g = g * a % m) % m] = i;
for (int j = 1; j <= s; j++)
if (mp.find(gg = gg * g % m) != mp.end())
return j * s - mp[gg];
return -1;
}
ll y, z, p, t, k;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> t >> k;
while (t--){
cin >> y >> z >> p;
if (z >= p || y >= p){
cout << "Orz, I cannot find x!\n";
continue;
}
if (k == 1){
int ans = 1;
while (z){
if (z & 1)
ans = ans * y % p;
y = y * y % p;
z >>= 1;
}
cout << ans << "\n";
}else if (k == 2){
ll K = p - 2, ans = 1;
while (K){
if (K & 1)
ans = ans * y % p;
y = y * y % p;
K >>= 1;
}
cout << z * ans % p << "\n";
}else{
int x = bsgs(y, z, p);
if (x == -1)
cout << "Orz, I cannot find x!\n";
else
cout << x << "\n";
}
}
return 0;
}