#include <bits/stdc++.h>
using namespace std;
int arr[(int)1e6 + 5] = {};
int ans[(int)1e6 + 5][10];
int solve(int x) {
  if (x <= 9) {
    return x;
  }
  if (arr[x] != -1) {
    return arr[x];
  }
  int res = 1;
  while (x > 0) {
    int c = x % 10;
    if (c) res *= (c);
    x /= 10;
  }
  return arr[x] = solve(res);
}
int main() {
  int q;
  cin >> q;
  memset(arr, -1, sizeof arr);
  memset(ans, 0, sizeof ans);
  for (int i = 1; i <= 1e6; i++) {
    int x = solve(i);
    ans[i][x] = 1;
  }
  for (int i = 1; i <= 1e6; i++) {
    for (int j = 1; j < 10; j++) {
      ans[i][j] += ans[i - 1][j];
    }
  }
  while (q--) {
    int l, r, k, c = 0;
    cin >> l >> r >> k;
    cout << ans[r][k] - ans[l - 1][k] << endl;
  }
  return 0;
}
