#include <bits/stdc++.h>
using namespace std;
int recur(int n) {
  if (n <= 9)
    return n;
  else {
    int m = 1;
    while (n > 0) {
      if (n % 10 != 0) m = m * (n % 10);
      n /= 10;
    }
    n = recur(m);
  }
  return n;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  vector<vector<int> > mapp(10, vector<int>(1000010));
  for (int i = 1; i <= 9; i++) {
    int total = 0;
    for (auto j = 0; j < 1000001; j++) {
      if (recur(j) == i) {
        mapp[i][j] = 1 + mapp[i][j - 1];
      } else
        mapp[i][j] = mapp[i][j - 1];
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << (mapp[k][r] - mapp[k][l - 1]) << "\n";
  }
}
