#include <bits/stdc++.h>
using namespace std;

uint64_t memo[1000000] = {0};

int main() {
    ios::sync_with_stdio(false);

    int T;
    cin >> T;

    for(int test = 0; test < T; test++) {
        uint64_t n;
        cin >> n;

        if(n < 1000000 && memo[n] != 0) {
            cout << memo[n] << '\n';
            continue;
        }

        unordered_map<uint64_t, bool> razlicite;
        for(uint64_t i = 1; i <= n; i++) {
            string s = to_string(i);
            uint64_t proizvod = 1;
            for(int j = 0; j < s.size(); j++) {
                proizvod *= s[j] - '0';
            }
            razlicite[proizvod] = true;
            if(i < 1000000)
                memo[i] = razlicite.size();
        }
        cout << razlicite.size() << '\n';
    }
    return 0;
}