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.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class codechef_first_code {
    InputStream is;
    PrintWriter out;
    String INPUT = "";

    int gcd(int a, int b)
    {
        if(b==0)return a;
        return gcd(b,a%b);
    }

    int upper_bound(ArrayList a,int k,int l,int r){
        while (l<=r){
           // out.println(l+" "+r);
            int mid=(l+r)/2;
            if((Integer)a.get(mid)<k){
                l=mid+1;
            }
            else if((Integer)a.get(mid)>k){
                r=mid-1;
            }
            else return mid;

        }
        return l;
    }


    int lower_bound(ArrayList a,int k,int l,int r){

        while (l<=r){
            //out.println(l+" "+r);
            int mid=(l+r)/2;
            if((Integer)a.get(mid)<k){
                //out.println("b"+(Integer)a.get(mid));
                l=mid+1;
            }
            else if((Integer)a.get(mid)>k){
                r=mid-1;
            }
            else return mid;

        }
        return r;
    }

    int pr(int num){

        int ans=1;
        while (num>0){
            int r=num%10;
            if(r!=0)ans*=r;
            num/=10;
        }
        return ans;
    }

    int f(int i,int dp[]){
        if(i<10)return i;
        return f(pr(i),dp);
    }

    void solve() {
       int dp[]=new int[1000001];
       Arrays.fill(dp,-1);
        for (int i = 0; i <1000001 ; i++) {
            dp[i]=f(i,dp);
        }

        ArrayList l[]=new ArrayList[10];
        for (int i = 0; i <10 ; i++) {
            l[i]=new ArrayList<Integer>();
        }
        for (int i = 0; i <1000001 ; i++) {
            l[dp[i]].add(i);
        }
        int q=ni();

        while (q-->0){
            int l1=ni();
            int r1=ni();
            int k=ni();

           
            if(upper_bound(l[k],r1,0,l[k].size()-1) <lower_bound(l[k],l1,0,l[k].size()-1)){
                out.println(0);
            }
            else out.println(lower_bound(l[k],r1,0,l[k].size()-1)-upper_bound(l[k],l1,0,l[k].size()-1)+1);
        }
    }

    void run() throws Exception
    {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
        solve();
        out.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }

    public static void main(String[] args) throws Exception { new codechef_first_code().run(); }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private int[][] matix(int y1,int y2){
        int a[][]=new int[y1][y2];
        for (int i = 0; i <y1 ; i++) {
            for (int j = 0; j < y2; j++) {
                a[i][j]=ni();
            }
        }
        return a;
    }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}