#include <bits/stdc++.h>
using namespace std;
int g[1100000];
int dp[1100000][11];
int f(int n) {
  int sum = 1;
  while (n != 0) {
    if (n % 10 != 0) sum *= n % 10;
    n /= 10;
  }
  return sum;
}
int main() {
  long long l, r, k, x, q;
  for (int i = 1; i < 10; i++) g[i] = i;
  for (int i = 10; i <= 1000000; i++) g[i] = g[f(i)];
  dp[1][1] = 1;
  for (k = 2; k < 10; k++) dp[1][k] = 0;
  for (int i = 2; i <= 1000000; i++) {
    for (k = 1; k < 10; k++) {
      if (g[i] == k)
        dp[i][k] = dp[i - 1][k] + 1;
      else if (g[i] != k)
        dp[i][k] = dp[i - 1][k];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l >> r >> k;
    cout << dp[r][k] - dp[l - 1][k] << endl;
  }
}
