#include<bits/stdc++.h> const int INF = 0x3f3f3f3f; const int Maxn = 100005; #define ll long long #define mem(x,y) memset(x,y,sizeof(x)) using namespace std; int x[Maxn], num; int deal(ll N) { // 对N分解质因数 int nm = 0; for (int i = 2; i * i <= N; i++) { if (N && N % i == 0) { x[++nm] = i; while (N && N % i == 0) N /= i; } } if (N > 1) x[++nm] = N; return nm; } ll func(ll n, int m) { // 计算1-n的数中 和N不互质的个数 ll ans = 0, flag; for (int i = 1; i < (1 << m); i++) { // 用二进制来1,0来表示第几个素因子是否被用 ll tmp = 1, flag = 0; for (int j = 1; j <= m; j++) // 判断第几个因子目前被用到 if (i & (1 << (j - 1))) ++flag, tmp *= x[j]; if (flag & 1) // 容斥原理,奇加偶减 ans += n / tmp; else ans -= n / tmp; } return ans; } int main() { int T, icase = 1; ll a, b, N; cin >> T; while (T--) { cin >> a >> b >> N; num = deal(N); ll ans = (b - a + 1) - (func(b, num) - func(a - 1, num)); printf("Case #%d: %lld\n", icase++, ans); } }