#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005000;
int A[maxn], sum[maxn][10];
int dfs(int u) {
  if (u < 10) return u;
  if (A[u]) return A[u];
  int now = 1, tmp = u;
  while (u) {
    if (u % 10 != 0) now *= u % 10;
    u /= 10;
  }
  return A[tmp] = dfs(now);
}
int main() {
  for (int i = 1; i <= 1000000; i++) A[i] = dfs(i);
  for (int i = 1; i <= 1000000; i++) {
    for (int j = 1; j <= 9; j++) {
      if (A[i] == j)
        sum[i][j] = sum[i - 1][j] + 1;
      else
        sum[i][j] = sum[i - 1][j];
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    cout << sum[r][k] - sum[l - 1][k] << endl;
  }
}
