#include <bits/stdc++.h>
using namespace std;
int f[1000001];
int g[1000001];
int spart[1000001][10];
int main() {
  for (int i = 1; i <= 1000000; i++) {
    int ci = i, p = 1;
    while (ci) {
      if (ci % 10 != 0) {
        p *= ci % 10;
      }
      ci /= 10;
    }
    f[i] = p;
  }
  for (int i = 1; i <= 1000000; i++) {
    if (i < 10)
      g[i] = i;
    else
      g[i] = g[f[i]];
    spart[i][g[i]] = spart[i - 1][g[i]] + 1;
    for (int j = 1; j <= 9; j++) {
      if (j != g[i]) spart[i][j] = spart[i - 1][j];
    }
  }
  int q;
  int l, r, cif;
  cin >> q;
  while (q--) {
    cin >> l >> r >> cif;
    cout << spart[r][cif] - spart[l - 1][cif] << endl;
  }
}
