#include <bits/stdc++.h>
using namespace std;
int s[1000005] = {0};
int Hash[10][1000005] = {0};
int g(int x) {
  if (x < 10) return x;
  int temp = 1;
  while (x) {
    if (x % 10 != 0) temp = temp * (x % 10);
    x /= 10;
  }
  return g(temp);
}
int main() {
  for (int i = 1; i <= 1e6; i++) {
    s[i] = g(i);
  }
  for (int i = 1; i <= 1e6; i++) {
    for (int j = 1; j <= 9; j++) {
      Hash[j][i] = Hash[j][i - 1];
    }
    Hash[s[i]][i]++;
  }
  int q = 0;
  cin >> q;
  while (q--) {
    int l = 0, r = 0, k = 0;
    scanf("%d%d%d", &l, &r, &k);
    printf("%d\n", Hash[k][r] - Hash[k][l - 1]);
  }
  return 0;
}
