#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAX 1000000

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);

    int t; cin>>t;

    vector<int> cache(MAX + 1);
    unordered_set<int> s;
    int ctr = 0;

    for (int i = 1; i <= MAX; ++i){
        int digits = 0;
        int mult = 1;
        int r = i;

        while (pow(10LL, digits) <= i)
            ++digits;

        for (int k = 0; k < digits; ++k){
            int digit = r % 10;
            r/=10;
            mult *= digit;
        }

        if (s.find(mult) == s.end()){
            ++ctr;
            s.insert(mult);
        }

        cache[i] = ctr;
    }

    for (int i = 0; i < t; ++i){
        ll n;cin>>n;

        cout<<cache[n]<<'\n';
    }

    return 0;
}