#include <bits/stdc++.h>
const int N = 1e6 + 7;
using namespace std;
int dp[N][10];
template <typename T>
void read(T &x) {
  T f = 1;
  x = 0;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  x *= f;
}
int F(int x) {
  if (x < 10) return x;
  int y = 1;
  while (x) {
    int tp = x % 10;
    if (tp) y *= tp;
    x /= 10;
  }
  return F(y);
}
int main() {
  int up = 1e6;
  for (int i = 1; i <= up; i++) {
    int tp = F(i);
    if (tp) dp[i][tp]++;
  }
  for (int i = 2; i <= up; i++) {
    for (int j = 1; j < 10; j++) dp[i][j] += dp[i - 1][j];
  }
  int Q, l, r, k;
  read(Q);
  while (Q--) {
    read(l);
    read(r);
    read(k);
    printf("%d\n", dp[r][k] - dp[l - 1][k]);
  }
  return 0;
}
