# include <bits/stdc++.h>
# define ll long long
using namespace std;
const int N = 2e5 + 5;
ll fac[N], ifac[N];
ll mod = 1e9 + 7;
ll qp(ll x, int y = mod - 2) {
ll res = 1;
for (; y; y >>= 1, x = x * x % mod) {
if (y & 1) {
res = res * x % mod;
}
}
return res;
}
int main() {
# ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
# endif
init();
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T; cin >> T;
for (int i = 1; i <= T; i++) {
int n, k;
cin >> n >> k >> mod;k = min(n, k);
ll ans = (k * (n - k) + (n - k) * (n - k - 1) + (n - k) - (n - 1 - k)) % mod;
for (int i = 1; i <= k; i++) {ans = ans * i % mod;}
cout << "Case #" << i << ": " << ans << '\n';
}
}