#include <bits/stdc++.h>
using namespace std;
int arr[1000000 + 7];
int q_sum[10][1000000 + 7];
int digit(int x) {
  int s = 1, a;
  while (x) {
    a = x % 10;
    if (a == 0) a = 1;
    s *= a;
    x = x / 10;
  }
  if (s >= 10)
    return digit(s);
  else
    return s;
}
void pre() {
  int i, j;
  for (i = 1; i <= 1000000; i++) arr[i] = digit(i);
  for (i = 1; i < 10; i++)
    for (j = 1; j <= 1000000; j++) {
      if (arr[j] == i) q_sum[i][j] = 1;
      q_sum[i][j] = q_sum[i][j - 1] + q_sum[i][j];
    }
}
int main() {
  pre();
  int q, l, r, k;
  cin >> q;
  while (q--) {
    cin >> l >> r >> k;
    printf("%d\n", q_sum[k][r] - q_sum[k][l - 1]);
  }
  return 0;
}
