#include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define pb push_back

using namespace std;

using ll = long long;

ll n=1e6+15;
vector<ll>resenja(n);
map<ll,ll>mp;

void solve()
{
    ll x;
    cin >> x;
    cout << resenja[x] << endl;



}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    ll i;
    for(i=1;i<n;i++)
    {
        ll res=1;
        ll broj=i;
        while(broj)
        {
            res*=broj%10;
            broj/=10;
        }
        mp[res]++;
        if(mp[res]==1)
        {
            resenja[i]++;
        }
        resenja[i]+=resenja[i-1];
    }
    cin >> t;
    while (t--)
    {
        solve();
    }
}