#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
int pre[1000007], ar[15][1000007];
int pro(int num) {
  if (pre[num]) return pre[num];
  int r, p = 1, tmp = num;
  while (tmp > 0) {
    r = tmp % 10;
    tmp /= 10;
    if (r) p *= r;
  }
  return p;
}
void clc() {
  for (int i = 1; i <= 1e6; i++) {
    int tmp = i;
    while (tmp > 9) {
      tmp = pro(tmp);
    }
    pre[i] = tmp;
  }
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 1e6; j++) {
      int ck = pro(j);
      if (pre[ck] == i) ar[i][j]++;
      ar[i][j] += ar[i][j - 1];
    }
  }
}
int main() {
  clc();
  int l, r, q, val;
  cin >> q;
  while (q--) {
    cin >> l >> r >> val;
    int ans = ar[val][r] - ar[val][l - 1];
    cout << ans << endl;
  }
  return 0;
}
