#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define mp make_pair
const int lg=18;
const ll INF=1e18;
ll Calc(ll x){
    ll res=1;
    while(x>0)res*=x%10,x/=10;
    return res;
}
ll Calc1(ll x){
    vector<ll>vals;
    for(int i=1;i<=x;i++)vals.pb(Calc(i));
    sort(vals.begin(),vals.end());
    vals.resize(unique(vals.begin(),vals.end())-vals.begin());
    //for(auto i:vals)printf("%lld ",i);printf("\n");
    return vals.size();
}
vector<ll>vals[lg+1];
/*ll Calc2(ll n){
    ll n1=n;
    int cifra=0,num=-1;
    while(n)cifra=n%10,n/=10,num++;
    //printf("%i %i\n",cifra,num);
    ll res=max(cifra-1,1)*(dp[num]-1)+1;
    if(num==0)res=n1;
    return res;
}*/
/*ll najmanji(ll x){
    if(x==0)return 10;
    if(x==1)return 1;
    int num[10]={0};
    for(int i=2;i<=9;i++)while(x%i==0){
        num[i]++;
        x/=i;
    }
    for(int i=2;i<=9;i++){
        for(int j=2;j<=9;j++)if(i*j<=9){
            if(i==j){
                num[i*i]+=(num[i]>>1);
                num[i]&=1;
            }
            else{
                int d=min(num[i],num[j]);
                num[i*j]+=d;
                num[i]-=d;
                num[j]-=d;
            }
        }
    }
    vector<int>cifre;
    for(int i=9;i>=2;i--)for(int j=1;j<=num[i];j++)cifre.pb(i);
    ll res=0;
    for(ll i=0,e=1;i<cifre.size();i++,e*=10)res+=e*cifre[i];
    return res;
}*/
map<ll,int>mapa;
vector<ll>Vals;
ll najmanji(ll x){
    int I=mapa[x];
    if(Vals[I]>0)return Vals[I];
    if(x==0)return Vals[I]=10;
    if(x<=9)return Vals[I]=x;
    ll res=INF;
    for(int i=2;i<=9;i++)if(x%i==0){
        ll y=najmanji(x/i);
        y=10*y+i;
        res=min(res,y);
    }
    return Vals[I]=res;
}
ll Solve(ll n){
    return upper_bound(Vals.begin(),Vals.end(),n)-Vals.begin();
}
int main(){
    /*for(int j=1;j<=9;j++)F[1][j]=j;
    for(int i=2;i<20;i++){
        for(int j=1;j<=9;j++){
            F[i][j]=F[i][j-1]+F[i-1][j];
        }
    }
    for(int i=1;i<20;i++)dp[i]=F[i][9];*/
    for(int i=0;i<=9;i++)vals[1].pb(i);
    for(int i=2;i<=lg;i++){
        for(int j=1;j<=9;j++){
            for(auto k:vals[i-1]){
                vals[i].pb(k*j);
            }
        }
        sort(vals[i].begin(),vals[i].end());
        vals[i].resize(unique(vals[i].begin(),vals[i].end())-vals[i].begin());
    }
    //for(int i=1;i<=lg;i++)dp[i]=vals[i].size();
    //for(int i=1;i<=lg;i++)printf("%i\n",vals[i].size());
    //for(int i=0;i<15;i++)printf("%lld %lld\n",vals[lg][i],najmanji(vals[lg][i]));
    for(int i=0;i<vals[lg].size();i++){
        mapa[vals[lg][i]]=i;
    }
    Vals.resize(vals[lg].size());
    for(auto i:vals[lg])Vals[mapa[i]]=najmanji(i);
    sort(Vals.begin(),Vals.end());
    //for(int i=0;i<15;i++)printf("%lld %lld\n",vals[lg][i],Vals[i]);printf("\n");
    //for(int i=1;i<20;i++)for(int j=1;j<=9;j++)printf("%i %i: %lld\n",i,j,F[i][j]);
    /*for(int i=1;i<=10000;i++)if(Calc1(i)!=Calc2(i)){
        printf("%i: %lld %lld",i,Calc1(i),Calc2(i));
        break;
    }*/
    int t;scanf("%i",&t);
    while(t--){
        ll n;scanf("%lld",&n);
        ll res=Solve(n);
        printf("%lld\n",res);
    }
    return 0;
}