#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int to[N];
int dp[N][15];
int solve(int x) {
  if (x < 10) return x;
  int ret = 1;
  while (x) {
    int cur = x % 10;
    if (cur) ret *= cur;
    x /= 10;
  }
  return ret;
}
int main() {
  for (int i = int(1); i <= int(N - 1); i++) {
    if (i < 10) {
      to[i] = i;
    } else {
      to[i] = to[solve(i)];
    }
    dp[i][to[i]]++;
  }
  for (int i = int(1); i <= int(N - 1); i++) {
    for (int k = 0; k < int(10); k++) {
      dp[i][k] += dp[i - 1][k];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);
    printf("%d\n", dp[r][k] - dp[l - 1][k]);
  }
  return 0;
}
