#include <iostream>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int T, n, m, p;
inline int fast(int x, int y) {
int res = 1;
while (y) {
if (y & 1) {
res *= x;
res %= p;
}
x = (x * x) % p;
y >>= 1;
}
return res;
}
inline int get(int a, int b) {
b = min(b, a - b);
int res = 1;
for (int i = a - b + 1; i <= a; ++i) {
res *= i;
res %= p;
}
for (int i = 1; i <= b; ++i) {
res *= fast(i, p - 2);
res %= p;
}
return res;
}
int Lucas(int a, int b) {
if (b == 0) return 1;
return (Lucas(a / p, b / p) + get(a % p, b % p)) % p;
}
signed main() {
cin >> T;
while (T--) {
cin >> n >> m >> p;
cout << Lucas(n + m, n) << '\n';
}
return 0;
}