#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 11;
const long long inf = 1e9 + 7;
int Q;
int d[N + 10][10];
int g(int x) {
  int ans = 1;
  if (x < 10) {
    return x;
  }
  while (x > 0) {
    if ((x % 10) == 0) {
      x /= 10;
      continue;
    }
    ans *= (x % 10);
    x /= 10;
  }
  return g(ans);
}
int main() {
  cin >> Q;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= 9; ++j) {
      d[i][j] = d[i - 1][j];
    }
    ++d[i][g(i)];
  }
  for (int i = 1; i <= Q; ++i) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << d[r][k] - d[l - 1][k] << "\n";
  }
  return 0;
}
