#include <iostream>
#include <cstring>
using namespace std;
int t, a, b, k;
string nString;
int memo[10][2][90][90];
int dp(int index, bool tight, int sumMod, int sumDigitMod) {
if (index == nString.size()) {
if (sumMod == 0 && sumDigitMod == 0) {
return 1;
}
return 0;
}
if (memo[index][tight][sumMod][sumDigitMod] != -1) {
return memo[index][tight][sumMod][sumDigitMod];
}
int limit = (tight ? nString[index] - '0' : 9);
int result = 0;
for (int digit = 0; digit <= limit; ++digit) {
bool newTight = tight && (digit == limit);
int newSumMod = (sumMod * 10 + digit) % k;
int newSumDigitMod = (sumDigitMod + digit) % k;
result += dp(index + 1, newTight, newSumMod, newSumDigitMod);
}
memo[index][tight][sumMod][sumDigitMod] = result;
return result;
}
int f(int n) {
if (k > 90) {
return 0;
}
nString = to_string(n);
memset(memo, -1, sizeof(memo));
return dp(0, true, 0, 0);
}
int main() {
cin >> t;
for (int i = 1; i <= t; ++i) {
cin >> a >> b >> k;
cout << "Case " << i << ": " << f(b) - f(a - 1) << '\n';
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCB0LCBhLCBiLCBrOwpzdHJpbmcgblN0cmluZzsKaW50IG1lbW9bMTBdWzJdWzkwXVs5MF07CgppbnQgZHAoaW50IGluZGV4LCBib29sIHRpZ2h0LCBpbnQgc3VtTW9kLCBpbnQgc3VtRGlnaXRNb2QpIHsKCWlmIChpbmRleCA9PSBuU3RyaW5nLnNpemUoKSkgewoJCWlmIChzdW1Nb2QgPT0gMCAmJiBzdW1EaWdpdE1vZCA9PSAwKSB7CgkJCXJldHVybiAxOwoJCX0KCQkKCQlyZXR1cm4gMDsKCX0KCQoJaWYgKG1lbW9baW5kZXhdW3RpZ2h0XVtzdW1Nb2RdW3N1bURpZ2l0TW9kXSAhPSAtMSkgewoJCXJldHVybiBtZW1vW2luZGV4XVt0aWdodF1bc3VtTW9kXVtzdW1EaWdpdE1vZF07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