#include <bits/stdc++.h>
using namespace std;
int calc_g(int n, vector<int>& g) {
  if (g[n]) {
  } else if (n < 10) {
    g[n] = n;
  } else {
    int f_n = 1;
    while (n) {
      if (n % 10) {
        f_n *= n % 10;
      }
      n /= 10;
    }
    g[n] = calc_g(f_n, g);
  }
  return g[n];
}
int main() {
  int q;
  cin >> q;
  vector<int> g(1000001);
  vector<vector<int> > num_k(10, vector<int>(1000001));
  for (int i = 1; i <= 1000000; i++) {
    g[i] = calc_g(i, g);
    if (i) {
      for (int j = 0; j < 10; j++) {
        num_k[j][i] = num_k[j][i - 1];
      }
    }
    num_k[g[i]][i]++;
  }
  for (int i = 0; i < q; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    int ans = num_k[k][r];
    if (l) {
      ans -= num_k[k][l - 1];
    }
    cout << ans << '\n';
  }
}
