#include <bits/stdc++.h>
using namespace std;
long long dp[10][1000006];
long long chk[1000006];
void precal() {
  for (long long i = 1; i <= 9; i++) {
    chk[i] = i;
    dp[i][i] = 1;
  }
  for (long long i = 10; i <= 1000001; i++) {
    long long tmp = i;
    long long mul = 1;
    while (tmp) {
      long long rem = tmp % 10;
      if (rem) mul *= rem;
      tmp /= 10;
    }
    chk[i] = chk[mul];
    dp[chk[i]][i] = 1;
  }
  for (long long i = 1; i <= 9; i++) {
    for (long long j = 1; j <= 1000000; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }
}
int main() {
  precal();
  long long test;
  scanf("%lld", &test);
  while (test--) {
    long long l, r, k;
    scanf("%lld", &l);
    scanf("%lld", &r);
    scanf("%lld", &k);
    printf("%lld\n", dp[k][r] - dp[k][l - 1]);
  }
}
