#include<iostream> #include<cstdio> #include<cstring> #define maxn 20 #define maxm 11 #define ll long long usingnamespacestd;
int n; ll a, b;
int L[1 << maxm]; voidinit(){ int M = (1 << maxm) - 1, m = 10; for (int S = 1; S <= M; ++S) for (int i = 1; i <= m; ++i) L[S] += S >> i - 1 & 1; } intcalc(int S, int k){ for (int i = k; i < 10; ++i) if (S >> i & 1) { S ^= 1 << i; break; } S |= 1 << k; return S; }
ll f[maxn][1 << maxm][maxm]; int K, d[maxn], len; ll dfs(int pos, int lead, int limit, int S, int k){ if (!pos) return L[S] == k; if (!lead && !limit && ~f[pos][S][k]) return f[pos][S][k]; int up = limit ? d[pos] : 9; ll ans = 0; for (int i = 0; i <= up; ++i) { if (lead && !i) { ans += dfs(pos - 1, 1, 0, 0, k); continue; } ans += dfs(pos - 1, 0, limit && i == d[pos], calc(S, i), k); } if (!lead && !limit) f[pos][S][k] = ans; return ans; }
ll solve(ll x){ len = 0; while (x) { d[++len] = x % 10; x /= 10; } return dfs(len, 1, 1, 0, n); }
int icase; voidwork(){ cin >> a >> b >> n; printf("Case #%d: %lld\n", ++icase, solve(b) - solve(a - 1)); }
intmain(){ int T; cin >> T; init(); memset(f, -1, sizeof f); while (T--) work(); return0; }
voidmem(){ for (int i = 0; i < maxn; ++i) for (int j = 0; j < 2; ++j) for (int k = 0; k < 2; ++k) for (int l = 0; l < 2; ++l) f[i][j][k][l] = node(-1, -1); }
voidwork(){ cin >> n >> m >> k >> p; --n; --m; mem(); node t = solve(n, m, k); cout << (t.v - t.s * (k % p) % p + p) % p << endl; }
intmain(){ int T; cin >> T; while (T--) work(); return0; }