#include <bits/stdc++.h>
using namespace std;
int a[1000001][10], f[10];
void fnc() {
  for (int i = 1; i < 1000002; i++) {
    int j = i;
    while (j > 9) {
      int l = j, k = 1;
      while (l) {
        if (l % 10) k = k * (l % 10);
        l = l / 10;
      }
      j = k;
    }
    for (int d = 1; d < 10; d++) {
      a[i][d] = f[d];
    }
    f[j]++;
  }
}
int main() {
  fnc();
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    if (a[r][k] != a[r + 1][k]) {
      printf("%d\n", a[r][k] - a[l][k] + 1);
    } else
      printf("%d\n", a[r][k] - a[l][k]);
  }
  return 0;
}
