intqpow(int a, int x, int mo) { int res = 1; while (x) { if (x & 1) res = 1ll * a * res % mo; a = 1ll * a * a % mo; x >>= 1; } return res; } structLucas { int fac[PP], inv[PP]; voidinit(int p) { fac[0] = 1; inv[0] = 0; for (int i = 1; i <= p; i++) fac[i] = 1ll * fac[i - 1] * i % p; inv[1] = 1; for (int i = 2; i <= p; ++i) inv[i] = 1ll * (p - p / i) * inv[p % i] % p; } intC(int n, int m, int p) { if (m > n) return0; return1ll * fac[n] * inv[fac[n - m]] % p * inv[fac[m]] % p; } intcalc(int n, int m, int p) { if (!m) return1; return1ll * C(n % p, m % p, p) * calc(n / p, m / p, p) % p; } } t; intmain() { int T; scanf("%d", &T); while (T--) { int n, m, p; scanf("%d%d%d", &n, &m, &p); t.init(p); cout << t.calc(n + m, n, p) << endl; } }