#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int q, l, r, k, dp[11][N + 5];
inline int f(int n) {
  int cur = 1;
  while (n) {
    int y = n % 10;
    if (y) {
      cur *= y;
    }
    n /= 10;
  }
  return cur;
}
int rec(int x) {
  if (x < 10) {
    return x;
  } else {
    return rec(f(x));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= 9; j++) {
      dp[j][i] = dp[j][i - 1];
    }
    dp[rec(i)][i]++;
  }
  cin >> q;
  while (q--) {
    cin >> l >> r >> k;
    cout << dp[k][r] - dp[k][l - 1] << "\n";
  }
}
