acwing:https://www.acwing.com/problem/content/889/
\(C_a^b \equiv C_{a \% p}^{b \% p}\) * $C_{\frac{a}{p}}^{\frac{b}{p}} (mod p) $
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL n, a, b, p;
LL qp(LL a, LL k, LL p){
LL ans = 1;
while (k){
if (k & 1) ans = ans * a % p;
k >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL a, LL b, LL p){
if (a < b) return 0;
LL x = 1, y = 1;
for (LL i = a, j = 1; j <= b; i --, j ++ ){
x = x * i % p;
y = y * j % p;
}
return x * qp(y, p - 2, p) % p;
}
LL lucas(LL a, LL b, LL p){
if (a < p && b < p) return C(a, b, p);
return C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
int main(){
cin >> n;
for (int i = 1; i <= n; i ++ ){
cin >> a >> b >> p;
cout << lucas(a, b, p) << "\n";
}
return 0;
}