#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
int main()
{
	int t;
	std::cin >> t;
	std::vector<long long> v;
	for (int k = 0; k < t; k++) {
		long long n;
		std::cin >> n;
		int m = 0;
		std::unordered_map<long long, int> rez;
		for (long long i = 1; i <= n; i++) {
			std::string s = std::to_string(i);
			long long p = 1;
			for (int j = 0; j < s.length(); j++) {
				p *= (int)s[j] - 48;
			}
			if (rez[p] != -1) {
				m++;
				rez[p] = -1;
			}
		}
		v.push_back(m);
	}
	for (int x : v) {
		std::cout << x << std::endl;
	}
}