https://oi-wiki.org/math/lucas/
# include <iostream>
# include <cstdio>
# define LL long long
# define MAXN 100005
using namespace std;
template<typename T>
T rd(){
T fl = 1, x = 0;
int ch = getchar();
for( ;!isdigit(ch); ch = getchar()){
if(ch == '-'){
fl = -1;
}
}
for( ; isdigit(ch); ch = getchar()){
x = x * 10 + ch - '0';
}
return fl * x;
}
//
LL fac[MAXN];
//
LL QPow(LL x, LL y, LL p){
LL base = x % p, ans = 1;
for( ; y; y >>= 1){
if(y & 1){
(ans *= base) %= p;
}
(base *= base) %= p;
}
return ans;
}
LL Inv(LL x, LL p){
return QPow(x, p-2, p);
}
LL Combine(LL m, LL n, LL p){
return m > n ? 0 : fac[n]*Inv(fac[m], p)*Inv(fac[n-m], p)%p;
}
LL Lucas(LL m, LL n, LL p){
if(!m){
return 1;
}
return m > n ? 1 : Combine(m%p, n%p, p) * Lucas(m/p, n/p, p) % p;
}
//
int main(){
fac[0] = 1;
int T;
T = rd<int>();
for( ; T; T--){
LL n, m, p;
n = rd<LL>(), m = rd<LL>(), p = rd<LL>();
for(int i = 1; i <= p; i++){
fac[i] = fac[i-1]*i%p;
}
printf("%lld\n", Lucas(m, n+m, p));
}
return 0;
}