using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static long Proizvod(long broj) 
        {
            long proiz = 1;
            while (broj > 0) 
            {
                proiz = proiz * (broj % 10);
                broj /= 10;
            }
            return proiz;
        }
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                long n = Convert.ToInt64(Console.ReadLine());

                Dictionary<long, bool> brojac = new Dictionary<long, bool>();
                long tr = 1;
                for (long j = 1; j <= n; j++)
                {
                    long k = j;
                    long proiz = (Proizvod(j));
                    if (!brojac.ContainsKey(proiz)) brojac[proiz] = true;

                }
                Console.WriteLine(brojac.Count);
            }
        }
    }
}