#include <bits/stdc++.h>
using namespace std;
vector<int> ar;
int f(int temp) {
  if (temp <= 9 && temp >= 1)
    return temp;
  else {
    if (ar[temp] != -1)
      return ar[temp];
    else {
      int k = 1;
      int temp_temp = temp;
      while (temp_temp != 0) {
        int h = temp_temp % 10;
        if (h != 0) k *= h;
        temp_temp /= 10;
      }
      ar[temp] = f(k);
      return ar[temp];
    }
  }
}
int main(void) {
  std::ios::sync_with_stdio(false);
  ar.resize(1000001);
  for (int i = 0; i < 1000001; i++) ar[i] = -1;
  int n;
  cin >> n;
  vector<vector<int> > ar1;
  ar1.resize(9);
  for (int i = 0; i < 9; i++) {
    ar1[i].resize(1000001);
    ar1[i][0] = 0;
  }
  for (int j = 0; j < 9; j++)
    for (int i = 1; i < 1000001; i++) {
      if (f(i) == j + 1) ar1[j][0]++;
      ar1[j][i] = ar1[j][0];
    }
  for (int i = 0; i < 9; i++) ar1[i][0] = 0;
  for (int i = 0; i < n; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << ar1[k - 1][r] - ar1[k - 1][l - 1] << endl;
  }
  return 0;
}
