#include <bits/stdc++.h>
long long prod_dig(long long n) {
  long long prod = 1;
  int a;
  while (n) {
    a = n % 10;
    if (a) {
      prod *= a;
    }
    n /= 10;
  }
  return prod;
}
int arr[10][1000000 + 1];
int main() {
  int t;
  for (int i = 1; i <= 1000000; i++) {
    t = i;
    while (t / 10) {
      t = prod_dig(t);
    }
    for (int j = 0; j < 10; j++) {
      arr[j][i] = arr[j][i - 1];
    }
    arr[t][i]++;
  }
  int Q, l, r, x, key;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d%d", &l, &r, &key);
    printf("%d\n", arr[key][r] - arr[key][l - 1]);
  }
}
