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