#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
int pre[1000010];
int GetN(int n) {
  if (pre[n] > 0) return pre[n];
  if (n < 10) {
    pre[n] = n;
    return n;
  }
  int temp = n;
  int product = 1;
  while (n > 0) {
    int rem = n % 10;
    product *= rem == 0 ? 1 : rem;
    n = n / 10;
  }
  return pre[temp] = GetN(product);
}
int dp[1000010][10];
void Precal() {
  for (int i = 1; i < 1000010; i++) {
    int res = GetN(i);
    dp[i][res] = 1;
    for (int k = 1; k <= 9; k++) {
      dp[i][k] = dp[i][k] + dp[i - 1][k];
      if (dp[i][0] == 1) {
        cout << i << " " << k;
        int v;
        cin >> v;
        exit(0);
      }
    }
  }
}
int main() {
  Precal();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << dp[r][k] - dp[l - 1][k] << endl;
    ;
  }
  int v;
  cin >> v;
  return 0;
}
