#include <bits/stdc++.h>
using namespace std;
int sum[1100000][10];
int f(int x) {
  if (x < 10) return x;
  int ans = 1;
  while (x != 0) {
    if (x % 10 != 0) ans *= (x % 10);
    x /= 10;
  }
  return f(ans);
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(false);
  int q;
  int l, r, k;
  for (int j = 1; j <= 9; j++) sum[0][j] = 0;
  for (int i = 1; i <= 1000000; i++) {
    for (int j = 1; j <= 9; j++) sum[i][j] = sum[i - 1][j];
    sum[i][f(i)]++;
  }
  cin >> q;
  for (int t = 0; t < q; t++) {
    cin >> l >> r >> k;
    cout << sum[r][k] - sum[l - 1][k] << "\n";
  }
  return 0;
}
