using System;
using System.Collections; 
using System.Collections.Generic; 
namespace Zadatak3
{
	class Program
	{
		public static void Main(string[] args)
		{
			
			int n=int.Parse(Console.ReadLine());
			int[] x=new int[n];
			
			for(int i=0;i<n;i++)
			{
				x[i]=int.Parse(Console.ReadLine());
			}
			
			for(int i=0;i<n;i++)
			{
				int br=1;
				int br1=0;
				int sum=1;
				List<int> brojevi = new List<int>();
				while(br<=x[i])
				{
					sum=1;
					int temp=br;
					while(temp!=0)
					{
						sum=sum*(temp%10);
						temp=temp/10;
					}
					br++;
					if(brojevi.Contains(sum))
					{
					}
					else
					{
						brojevi.Add(sum);
						br1++;
					}
				}
				Console.WriteLine(br1+"");
			}
		}
	}
}