#include <bits/stdc++.h>
using namespace std;
int arr[10][1000001];
int main() {
  int tc;
  cin >> tc;
  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      arr[i][j] = 0;
      if (i <= j) arr[i][j] = 1;
    }
  }
  for (int i = 10; i <= 1000000; i++) {
    int s = i;
    while (1) {
      if (s > 9) {
        int r = 1;
        while (s) {
          if (s % 10 == 0) {
          } else
            r *= s % 10;
          s /= 10;
        }
        s = r;
      } else {
        for (int j = 1; j < 10; j++) {
          if (j == s)
            arr[j][i] = arr[j][i - 1] + 1;
          else
            arr[j][i] = arr[j][i - 1];
        }
        break;
      }
    }
  }
  while (tc--) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << arr[c][b] - arr[c][a - 1] << endl;
  }
}
