#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
const ll mod = 1e9 + 7;
ll k, dp[101][2][2][9 * 105];
ll solve(int pos, int top, int first, int prod, char str[], int sz){
if(pos == sz) return (prod == k);
if(dp[pos][top][first][prod] != -1) return dp[pos][top][first][prod];
ll Ans = 0;
int lim = top ? (str[pos] - '0') : 9;
for(int i = 0; i<=lim; i++){
if(!first && i == 0){
Ans = (Ans + solve(pos + 1, top && (i == lim), first || (i != 0), prod, str, sz) % mod) % mod;
}else{
Ans = (Ans + solve(pos + 1, top && (i == lim), first || (i != 0), prod * i, str, sz) % mod) % mod;
}
}
return dp[pos][top][first][prod] = Ans % mod;
}
int main() {
int t;
char A[105], B[105];
scanf("%d", &t);
for(int i = 1; i<=t; i++){
scanf("%s %s %lld", A, B, &k);
ll tmp = k;
while(tmp % 2 == 0) tmp/=2;
while(tmp % 3 == 0) tmp/=3;
while(tmp % 5 == 0) tmp/=5;
while(tmp % 7 == 0) tmp/=7;
if(k > 900 || tmp > 1){
printf("Case %d: 0\n", i);
continue;
}
ll Ans = 0;
memset(dp, -1, sizeof(dp));
Ans = solve(0, 1, 0, 1, B, strlen(B)) % mod;
memset(dp, -1, sizeof(dp));
Ans = (Ans - solve(0, 1, 0, 1, A, strlen(A)) % mod) % mod;
int val = 1;
for(int j = 0; j<strlen(A); j++){
val *= (A[j] - '0');
}
Ans += ((ll)val == k);
if(Ans < 0) Ans += mod;
printf("Case %d: %lld\n", i, Ans % mod);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHNpZ25lZCBsb25nIGxvbmcgbGw7CmNvbnN0IGxsIG1vZCA9IDFlOSArIDc7CmxsIGssIGRwWzEwMV1bMl1bMl1bOSAqIDEwNV07CgpsbCBzb2x2ZShpbnQgcG9zLCBpbnQgdG9wLCBpbnQgZmlyc3QsIGludCBwcm9kLCBjaGFyIHN0cltdLCBpbnQgc3op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