#include <bits/stdc++.h>
int g[1000000 + 1], c[9][1000000 + 1];
char o[2 * 1000000];
int getint(void) {
  int c, v = 0;
  while ((c = getchar()) >= '0') v = 10 * v + c - '0';
  return v;
}
int f(int n) {
  int v = 1;
  for (; n; n /= 10)
    if (n % 10) v *= n % 10;
  return v;
}
int main(void) {
  for (int i = 1; i < 10; i++) g[i] = i;
  for (int i = 10; i <= 1000000; i++) g[i] = g[f(i)];
  for (int k = 1; k <= 9; k++)
    for (int i = 1; i <= 1000000; i++)
      c[k - 1][i] = c[k - 1][i - 1] + (g[i] == k);
  int Q = getint();
  char *op = o;
  for (int q = 0; q < Q; q++) {
    int l = getint(), r = getint(), k = getint();
    op += sprintf(op, "%d\n", c[k - 1][r] - c[k - 1][l - 1]);
  }
  printf("%s", o);
}
