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
#include <bits/extc++.h>

using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 50'000;
const int K = 17;
ll pre[20][11];

vector<pair<ll, pair<int, ll>>> v;
ll nk[30][30], p9[20];

ll tab[N], il[20][N], td0[20];
int rt[N];

struct chash { // large odd number for C
    const uint64_t C = ll(4e18 * acos(0)) | 71;
    ll operator()(ll x) const { return __builtin_bswap64(x*C); }
};

cc_hash_table<ll, int, chash> hlp;
//unordered_map<ll, int, chash> hlp;
vector<int> pos[20];

ll answer[11];

void Do()
{
    p9[0] = 1LL;
    for(int i = 1; i <= 17; ++i)
        p9[i] = p9[i - 1] * 10LL;
    for(int i = 1; i <= 20; ++i)
    {
        nk[i][0] = 1; nk[i][i] = 1;
        for(int j = 1; j < i; ++j)
            nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
    }
}

void G(ll d)
{
    int l = (int)v.size();
    for(int i = 0; i < l; ++i)
    {
        ll a = 1LL;
        for(int j = 1; j <= K - v[i].nd.st; ++j)
        {
            a *= d;
            v.pb(make_pair(v[i].st * a, make_pair(v[i].nd.st + j, v[i].nd.nd * nk[v[i].nd.st + j][j])));
        }
    }
}

inline int F(ll x)
{
    ll a;
    while(x >= 10LL)
    {
        a = 1LL;
        while(x > 0LL)
        {
            a *= (x % 10LL);
            x /= 10LL;
        }
        x = a;
        //cerr << x << "\n";
    }
    return x;
}

int Gen()
{
    v.pb(make_pair(1LL, make_pair(0, 1LL)));
    int l = 0;
    for(int i = 9; i >= 1; --i)
        G(i);
    sort(v.begin(), v.end());

    for(int i = 1; i <= K; ++i)
        pos[i].pb(1);
    l = 1; tab[1] = 0LL;
    int a;
    for(int i = 0; i < (int)v.size(); ++i)
    {
        if(v[i].st % 10LL == 0LL) a = 1;
        else if(i == 0 || v[i].st != v[i - 1].st) {++l; a = l;}

        if(a != 1 && (i == 0 || (v[i].st != v[i - 1].st || v[i].nd.st != v[i - 1].nd.st)))
            pos[v[i].nd.st].pb(l);
        if(a != 1)
            tab[a] = v[i].st;
        il[v[i].nd.st][a] += v[i].nd.nd;
    }
    for(int i = 1; i <= l; ++i)
    {
        rt[i] = F(tab[i]);
        hlp[tab[i]] = rt[i];
    }
    //cout << l << "\n";
    //cout << "G0" << "\n";
    //for(int i = 1; i <= 5; ++i)
        //cout << il[i][1] << " ";
    //cout << "\n";
    for(int j = K - 1; j >= 1; --j)
        for(int i = 1; i <= l; ++i)
            for(int d = 1; d <= K - j; ++d)
            {
                il[j + d][1] += nk[j + d - 1][d] * il[j][i];
                td0[j + d] += nk[j + d][d] * il[j][i] - nk[j + d - 1][d] * il[j][i];
            }
    for(int j = 1; j <= K; ++j)
    {
        for(int r = 0; r <= 9; ++r)
            pre[j][r] = pre[j - 1][r];
        for(int i = 1; i <= l; ++i)
            pre[j][rt[i]] += il[j][i];
    }
    for(int i = 1; i <= K; ++i)
        il[i][1] += td0[i] + 1LL;
    return l;
}

inline void Dod(ll cur, int lft)
{
    int r = 0;
    if(lft == 0)
    {
        r = F(cur);
        ++answer[r];
        return;
    }
    if(cur % 10LL == 0LL)
    {answer[0] += p9[lft]; return;}
    for(int j = 0; j < (int)pos[lft].size(); ++j)
    {
        int i = pos[lft][j];
        r = hlp[cur * tab[i]];
        //r = F(cur * tab[i]);
        answer[r] += il[lft][i];
    }
}

string S(ll x)
{
    string ans;
    while(x > 0LL)
    {
        ans.pb((char)((int)(x % 10LL) + '0'));
        x /= 10LL;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

void Solve()
{
    ll n; string s;
    cin >> n;
    if(n == I)
    {
        ++answer[0];
        --n;
    }
    s = S(n);
    int m = s.size();
    //cerr << m << " " << n << " " << s << "\n";
    for(int i = 0; i <= 9; ++i)
        answer[i] += pre[m - 1][i];
    ll cur = 1LL;
    for(int i = 0; i < m; ++i)
    {
        int a = (s[i] - '0');
        //cerr << "1: " << i << " " << a << " " << cur << " " << answer[1] << "\n";
        for(int j = 1; j < a; ++j)
        {
            Dod(cur * (ll)j, m - i - 1);
        }
        if(i > 0 && a > 0)
            Dod(0LL, m - i - 1);
        cur *= (ll)a;
    }
    ++answer[F(n)];
    for(int i = 0; i <= 9; ++i)
        cout << answer[i] << " ";
    cout << "\n";
    for(int i = 0; i <= 9; ++i)
        answer[i] = 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    Do();
    Gen();
    int t; cin >> t;
    while(t--)
        Solve();

    return 0;
}