#include <bits/stdc++.h>
using namespace std;
vector<vector<long long> > solved(10);
void solve2() {
  for (long long i = 1; i < 1000001; i++) {
    long long x = i;
    long long res = 1;
    while (x) {
      if (x / 10 == 0) {
        break;
      }
      long long temp = 1;
      long long y = x;
      while (y) {
        if (y % 10 != 0) {
          temp *= (y % 10);
        }
        y /= 10;
      }
      x = temp;
    }
    solved[x].push_back(i);
  }
}
void solve() {
  long long x, y, k;
  cin >> x >> y >> k;
  x = upper_bound(solved[k].begin(), solved[k].end(), y) -
      lower_bound(solved[k].begin(), solved[k].end(), x);
  cout << x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve2();
  long long test;
  test = 1;
  cin >> test;
  while (test--) {
    solve();
    cout << "\n";
  }
}
