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

public class Program
{

    static int Sol(long num)
    {
        if (num <= 10000)
        {
            var set = new bool[1000000];
            for (long val = 1; val <= num; val++)
            {
                long tmp = val, pro = 1;
                while (tmp > 0)
                {
                    pro *= tmp % 10;
                    tmp /= 10;
                }
                set[pro] = true;
            }
            int cnt = 0;
            for (int i = 0; i < set.Length; i++) if (set[i]) cnt++;
            return cnt;
        }
        var dig = new List<int>();
        long tmp2 = num;
        while (tmp2 > 0)
        {
            dig.Add((int)(tmp2 % 10));
            tmp2 /= 10;
        }
        dig.Reverse();
        var mem = new Dictionary<(int, bool, bool, long), bool[]>();
        bool[] DP(int pos, bool tig, bool sta, long pro)
        {
            var key = (pos, tig, sta, pro);
            if (mem.ContainsKey(key)) return mem[key];
            bool[] res = new bool[1000000];
            if (pos == dig.Count)
            {
                if (sta && pro < res.Length) res[pro] = true;
                return res;
            }
            int mx = tig ? dig[pos] : 9;
            for (int d = 0; d <= mx; d++)
            {
                bool ntg = tig && (d == mx);
                bool nst = sta || d != 0;
                long npr = nst ? pro * d : pro;
                if (!nst) npr = 1;
                if (npr >= res.Length) continue;
                var sub = DP(pos + 1, ntg, nst, npr);
                for (int i = 0; i < res.Length; i++)
                    res[i] |= sub[i];
            }
            mem[key] = res;
            return res;
        }
        var ret = DP(0, true, false, 1);
        int cnt2 = 0;
        for (int i = 0; i < ret.Length; i++) if (ret[i]) cnt2++;
        return cnt2;
    }

    public static void Main(string[] args)
    {
        int ttt = int.Parse(Console.ReadLine());
        for (int iii = 0; iii < ttt; iii++)
        {
            long num = long.Parse(Console.ReadLine());
            int res = Sol(num);
            Console.WriteLine(res);
        }
    }
}