#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

const ull MAX_N = 1000000000000000000ULL;

map<ull, ull> minRep;

void generate(ull num, ull prod, int lastDigit)
{
    if(num>MAX_N)

    return;


    if (minRep.find(prod)==minRep.end() || num < minRep[prod])
    {
        minRep[prod] = num;
    }


    for (int d=lastDigit;d<=9;d++)
    {
        ull newNum =num*10+d;


        if (newNum >MAX_N)
            break;


        generate(newNum,prod*d,d);
    }
}

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


    minRep[1]=1;


    for (int d =2;d<=9;d++)
    {
        generate(d, d, d);
    }


    vector<ull> reps;


    for (auto& p : minRep)
    {
        reps.push_back(p.second);
    }
    sort(reps.begin(), reps.end());

    int T;

    cin>>T;

    while (T--)
    {
        ull n;

        cin>> n;

        int cnt=upper_bound(reps.begin(),reps.end(), n)-reps.begin();


        if (n>=10)

        cnt++;

        cout<<cnt<<"\n";
    }

    return 0;
}