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

public class Program
{
    // moram unapred jer je vrem b ilo preko 2 s
    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 GenNoZero(long n, long curr, int len, HashSet<long> set)
    {
        if (curr > n) return;
        if (curr > 0) 
        {
            long prod = 1;
            long x = curr;
            while (x > 0)
            {
                prod *= x % 10;
                x /= 10;
            }
            set.Add(prod);
        }
        if (curr == 0 && len > 0) return;
        for (int d = 1; d <= 9; d++)
        {
            long next = curr * 10 + d;
            if (next > n) break;
            GenNoZero(n, next, 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>(unapred);
                GenNoZero(n, 0, 0, set);
                Console.WriteLine(set.Count);
            }
        }
    }
}