#include <bits/stdc++.h>
using namespace std;
int first(int a) {
  int x = 1;
  while (a) {
    if (a % 10) x *= a % 10;
    a /= 10;
  }
  return x;
}
int main() {
  int q;
  cin >> q;
  vector<vector<int> > m(int(1e6 + 1));
  for (int i = 0; i < int(1e6 + 1); ++i) m[i].resize(10);
  vector<int> par(int(1e6 + 1));
  for (int i = 1; i < int(1e6 + 1); ++i) {
    int z = first(i);
    while (z >= 10) {
      z = par[z];
    }
    par[i] = z;
    m[i][z]++;
    for (int j = 0; j < 10; ++j) {
      m[i][j] += m[i - 1][j];
    }
  }
  while (q--) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << m[r][k] - m[l - 1][k] << endl;
  }
}
