1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <bits/stdc++.h>

#define ndl '\n'
#define ll long long
#define st first
#define nd second
#define debug(x) cout << #x << ": " << x << ndl
#define all(x) (x).begin(), (x).end()

using namespace std;


vector<vector<ll>> pow_mem(10, vector<ll>(70, -1));

ll ppow(ll a, ll b) {
    if(pow_mem[a][b]!=-1) {
        return pow_mem[a][b];
    }
    return pow_mem[a][b] = pow(a,b);
}


struct Hasher {
    size_t operator()(const tuple<int, bool, bool, int, int, int, int>& zmn) const {
        auto [pos, smaller, started, i2, i3, i5, i7] = zmn;
        ll res = 17;
        res = res * 31 + hash<int>()(pos);
        res = res * 31 + hash<bool>()(smaller);
        res = res * 31 + hash<bool>()(started);
        res = res * 31 + hash<int>()(i2);
        res = res * 31 + hash<int>()(i3);
        res = res * 31 + hash<int>()(i5);
        res = res * 31 + hash<int>()(i7);
        return res;
    }
};

struct Hasher2 {
    size_t operator()(const tuple<int, bool, int, int, int, int>& zmn) const {
        auto [pos, started, i2, i3, i5, i7] = zmn;
        ll res = 17;
        res = res * 31 + hash<int>()(pos);
        res = res * 31 + hash<bool>()(started);
        res = res * 31 + hash<int>()(i2);
        res = res * 31 + hash<int>()(i3);
        res = res * 31 + hash<int>()(i5);
        res = res * 31 + hash<int>()(i7);
        return res;
    }
};


struct v_sol{
    v_sol() : v(10, 0) {}
    v_sol(int num) : v(10, 0) {
        this->v[num]++;
    }
    v_sol operator+(const v_sol &other) {
        v_sol newer{};
        for(int i=0;i<10;i++) {
            newer.v[i] = this->v[i] + other.v[i];
        }
        return newer;
    }
    v_sol& operator+=(const v_sol &other) {
        for(int i=0;i<10;i++) {
            this->v[i] += other.v[i];
        }
        return *this;
    }
    friend ostream& operator<<(ostream& os, const v_sol& sol);
    vector<ll> v;
};

ostream& operator<<(ostream& os, const v_sol& sol) {
    for(int i=0;i<=9;i++) {
        os << sol.v[i] <<' ';
    }
    return os;
}


unordered_map<tuple<int, bool, int, int, int, int>, v_sol, Hasher2> global_memo; // ile left, ZAWSZE JEST SMALLER, started, i2, i3, i5, i7


void rob() {
    ll n; cin>>n;
    ll n_cpy = n;
    vector<int> liczba;
    while(n_cpy>0 || (n_cpy==0 && liczba.size()==0)) {
        liczba.push_back(n_cpy%10);
        n_cpy/=10;
    }

    reverse(liczba.begin(), liczba.end());


    unordered_map<tuple<int, bool, bool, int, int, int, int>, v_sol, Hasher> memo;

    function<v_sol(int, bool, bool, int, int, int, int)> digit_dp = [&](int pos, bool smaller, bool started, int i2, int i3, int i5, int i7) {
        tuple<int, bool, bool, int, int, int, int> klucz{pos, smaller, started, i2, i3, i5, i7};
        tuple<int, bool, int, int, int, int> global_klucz{liczba.size()-pos, started, i2, i3, i5, i7};
        // if(i2 == 1 && i5 == 1) {
        //     cout<<"FSDF";
        // }
        if(pos == liczba.size()) {
            if(!started) {
                return memo[klucz] = v_sol();
            }
            if(long long nowa = ppow(2, i2) * ppow(3, i3) * ppow(5, i5) * ppow(7, i7); nowa <= 9) {
                return memo[klucz] = v_sol(nowa);
            } else {
                int new_i2 = 0;
                int new_i3 = 0;
                int new_i5 = 0;
                int new_i7 = 0;
                ll nowa_cpy = nowa;

                while(nowa > 0) {
                    int zz = nowa%10;
                    switch(zz) {
                    case 0: 
                        return memo[klucz] = v_sol(0);
                    case 2:
                        new_i2 += 1;
                        break;
                    case 3:
                        new_i3 += 1;
                        break;
                    case 4:
                        new_i2 += 2;
                        break;
                    case 5:
                        new_i5 += 1;
                        break;
                    case 6:
                        new_i2 += 1;
                        new_i3 += 1;
                        break;
                    case 7:
                        new_i7 += 1;
                        break;
                    case 8:
                        new_i2 += 3;
                        break;
                    case 9:
                        new_i3 += 2;
                        break;
                    default:
                        break;
                    }
                    nowa/=10;
                }

                auto zzzzz = digit_dp(liczba.size(), nowa_cpy < n, true, new_i2, new_i3, new_i5, new_i7);

                if(smaller) {
                    global_memo[global_klucz] = zzzzz;
                }

                return memo[klucz] = zzzzz; 
            }
        }


        if(memo.count(klucz)) {
            return memo[klucz];
        }

        if(smaller && global_memo.count(global_klucz)) {
            return memo[klucz] = global_memo[global_klucz];
        }

        int limit = liczba[pos];
        if(smaller) {
            limit = 9;
        }

        auto sol = v_sol{};

        int new_i2, new_i3, new_i5, new_i7;
        for(ll i=0;i<=limit;i++) {
            if(started && i == 0) {
                if(smaller || 0 < liczba[pos]) {
                    sol.v[0] += pow(10, liczba.size() - pos - 1);
                } else {
                    ll tmpnum = 0;
                    for(int kk = pos+1; kk<liczba.size();kk++) {
                        tmpnum = tmpnum * 10 + liczba[kk];
                    }
                    sol.v[0] += tmpnum+1;
                }
                continue;
            }

            new_i2 = i2;
            new_i3 = i3;
            new_i5 = i5;
            new_i7 = i7;

            switch(i) {
            case 2:
                new_i2 += 1;
                break;
            case 3:
                new_i3 += 1;
                break;
            case 4:
                new_i2 += 2;
                break;
            case 5:
                new_i5 += 1;
                break;
            case 6:
                new_i2 += 1;
                new_i3 += 1;
                break;
            case 7:
                new_i7 += 1;
                break;
            case 8:
                new_i2 += 3;
                break;
            case 9:
                new_i3 += 2;
                break;
            default:
                break;
            }

            sol += digit_dp(pos+1, smaller || (i < liczba[pos]), started || (i > 0), new_i2, new_i3, new_i5, new_i7);
        } 

        if(smaller) {
            global_memo[global_klucz] = sol;
        }

        return memo[klucz] = sol;
    };
    cout<<digit_dp(0, 0, 0, 0, 0, 0, 0)<<'\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
        rob();
}