using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    static HashSet<long> unapred = unapredizracun();
    static HashSet<long> unapredizracun()
    {
        var set = new HashSet<long>();
        for (int i = 1; i < 1_000_000; i++)
        {
            int x = i;
            long prod = 1;
            while (x > 0)
            {
                prod *= x % 10;
                x /= 10;
            }
            set.Add(prod);
        }
        return set;
    }

    static void GenProducts(long n, long curr, long prod, int len, HashSet<long> set)
    {
        if (curr > n) return;
        if (curr > 0) set.Add(prod);
        if (len == 12) return;
        for (int d = 1; d <= 9; d++)
        {
            long next = curr * 10 + d;
            if (next > n) break;
            GenProducts(n, next, prod * d, len + 1, set);
        }
    }

    public static void Main(string[] args)
    {
        int T = int.Parse(Console.ReadLine());
        for (int t = 0; t < T; t++)
        {
            long n = long.Parse(Console.ReadLine());
            if (n < 1_000_000)
            {
                var set = new HashSet<long>();
                for (long i = 1; i <= n; i++)
                {
                    long x = i, prod = 1;
                    while (x > 0)
                    {
                        prod *= x % 10;
                        x /= 10;
                    }
                    set.Add(prod);
                }
                Console.WriteLine(set.Count);
            }
            else
            {
                var set = new HashSet<long>();
                GenProducts(n, 0, 1, 0, set);
                Console.WriteLine(set.Count);
            }
        }
    }
}