#include <bits/stdc++.h>
using namespace std;
int ans[1000005];
int presum[1000005][10];
void preprocess() {
  for (int j = 0; j < 1000005; j++)
    for (int i = 0; i < 10; i++) presum[j][i] = 0;
  ans[0] = 0;
  for (int i = 1; i < 10; i++) ans[i] = i;
  for (int i = 10; i < 1000005; i++) {
    long long temp = 1;
    long long temp2 = i;
    while (temp2) {
      if (temp2 % 10) temp *= (temp2 % 10);
      temp2 /= 10;
    }
    ans[i] = ans[temp];
  }
  for (int i = 1; i < 1000005; i++) {
    for (int j = 1; j < 10; j++) presum[i][j] = presum[i - 1][j];
    presum[i][ans[i]] += 1;
  }
}
int main() {
  preprocess();
  int q, l, r, k;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &l);
    scanf("%d", &r);
    scanf("%d", &k);
    printf("%d\n", presum[r][k] - presum[l - 1][k]);
  }
}
