#include <bits/stdc++.h>
using namespace std;
int dp[1000006];
int co[1000001][10];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (int i = 1; i <= 9; i++) dp[i] = i;
  for (int i = 10; i <= 1000000; i++) {
    int pr = 1;
    int x = i;
    while (x != 0) {
      int y = (x % 10);
      x = x / 10;
      if (y != 0) pr = pr * y;
    }
    dp[i] = dp[pr];
  }
  for (int i = 1; i < 10; i++) {
    co[0][i] = 0;
  }
  for (int i = 1; i <= 1000000; i++) {
    for (int j = 1; j < 10; j++) co[i][j] = co[i - 1][j];
    co[i][dp[i]]++;
  }
  int n;
  cin >> n;
  while (n--) {
    int l, r, k;
    cin >> l >> r >> k;
    cout << co[r][k] - co[l - 1][k] << "\n";
  }
  return 0;
}
