// 重写power
#include <iostream>

using namespace std;

long long power(long long base, long long exp, long long mod) {
    long long res = 1 % mod;
    base %= mod;
    while (exp > 0) {
        if (exp % 2 == 1) res = (res * base) % mod;
        base = (base * base) % mod;
        exp /= 2;
    }
    return res;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        long long a, b, m;
        cin >> a >> b >> m;
        cout << power(a, b, m) << endl;
    }
    return 0;
}