Problem: Let us define two functions f and g on positive integer numbers. 

<image>

<image>

You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k. 

Input

The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries. 

Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).

Output

For each query, print a single line containing the answer for that query.

Examples

Input

4
22 73 9
45 64 6
47 55 7
2 62 4


Output

1
4
0
8


Input

4
82 94 6
56 67 4
28 59 9
39 74 4


Output

3
1
1
5

Note

In the first example:

  * g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
  * g(47) = g(48) = g(60) = g(61) = 6
  * There are no such integers between 47 and 55. 
  * g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4

Solution (in JAVA):

Solution:
/*
 
    /$$$$$  /$$$$$$  /$$    /$$  /$$$$$$                                                                  
   |__  $$ /$$__  $$ |$$    |$$ /$$__  $$                                                                 
          | $$| $$  \ $$|    $$|$$| $$  \ $$                                                                 
          | $$| $$$$$$$$|  $$ / $$/| $$$$$$$$                                                                 
/ $$  | $$| $$__  $$ \  $$ $$/ | $$__  $$                                                                 
| $$  | $$| $$  | $$  \  $$$/  | $$  | $$                                                                 
|  $$$$$$/| $$  | $$   \  $/   | $$  | $$                                                                 
 \______/ |__/  |__/    \_/    |__/  |__/    
 
/$$$$$$$  /$$$$$$$   /$$$$$$   /$$$$$$  /$$$$$$$   /$$$$$$  /$$      /$$ /$$      /$$ /$$$$$$$$ /$$$$$$$ 
| $$__  $$| $$__  $$ /$$__  $$ /$$__  $$| $$__  $$ /$$__  $$| $$$    /$$$| $$$    /$$$| $$_____/| $$__  $$
| $$  \ $$| $$  \ $$| $$  \ $$| $$  \__/| $$  \ $$| $$  \ $$| $$$$  /$$$$| $$$$  /$$$$| $$      | $$  \ $$
| $$$$$$$/| $$$$$$$/| $$  | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$   | $$$$$$$/
| $$____/ | $$__  $$| $$  | $$| $$|_  $$| $$__  $$| $$__  $$| $$  $$$| $$| $$  $$$| $$| $$__/   | $$__  $$
| $$      | $$  \ $$| $$  | $$| $$  \ $$| $$  \ $$| $$  | $$| $$\  $ | $$| $$\  $ | $$| $$      | $$  \ $$
| $$      | $$  | $$|  $$$$$$/|  $$$$$$/| $$  | $$| $$  | $$| $$ \/  | $$| $$ \/  | $$| $$$$$$$$| $$  | $$
|__/      |__/  |__/ \______/  \______/ |__/  |__/|__/  |__/|__/     |__/|__/     |__/|________/|__/  |__/
                                                                                                                                                                                                                
                                                                                                                                                                                                                
 
*/
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
 public class codeforces
{
    static void shuffle( int arr[])
    {
        int n = arr.length;
        Random r = new Random();
         
       for (int i = n-1; i > 0; i--) {
           int j = r.nextInt(i+1);
             
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    static void shuffle( int arr[][])
    {
        int n = arr.length;
        Random r = new Random();
         
       for (int i = n-1; i > 0; i--) {
           int j = r.nextInt(i+1);
             
            int[] temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        int[][] tdiarr(int m , int n){
            int[][] a = new int[m][n];
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    a[i][j]=Integer.parseInt(next());
                }
            }
            return a;
        } 
        int[] niarr(int n)
        {
            int[] a = new int[n];
            for(int i=0;i<n;i++)a[i]=Integer.parseInt(next());
            return a;
        }
        long[] nlarr(int n)
        {
            long[] a = new long[n];
            for(int i=0;i<n;i++)a[i]=Long.parseLong(next());
            return a;
        }
        char[] ncarr(){
            return next().trim().toCharArray();
            
            
        }
        
  
        int ni() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nl() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nd() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nstr() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }
    
    static void SegmentTree(int arr[], int n)
    {
        // Allocate memory for segment tree
        //Height of segment tree
        int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
 
        //Maximum size of segment tree
        int max_size = 2 * (int) Math.pow(2, x) - 1;
 
         int[] st = new int[max_size]; // Memory allocation
 
        constructSTUtil(arr, 0, n - 1, 0);
    }
    static int constructSTUtil(int arr[], int ss, int se, int si)
    {
        // If there is one element in array, store it in current node of
        // segment tree and return
        if (ss == se) {
            st[si] = arr[ss];
            return arr[ss];
        }
 
        // If there are more than one elements, then recur for left and
        // right subtrees and store the sum of values in this node
        int mid = getMid(ss, se);
        st[si] = Math.min(constructSTUtil(arr, ss, mid, si * 2 + 1) ,
                 constructSTUtil(arr, mid + 1, se, si * 2 + 2));
        return st[si];
    }
    static int getMid(int s, int e) {
        return s + (e - s) / 2;
    }
    static int getSum(int n, int qs, int qe)
    {
        // Check for erroneous input values
        if (qs < 0 || qe > n - 1 || qs > qe) {
           // System.out.println("Invalid Input");
            return -1;
        }
        return getSumUtil(0, n - 1, qs, qe, 0);
    }
    static int getSumUtil(int ss, int se, int qs, int qe, int si)
    {
        // If segment of this node is a part of given range, then return
        // the sum of the segment
        if (qs <= ss && qe >= se)
            return st[si];
 
        // If segment of this node is outside the given range
        if (se < qs || ss > qe)
            return Integer.MAX_VALUE;
 
        // If a part of this segment overlaps with the given range
        int mid = getMid(ss, se);
        return Math.min(getSumUtil(ss, mid, qs, qe, 2 * si + 1) ,
                getSumUtil(mid + 1, se, qs, qe, 2 * si + 2));
    }
    static void updateValue(int arr[], int n, int i, int new_val)
    {
        // Check for erroneous input index
        if (i < 0 || i > n - 1) {
//            System.out.println("Invalid Input");
            return;
        }
 
        // Get the difference between new value and old value
        int diff = new_val - arr[i];
 
        // Update the value in array
        arr[i] = new_val;
 
        // Update the values of nodes in segment tree
        updateValueUtil(0, n - 1, i, diff, 0);
    }
    static void updateValueUtil(int ss, int se, int i, int diff, int si)
    {
        // Base Case: If the input index lies outside the range of
        // this segment
        if (i < ss || i > se)
            return;
 
        // If the input index is in range of this node, then update the
        // value of the node and its children
        st[si] = st[si] + diff;
        if (se != ss) {
            int mid = getMid(ss, se);
            updateValueUtil(ss, mid, i, diff, 2 * si + 1);
            updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
        }
    }
    static boolean checkbound(int x , int y , int m , int n){
        if(x<0 || x>=m || y<0 || y>=n)return false;
        return true;
    }
    static void printarr(int[][] a){
        int m = a.length , n = a[0].length;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
    static void printarr(int[] a){
        int n = a.length;
            for(int j=0;j<n;j++){
                System.out.print(a[j]+" ");
            }
            System.out.println();

        
    }

    static void revarr(int[] a ){
        int i=0,j=a.length-1;
        while(i<j){
            int tem = a[i];
            a[i]=a[j];
            a[j]=tem;
            i++;j--;
        }
        //return a;
    }
    
    
    static int[] st;
    static FastReader br=new FastReader();//(new InputStreamReader(System.in));
/*

*/
///////////////////////////////////////////////////////////


 

static int solve(boolean[] vis , int[] g , int m , int ind){
     int u = ind;
     int y = 1;
     if(vis[ind])return g[ind];
     while(u>0){
        int mod = u%10;
        if(mod != 0)y*=mod;
        u/=10;
     }
     if(y<10){

        return y;
     }
     vis[ind]=true;
    g[ind]= g[y];
    return solve(vis,g,m,y);
     
}

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        int t=1;
        br=new FastReader();
            //    t=br.ni();//Integer.parseInt(br.readLine().trim());
        StringBuilder sb=new StringBuilder();
        int m = 1000003; 
        int[] g = new int[m];
       // Arrays.fill(g,1);
        for(int i=0;i<10;i++)g[i]=i;
        boolean[] vis = new boolean[m];
         for(int i=1;i<m;i++){
             if(!vis[i])g[i]=solve(vis,g,m,i);

        }
     //   System.out.println(solve(vis,g,m,47));
        int[][] dp = new int[10][m];
        for(int i=1;i<10;i++){
            for(int j=1;j<m;j++){
                if(g[j]==i){
                    dp[i][j]=1+dp[i][j-1];

                }
                else {
                    dp[i][j]=dp[i][j-1];
                }
            }
        }
    in1 :   while(t-->0)
        {
            int q = br.ni();
            while(q-->0){
                int l = br.ni() , r = br.ni() ,  k = br.ni();
                //for(int j=l;j<=r;j++){
                  //  if(g[j]==k)System.out.print(j+" ");
               // }
               // 
                sb.append((dp[k][r]-dp[k][l-1])+"\n");
            }
             //
                 // solve();
            }
           // printarr(g);
                //sb.append(solve()+"\n");
        
            //          System.out.println("OUTPUT:::");
    
        System.out.println(sb);
    }


        
    /*   */
    }