#include <bits/stdc++.h>
using namespace std;
long long int arr[1000006] = {0};
vector<long long int> vc;
void f() {
  for (long long int i = 1; i <= 1000000; i++) {
    long long int n = i;
    long long int sum = 1, res = 1;
    while (1) {
      sum = 1;
      while (n > 0) {
        res = n % 10;
        n /= 10;
        if (res == 0) continue;
        sum = sum * res;
      }
      n = sum;
      if (sum < 10) {
        break;
      }
    }
    arr[i] = sum;
    vc.push_back(sum);
  }
}
long long int r[1000006][10] = {0};
int main() {
  f();
  long long int n;
  for (int i = 1; i <= 1000001; i++) {
    r[i][arr[i]]++;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 1; j <= 1000001; j++) {
      r[j][i] += r[j - 1][i];
    }
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int l, rr, k;
    cin >> l >> rr >> k;
    cout << r[rr][k] - r[l - 1][k] << endl;
  }
}
