#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  cin >> TESTS;
  vector<vector<int>> a(9, vector<int>(1000000 + 1, 0));
  for (long long int i = 1; i < 1000000 + 1; i++) {
    long long temp = i, prod = 1, d;
    while (temp > 9) {
      prod = 1;
      while (temp > 0) {
        d = temp % 10;
        if (d != 0) prod *= d;
        temp /= 10;
      }
      temp = prod;
    }
    for (long long int j = 0; j < 9; j++) a[j][i] = a[j][i - 1];
    a[temp - 1][i]++;
  }
  while (TESTS--) {
    long long x, y, n;
    cin >> x >> y >> n;
    cout << a[n - 1][y] - a[n - 1][x - 1] << '\n';
  }
  return 0;
}
