#include <bits/stdc++.h>
using namespace std;
int f(int n) {
  if (n < 10) {
    return n;
  }
  int ans = 1;
  while (n) {
    if (n % 10 != 0) {
      ans *= n % 10;
    }
    n /= 10;
  }
  return ans;
}
int g[1000005];
int cnt[10][1000005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 1; i <= 1000000; i++) {
    if (i < 10) {
      g[i] = i;
    } else {
      g[i] = g[f(i)];
    }
    if (i <= 60) {
    }
    for (int j = 1; j <= 9; j++) {
      cnt[j][i] = cnt[j][i - 1];
    }
    cnt[g[i]][i]++;
  }
  int T;
  cin >> T;
  while (T--) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << cnt[k][r] - cnt[k][l - 1] << "\n";
  }
  return 0;
}
