#include <bits/stdc++.h>
using namespace std;
inline void init_code() {}
const long long int N = 1e6 + 3;
long long int dp[N][10];
long long int check(long long int n) {
  long long int ans = 1;
  while (n > 0) {
    long long int a = n % 10;
    n /= 10;
    if (a != 0) {
      ans *= a;
    }
  }
  return ans;
}
void maker() {
  memset(dp, 0, sizeof(dp));
  for (long long int i = 0; i < 10; i++) {
    dp[i][i] = 1;
  }
  for (long long int i = 1; i <= 1000000; i++) {
    long long int val = check(i);
    for (long long int j = 0; j < 10; j++) {
      if (dp[val][j] > dp[val - 1][j]) {
        dp[i][j] = dp[i - 1][j] + 1;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
}
void solve() {
  long long int a, b, c;
  cin >> a >> b >> c;
  cout << dp[b][c] - dp[a - 1][c] << '\n';
}
int32_t main() {
  init_code();
  maker();
  long long int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
