#include <bits/stdc++.h>
using namespace std;
vector<int> arr(1000005, 0);
int brr[1000005][10];
int main() {
  int i, j, k, l, m, n, o, p;
  for (i = 1; i < 1000005; i++) {
    if (i < 10)
      arr[i] = i;
    else {
      l = 1;
      n = i;
      while (n != 0) {
        j = n % 10;
        n = n / 10;
        if (j != 0) l = l * j;
      }
      arr[i] = arr[l];
    }
  }
  for (i = 0; i < 1000005; i++) {
    for (j = 0; j < 10; j++) brr[i][j] = 0;
  }
  for (i = 1; i < 1000005; i++) {
    for (j = 0; j < 10; j++) brr[i][j] = brr[i - 1][j];
    j = arr[i];
    brr[i][j] = brr[i - 1][j] + 1;
  }
  cin >> o;
  while (o--) {
    cin >> l >> m >> p;
    j = brr[m][p] - brr[l][p];
    if (arr[l] == p) j++;
    cout << j << endl;
  }
}
