#include <bits/stdc++.h>
using namespace std;
int dp[1000001][10];
int ans[1000001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  for (int i = 0; i <= 1000000; i++) {
    for (int j = 1; j <= 9; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      dp[i][j] = dp[i - 1][j];
    }
    dp[i][i] = dp[i - 1][i] + 1;
    ans[i] = i;
  }
  for (long long int i = 10; i <= 1000000; i++) {
    long long int temp = i;
    long long int p = 1;
    while (temp > 0) {
      int d = temp % 10;
      if (d != 0) p *= d;
      temp = temp / 10;
    }
    ans[i] = ans[p];
    for (int j = 1; j < 10; j++) {
      dp[i][j] = dp[i - 1][j];
    }
    dp[i][ans[i]] = dp[i - 1][ans[i]] + 1;
  }
  long long int l, r, t;
  for (int i = 0; i < q; i++) {
    cin >> l >> r >> t;
    printf("%d\n", dp[r][t] - dp[l - 1][t]);
  }
}
