FZU2020
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
ll n,m,p;
ll Qpow(ll a,ll b)
{
ll ans=1;
a%=p;
while(b)
{
if(b&1)
ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
}
ll C(ll a,ll b)
{
if(a<b)
return 0;
ll ans=1;
for(int i=1;i<=b;i++)
{
ll x=(a-b+i)%p;
ll y=i%p;
ans=ans*(x*Qpow(y,p-2)%p)%p;
}
return ans;
}
ll Lucas(ll a,ll b)
{
if(b==0)
return 1;
return C(a%p,b%p)*Lucas(a/p,b/p)%p;
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n >> m >> p;
cout << Lucas(n,m)<< endl;
}
return 0;
}