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

typedef long long ll;

vector<int> num;
ll dp[20][2][10]; // For multiplicative root tracking
int transition[10][10]; // transition[curr_root][digit] = new_root

void precompute_transitions() {
    vector<int> digits = {1, 3, 7, 9};
    for (int curr = 1; curr <= 9; ++curr) {
        for (int d : digits) {
            int product = curr * d;
            while (product >= 10) {
                int p = 1;
                int temp = product;
                while (temp > 0) {
                    p *= temp % 10;
                    temp /= 10;
                }
                product = p;
            }
            transition[curr][d] = product;
        }
    }
}

ll dfs(int pos, int tight, int root, const vector<int>& allowed, bool initial) {
    if (pos == num.size()) {
        return initial ? 0 : (root != -1 ? 1 : 0); // root is set
    }
    if (!tight && !initial && dp[pos][tight][root] != -1)
        return dp[pos][tight][root];
    
    int limit = tight ? num[pos] : 9;
    ll res = 0;
    
    for (int d : allowed) {
        if (d > limit) continue;
        int new_tight = tight && (d == limit);
        int new_root;
        bool new_initial;
        
        if (initial && d == 0) {
            new_initial = true;
            new_root = -1;
        } else {
            new_initial = false;
            if (initial) {
                new_root = d;
            } else {
                new_root = transition[root][d];
            }
        }
        
        if (new_root == -1) new_root = d; // initial case
        
        res += dfs(pos + 1, new_tight, new_root, allowed, new_initial);
    }
    
    if (!tight && !initial)
        dp[pos][tight][root] = res;
    return res;
}

ll compute(const string& s, const vector<int>& allowed_digits, bool track_root) {
    num.clear();
    for (char c : s) {
        num.push_back(c - '0');
    }
    memset(dp, -1, sizeof(dp));
    return dfs(0, 1, -1, allowed_digits, true);
}

vector<ll> compute_category3(const string& s) {
    vector<int> allowed = {1, 3, 7, 9};
    num.clear();
    for (char c : s) {
        num.push_back(c - '0');
    }
    memset(dp, -1, sizeof(dp));
    
    vector<ll> cnt(10, 0);
    function<void(int, int, int, ll)> dfs_cnt = [&](int pos, int tight, int root, ll count) {
        if (pos == num.size()) {
            if (root != -1)
                cnt[root] += count;
            return;
        }
        int limit = tight ? num[pos] : 9;
        for (int d : allowed) {
            if (d > limit) continue;
            int new_tight = tight && (d == limit);
            int new_root = (root == -1) ? d : transition[root][d];
            dfs_cnt(pos + 1, new_tight, new_root, count + (new_root != -1 ? 1 : 0));
        }
    };
    
    dfs_cnt(0, 1, -1, 0);
    return cnt;
}

ll count_no_zeros(const string& s) {
    vector<int> allowed = {1,2,3,4,5,6,7,8,9};
    return compute(s, allowed, false);
}

ll count_no_5(const string& s) {
    vector<int> allowed = {1,2,3,4,6,7,8,9};
    return compute(s, allowed, false);
}

ll count_no_even(const string& s) {
    vector<int> allowed = {1,3,5,7,9};
    return compute(s, allowed, false);
}

ll count_no_5_no_even(const string& s) {
    vector<int> allowed = {1,3,7,9};
    return compute(s, allowed, false);
}

vector<ll> solve(ll n) {
    string s = to_string(n);
    
    ll cnt_no_zero = count_no_zeros(s);
    ll contrib_0_part1 = n - cnt_no_zero;
    
    ll A = count_no_5(s);
    ll B = count_no_even(s);
    ll C = count_no_5_no_even(s);
    
    ll contrib_0_part2 = cnt_no_zero - (A + B - C);
    ll contrib_0 = contrib_0_part1 + contrib_0_part2;
    
    vector<ll> category3 = compute_category3(s);
    
    vector<ll> res(10, 0);
    res[0] = contrib_0;
    for (int i = 1; i <= 9; ++i) {
        res[i] = category3[i];
    }
    
    return res;
}

int main() {
    precompute_transitions();
    
    int t;
    cin >> t;
    vector<ll> ns(t);
    for (int i = 0; i < t; ++i) {
        cin >> ns[i];
    }
    
    for (ll n : ns) {
        auto res = solve(n);
        for (ll x : res) {
            cout << x << " ";
        }
        cout << endl;
    }
    
    return 0;
}