#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
int jc[1000005];
int njc[1000005];
int p,y;
int ex_gcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1,y=0;
return a;
}
int k=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return k;
}
long long C(int n,int m)
{
return ( (((long long)jc[n]*njc[m])%p)*(long long)njc[n-m])%p;
}
long long lucas(int n,int m)
{
if(n<m) return 0;
if(n>=p || m>=p) return ((long long)lucas(n/p,m/p)*lucas(n%p,m%p))%p;
return C(n,m);
}
int main()
{
int t; scanf("%d",&t);
for(int q=1;q<=t;q++)
{
int n,m;
scanf("%d%d%d",&n,&m,&p);
jc[0]=1;//一定从0处理
for(int i=1;i<=p-1;i++) jc[i]=((long long)jc[i-1]*i)%p;
ex_gcd(jc[p-1],p,njc[p-1],y);
njc[p-1]=(njc[p-1]%p+p)%p;
for(int i=p-2;i>=0; i--) njc[i]=((long long)njc[i+1]*(i+1))%p;//逆元一定要求到0!!!
long long ans=lucas(m+n,m);
printf("%d\n",ans);
}
return 0;
}