#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> dp(10, vector<long long>(1000005, 0));
long long g(long long n) {
  if (n < 10) return n;
  long long ans = 1;
  while (n) {
    if (n % 10) ans *= (n % 10);
    n /= 10;
  }
  return g(ans);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (long long i = 1; i <= 1000000; i++) dp[g(i)][i]++;
  for (long long i = 0; i < 10; i++) {
    for (long long j = 2; j <= 1000000; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }
  long long t;
  cin >> t;
  while (t--) {
    long long l, r, k;
    cin >> l >> r >> k;
    cout << dp[k][r] - dp[k][l - 1] << "\n";
  }
}
