#include <bits/stdc++.h>
using namespace std;
int sum[10][1000001];
int l[200001], r[200001], k[200001];
long f(int n) {
  long ans = 1;
  while (n) {
    int digit = n % 10;
    n = n / 10;
    if (digit) {
      ans *= digit;
    }
  }
  return ans;
}
int g(long n) {
  if (n < 10)
    return n;
  else
    return g(f(n));
}
int main() {
  long t;
  int minl, maxr;
  minl = 0x7fffffff;
  maxr = 0;
  scanf("%ld", &t);
  for (long i = 0; i < t; i++) {
    scanf("%d%d%d", &l[i], &r[i], &k[i]);
    minl = min(minl, l[i]);
    maxr = max(maxr, r[i]);
  }
  memset(sum, 0, sizeof(sum));
  for (long i = minl; i <= maxr; i++) {
    int num = g(i);
    for (int j = 1; j <= 9; j++) {
      sum[j][i] = sum[j][i - 1];
    }
    sum[num][i]++;
  }
  for (long i = 0; i < t; i++) {
    printf("%d\n", sum[k[i]][r[i]] - sum[k[i]][l[i] - 1]);
  }
  return 0;
}
