#include <bits/stdc++.h>
using namespace std;
int n, k, a, b, i, j, m[100005], d[1000005], f[10][1000005];
int rec(int x) {
  if (x < 10)
    return x;
  else {
    int y = 1;
    while (x > 0) {
      if (x % 10 != 0) y *= x % 10;
      x /= 10;
    }
    return rec(y);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  while (i <= 1000003) {
    d[i] = rec(i);
    i++;
  }
  i = 0;
  while (i < 10) {
    while (j <= 1000003) {
      if (j > 0) f[i][j] = f[i][j - 1];
      if (d[j] == i) f[i][j]++;
      j++;
    }
    j = 0;
    i++;
  }
  i = 0;
  while (i < n) {
    cin >> a >> b >> k;
    a--;
    b--;
    cout << f[k][b + 1] - f[k][a] << endl;
    i++;
  }
}
