#include <iostream>
using namespace std;

typedef long long LL;

LL qmi(LL a, LL b, LL p) {
    LL res = 1 % p;  
    LL x = a % p;
    while (b) {
        if (b & 1) { 
            res = res * x % p; 
        }
        x = x * x % p; 
        b >>= 1;       
    }
    return res;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        LL a, b, p;
        cin >> a >> b >> p;
        LL res = qmi(a, b, p);
        cout<<res<<endl;
    }
    return 0;
}