#include using namespace std; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int main() { int n; cin >> n; while (n--) { long long a, b, p; cin >> a >> b >> p; cout << binpow(a, b, p) << endl; } return 0; }