#include <bits/stdc++.h>
using namespace std;
int dp_g[1000005];
int f(int n) {
  int total = 1;
  while (n > 0) {
    int tp = n % 10;
    if (tp > 0) total = total * tp;
    n = n / 10;
  }
  return total;
}
int g(int n) {
  if (dp_g[n] > 0) return dp_g[n];
  if (n < 10) {
    dp_g[n] = n;
    return dp_g[n];
  } else {
    dp_g[n] = g(f(n));
    return dp_g[n];
  }
}
int prefixsum[1000005][10];
int main() {
  for (int i = 1; i < 1000005; i++) g(i);
  for (int i = 1; i < 1000005; i++) {
    for (int j = 0; j < 10; j++) prefixsum[i][j] = prefixsum[i - 1][j];
    prefixsum[i][dp_g[i]] += 1;
  }
  int t;
  cin >> t;
  for (int tt = 0; tt < t; tt++) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << prefixsum[r][k] - prefixsum[l - 1][k] << endl;
  }
}
