using System;
using System.Collections.Generic;

public class Program
{
    static void gen(long n, int max, long cur, long pr, int duz, Dictionary<long, long> min)
    {
        if (duz > 0)
        {
            if (!min.ContainsKey(pr) || cur < min[pr])
                min[pr] = cur;
        }
        if (duz == max) return;
        for (int d = 1; d <= 9; d++)
        {
            long nxt = cur * 10 + d;
            gen(n, max, nxt, pr * d, duz + 1, min);
        }
    }

    static bool nula(long x) {
        while (x > 0) {
            if (x % 10 == 0) return true;
            x /= 10;
        }
        return false;
    }

    public static void Main(string[] args)
    {
        int t = int.Parse(Console.ReadLine());
        for (int i = 0; i < t; i++)
        {
            long n = long.Parse(Console.ReadLine());
            if (n < 10) {
                Console.WriteLine(n);
                continue;
            }
            int max = n.ToString().Length;
            var min = new Dictionary<long, long>();
            gen(n, max, 0, 1, 0, min);
            int br = 0;
            foreach (var kv in min)
            {
                if (kv.Value <= n) br++;
            }
            bool ima0 = false;
            for (long j = 10; j <= n; j *= 10) ima0 = true;
            for (long j = 1; j <= n; j++) {
                if (nula(j)) { ima0 = true; break; }
            }
            if (ima0) br++;
            Console.WriteLine(br);
        }
    }
}