#include <bits/stdc++.h>   using namespace std;   const double pi = 3.14159265359;   long long max(long long a, long long b) {     if (a >= b)       return a;     else       return b;   }   long long min(long long a, long long b) {     if (a <= b)       return a;     else       return b;   }   bool valid(int i, int j, long long n, long long m) {     if (i >= 0 && i < n && j >= 0 && j < m)       return 1;     else       return 0;   }   void yes() { cout <<  YES  <<   n ; }   void no() { cout <<  NO  <<   n ; }   vector<unsigned long long> divisor(10000007);   void printDivisors(long long n) {     for (int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         if (n / i == i) {           divisor[n] += i;         } else {           divisor[n] += i;           divisor[n] += n / i;         }       }     }   }   bool prime[1200000];   map<long long, long long> primes;   void SieveOfEratosthenes(long long n = 1200000) {     memset(prime, true, sizeof(prime));     prime[1] = false;     for (int p = 2; p * p < n; p++) {       if (prime[p] == true) {         for (int i = p * 2; i < n; i += p) prime[i] = false;       }     }     for (int i = 1; i < n + 1; i++)       if (prime[i]) {         primes[i]++;       }   }   int gcd(long long a, long long b) {     if (!b) return a;     return gcd(b, a % b);   }   string toBinary(long long n) {     string r;     while (n != 0) {       r = (n % 2 == 0 ?  0  :  1 ) + r;       n /= 2;     }     return r;   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   map<long long, long long> PF;   void primeFactors(long long n) {     while (n % 2 == 0) {       PF[2]++;       n = n / 2;     }     for (int i = 3; i <= sqrt(n); i = i + 2) {       while (n % i == 0) {         PF[i]++;         n = n / i;       }     }     if (n > 2) PF[n]++;   }   long double intlog(long long base, long long x) {     long double ans1 = log10(x), ans2 = log10(base);     return (long double)(ans1 / ans2);   }   bool IsPowerOfTwo(long long x) { return (x & (x - 1)) == 0; }   map<long long, long long> LN;   void GetLuckyNumbers(string s) {     if (s.size() > 10) return;     GetLuckyNumbers(s +  4 );     if (s !=   ) LN[stoll(s)]++;     GetLuckyNumbers(s +  7 );     if (s !=   ) LN[stoll(s)]++;   }   unsigned long long toDecimal(unsigned long long n) {     unsigned long long num = n;     unsigned long long dec_value = 0;     unsigned long long base = 1;     int temp = num;     while (temp) {       unsigned long long last_digit = temp % 10;       temp = temp / 10;       dec_value += last_digit * base;       base = base * 2;     }     return dec_value;   }   long double power(long double b, long long p) {     if (p == 0)       return 1;     else if (p == 1)       return b;     long double a = power(b, p / 2);     if (p % 2 == 0) return (a) * (a);     return (b) * (a) * (a);   }   long largest_power_of_two(long N) {     N = N | (N >> 1);     N = N | (N >> 2);     N = N | (N >> 4);     N = N | (N >> 8);     return (N + 1) >> 1;   }   long long cnt(string s) {     long long c = 0;     for (int i = 0; i < s.size(); i++) c += s[i] -  0 ;     return c;   }   bool ispal(string s) {     for (int i = 0; i < s.size() / 2; i++) {       if (s[i] != s[s.size() - i - 1]) return false;     }     return true;   }   map<string, long long> substrings;   void subString(string s, long long n) {     for (int i = 0; i < n; i++)       for (int len = 1; len <= n - i; len++) {         substrings[s.substr(i, len)]++;       }   }   bool check_divisabilty_prefix(string str, long long a) {     long long len = str.size();     vector<int> lr(len + 1, 0);     lr[0] = (str[0] -  0 ) % a;     for (int i = 1; i < len; i++)       lr[i] = ((lr[i - 1] * 10) % a + (str[i] -  0 )) % a;     if (lr[len - 1] == 0) return 1;     return 0;   }   bool check_divisabilty_suffix(string str, long long b) {     long long len = str.size();     vector<long long> rl(len + 1, 0);     rl[len - 1] = (str[len - 1] -  0 ) % b;     int power10 = 10;     for (int i = len - 2; i >= 0; i--) {       rl[i] = (rl[i + 1] + (str[i] -  0 ) * power10) % b;       power10 = (power10 * 10) % b;     }     if (rl[0] == 0) return 1;     return 0;   }   bool isSubSeq(int idx1, int idx2, string s1, string s2) {     if (idx2 == s2.size()) return true;     if (idx1 == s1.size()) return false;     if (s1[idx1] == s2[idx2])       return isSubSeq(idx1 + 1, idx2 + 1, s1, s2);     else       return isSubSeq(idx1 + 1, idx2, s1, s2);   }   vector<long long> v;   long long bs(long long st, long long en, long long rq, long long val) {     long long ans;     while (st <= en) {       long long mid = (st + en) / 2;       long long aa = v[mid] - v[rq];       if (aa <= val) {         st = mid + 1;         ans = mid;       } else         en = mid - 1;     }     return abs(ans - rq);   }   bool isPerfectSquare(long double x) {     if (x >= 0) {       long long sr = sqrt(x);       return (sr * sr == x);     }     return false;   }   long long fn(long long s) {     if (s < 10) return s;     long long pr = 1;     while (s) {       if (s % 10) pr *= (s % 10);       s /= 10;     }     return fn(pr);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     vector<vector<long long> > v(11, vector<long long>(1e6 + 1, 0));     for (int i = 1; i <= 1000000; i++) {       v[fn(i)].push_back(i);     }     int n;     long long x;     cin >> x;     while (x--) {       long long start, endd, nm;       cin >> start >> endd >> nm;       long long lower =           lower_bound(v[nm].begin(), v[nm].end(), start) - v[nm].begin();       long long upper =           lower_bound(v[nm].begin(), v[nm].end(), endd) - v[nm].begin();       if (v[nm][upper] == endd) {         cout << (upper - lower) + 1 << endl;       } else {         cout << upper - lower << endl;       }     }     return 0;   }