#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
vector<int> num;
int a, b, k;
int dp[12][90][90][2];
int go(int pos, int val, int sum, int f) {
if (pos == num.size()) {
if (sum == 0 and val == 0)
return 1;
return 0;
}
int &res = dp[pos][val][sum][f];
if (res != -1)
return res;
res = 0;
int LMT;
if (f == 0)
LMT = num[pos];
else
LMT = 9;
for (int dgt = 0; dgt <= LMT; dgt++) {
int nf = f;
if (f == 0 and dgt < LMT)nf = 1;
res += go(pos + 1, (val * 10 + dgt) % k, (sum + dgt) % k, nf);
}
return res;
}
int solve(int a) {
num.clear();
while (a > 0)
num.push_back(a % 10), a /= 10;
reverse(num.begin(), num.end());
memset(dp, -1, sizeof dp);
return go(0, 0, 0, 0);
}
int32_t main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> a >> b >> k;
int ans;
if (k > 83)
ans = 0;
else ans = solve(b) - solve(a - 1);
cout << "Case " << i << ": " << ans << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCiNkZWZpbmUgaW50IGxvbmcgbG9uZwp1c2luZyBuYW1lc3BhY2Ugc3RkOwp2ZWN0b3I8aW50PiBudW07CmludCBhLCBiLCBrOwppbnQgZHBbMTJdWzkwXVs5MF1bMl07CgppbnQgZ28oaW50IHBvcywgaW50IHZhbCwgaW50IHN1bSwgaW50IGYpIHsKICAgIGlmIChwb3MgPT0gbnVtLnNpemUoKSkgewogICAgICAgIGlmIChzdW0gPT0gMCBhbmQgdmFsID09IDApCiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIGludCAmcmVzID0gZHBbcG9zXVt2YWxdW3N1bV1bZl07CiAgICBpZiAocmVzICE9IC0xKQogICAgICAgIHJldHVybiByZXM7CiAgICByZXMgPSAwOwogICAgaW50IExNVDsKICAgIGlmIChmID09IDApCiAgICAgICAgTE1UID0gbnVtW3Bvc107CiAgICBlbHNlCiAgICAgICAgTE1UID0gOTsKCiAgICBmb3IgKGludCBkZ3QgPSAwOyBkZ3QgPD0gTE1UOyBkZ3QrKykgewogICAgICAgIGludCBuZiA9IGY7CiAgICAgICAgaWYgKGYgPT0gMCBhbmQgZGd0IDwgTE1UKW5mID0gMTsKICAgICAgICByZXMgKz0gZ28ocG9zICsgMSwgKHZhbCAqIDEwICsgZGd0KSAlIGssIChzdW0gKyBkZ3QpICUgaywgbmYpOwogICAgfQogICAgcmV0dXJuIHJlczsKfQoKCmludCBzb2x2ZShpbnQgYSkgewogICAgbnVtLmNsZWFyKCk7CiAgICB3aGlsZSAoYSA+IDApCiAgICAgICAgbnVtLnB1c2hfYmFjayhhICUgMTApLCBhIC89IDEwOwogICAgcmV2ZXJzZShudW0uYmVnaW4oKSwgbnVtLmVuZCgpKTsKCiAgICBtZW1zZXQoZHAsIC0xLCBzaXplb2YgZHApOwogICAgcmV0dXJuIGdvKDAsIDAsIDAsIDApOwp9CgppbnQzMl90IG1haW4oKSB7CgogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IHQ7IGkrKykgewogICAgICAgIGNpbiA+PiBhID4+IGIgPj4gazsKICAgICAgICBpbnQgYW5zOwogICAgICAgIGlmIChrID4gODMpCiAgICAgICAgICAgIGFucyA9IDA7CiAgICAgICAgZWxzZSBhbnMgPSBzb2x2ZShiKSAtIHNvbHZlKGEgLSAxKTsKICAgICAgICBjb3V0IDw8ICJDYXNlICIgPDwgaSA8PCAiOiAiIDw8IGFucyA8PCBlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0=