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
#include "bits/stdc++.h"
using namespace std;

#define rep(i, b, e) for(int i = (b); i <= (e); i++)
#define per(i, b, e) for(int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e) - 1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")"; }
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
    ((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif

ll mnoz(ll x) {
    ll res = 1;
    do { res *= x % 10, x /= 10; } while(x > 0);
    return res;
}

int path(ll x) { return x < 10 ? x : path(mnoz(x)); }

int digs(int a, int b, int c, int d) {
    int res = a / 3 + b / 2 + c + d, rem = a % 3 + b % 2;
    res += (rem > 0) + rem / 3;
    return res + (res == 0);
}

ll pov(ll base, ll exp) {
    ll res = (exp & 1) ? base : 1;
    for(exp >>= 1; exp; exp >>= 1) {
        base *= base;
        if(exp & 1) res = res * base;
    }
    return res;
}

const int B = 20;
const int Q = 36100;
const int D = 19;

array<int, 4> quads[Q];
array<int, 4> fact[10];
int pathEnd[Q];
ll dp[Q][D];
array<ll, 10> splot[Q][D];
int id[B * 3][B * 2][B][B];

int getId(const array<int, 4> &quad) {
    FOR(i, 0, 4) if(quad[i] < 0) return -1;
    return id[quad[0]][quad[1]][quad[2]][quad[3]];
}

array<int, 4> operator+(const array<int, 4> &a, const array<int, 4> &b) {
    return {a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]};
}

array<int, 4> operator-(const array<int, 4> &a, const array<int, 4> &b) {
    return {a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]};
}

array<ll, 10> operator+(const array<ll, 10> &a, const array<ll, 10> &b) {
    return {a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3], a[4] + b[4], a[5] + b[5], a[6] + b[6], a[7] + b[7], a[8] + b[8], a[9] + b[9]};
}

void prep() {
    int g = 0;
    FOR(a, 0, B * 3) FOR(b, 0, B * 2) FOR(c, 0, B) FOR(d, 0, B) {
        id[a][b][c][d] = -1;
        if(digs(a, b, c, d) < D) {
            ll me = pov(2, a) * pov(3, b) * pov(5, c) * pov(7, d);
            pathEnd[g] = path(me);
            id[a][b][c][d] = g;
            quads[g++] = {a, b, c, d};
        }
    }
    FOR(dig, 1, 10) {
        int me = dig;
        array<int, 4> vec = {2, 3, 5, 7};
        FOR(ind, 0, 4) while(me % vec[ind] == 0) me /= vec[ind], fact[dig][ind]++;
    }
    dp[0][0] = 1;
    FOR(d, 1, D) FOR(q, 0, Q) FOR(dig, 1, 10) {
        int befq = getId(quads[q] - fact[dig]);
        if(befq != -1) dp[q][d] += dp[befq][d-1];
    }
    FOR(me, 0, Q) FOR(q, me, Q) {
        int befq = getId(quads[q] - quads[me]);
        if(befq != -1) FOR(d, 0, D) splot[me][d][pathEnd[q]] += dp[befq][d];
    }
}

vi convert(ll n) {
    vi vec;
    do { vec.pb(n % 10), n /= 10; } while(n > 0);
    reverse(all(vec));
    return vec;
}

void solve() {
    ll n;
    cin >> n;
    array<ll, 10> ans = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    vi vec = convert(n);
    int digsOnRight = SZ(vec) - 1;
    array<int, 4> akt = {0, 0, 0, 0};
    FOR(ds, 1, digsOnRight + 1) ans = ans + splot[0][ds];
    FOR(i, 0, SZ(vec)) {
        int dig = vec[i];
        FOR(sd, 1, dig + (i + 1 == SZ(vec))) {
            auto me = getId(akt + fact[sd]);
            ans = ans + splot[me][digsOnRight];
        }
        if(dig == 0) break;
        digsOnRight--;
        akt = akt + fact[dig];
    }
    ans[0] += n - accumulate(all(ans), 0ll);
    FOR(i, 0, 10) cout << ans[i] << " \n"[i == 9];
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    prep();
    int tt = 1;
    cin >> tt;
    FOR(te, 0, tt) solve();
    return 0;
}