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
#include <iostream>
#include <unordered_map>
#include <limits.h>
#include <array>
#include <utility>
#include <vector>

using namespace std;

inline short int get_digit(long long val) {
    while(val >= 10) {
        long long temp_val = 1;
        while(val) {
            temp_val *= val % 10;
            val /= 10;
        }
        val = temp_val;
    }
    return val;
}

void limited_generate_numbers(long long cur_number, char started, unordered_map<long long, pair<short int, array<int, 4>>> & res_digit, array<int, 4> & digit_count, long long const n) {
    short int cur_digit = get_digit(cur_number);
    if(cur_digit) {
        res_digit[cur_number] = make_pair(cur_digit, digit_count);
    }
    if(cur_number*7 <= n) {
        digit_count[3]++;
        limited_generate_numbers(cur_number*7, 7, res_digit, digit_count, n);
        digit_count[3]--;
    }
    if(started > 5) {
        return;
    }
    if(cur_number*5 <= n) {
        digit_count[2]++;
        limited_generate_numbers(cur_number*5, 5, res_digit, digit_count, n);
        digit_count[2]--;
    }
    if(started > 3) {
        return;
    }
    if(cur_number*3 <= n) {
        digit_count[1]++;
        limited_generate_numbers(cur_number*3, 3, res_digit, digit_count, n);
        digit_count[1]--;
    }
    if(started > 2) {
        return;
    }
    if(cur_number*2 <= n) {
        digit_count[0]++;
        limited_generate_numbers(cur_number*2, 2, res_digit, digit_count, n);
        digit_count[0]--;
    }
}

long long get_permutations(array<short int, 10> const & freq, array<long long, 19> const & factorial) {
    short int n = 0;
    for(auto const fr : freq) {
        n += fr;
    }
    long long permutations = factorial[n];
    for(auto const fr : freq) {
        permutations /= factorial[fr];
    }
    return permutations;
}

long long count_equal(array<short int, 10> & freq, array<long long, 19> const & factorial, int const max_res_len,
    vector<short int> const & max_res_digits, int const set_ind) {
        long long result = 0;
        for(int j = 0; j < max_res_digits[max_res_len-set_ind-1]; j++) {
            if(freq[j] > 0) {
                freq[j]--;
                result += get_permutations(freq, factorial);
                freq[j]++;
            }
        }
        if(freq[max_res_digits[max_res_len-set_ind-1]] > 0) {
            freq[max_res_digits[max_res_len-set_ind-1]]--;
            result += count_equal(freq, factorial, max_res_len, max_res_digits, set_ind+1);
            freq[max_res_digits[max_res_len-set_ind-1]]++;
        }
        return result;
    }

long long get_count_for_all_merges(array<short int, 10> & freq, char const started, array<long long, 19> const & factorial, int const max_res_len,
    vector<short int> const & max_res_digits) {
    
    long long result = 0;
    int digits_cnt = 0;
    for(auto const el : freq) {
        digits_cnt += el;
    }

    if(digits_cnt < max_res_len) {
        long long short_perm = get_permutations(freq, factorial);
        result += short_perm;
        for(int i = 1; i < max_res_len - digits_cnt; i++) {
            short_perm *= digits_cnt + i;
            short_perm /= i;
            result += short_perm;
        }
    }

    if(max_res_len >= digits_cnt) {
        freq[1] = max_res_len - digits_cnt;
        result += count_equal(freq, factorial, max_res_len, max_res_digits, 0);
        freq[1] = 0;
    }

    if(freq[3] >= 2) {
        freq[3] -= 2;
        freq[9]++;
        result += get_count_for_all_merges(freq, 9, factorial, max_res_len, max_res_digits);
        freq[9]--;
        freq[3] += 2;
    }

    if(started > 8) {
        return result;
    }
    if(freq[2] >= 3) {
        freq[2] -= 3;
        freq[8]++;
        result += get_count_for_all_merges(freq, 8, factorial, max_res_len, max_res_digits);
        freq[8]--;
        freq[2] += 3;
    }

    if(started > 6) {
        return result;
    }
    if(freq[2] >= 1 && freq[3] >= 1) {
        freq[2]--;
        freq[3]--;
        freq[6]++;
        result += get_count_for_all_merges(freq, 6, factorial, max_res_len, max_res_digits);
        freq[6]--;
        freq[3]++;
        freq[2]++;
    }
    
    if(started > 4) {
        return result;
    }
    if(freq[2] >= 2) {
        freq[2] -= 2;
        freq[4]++;
        result += get_count_for_all_merges(freq, 4, factorial, max_res_len, max_res_digits);
        freq[4]--;
        freq[2] += 2;
    }

    return result;
}

long long get_digits_num(long long const max_res, long long const gen_num, array<int, 4> primes, array<long long, 19> const & factorial) {
    int max_res_len = 0;
    vector<short int> max_res_digits;
    long long temp = max_res;
    while(temp) {
        max_res_len++;
        max_res_digits.push_back(temp % 10);
        temp /= 10;
    }

    array<short int, 10> freq{};

    freq[2] = primes[0];
    freq[3] = primes[1];
    freq[5] = primes[2];
    freq[7] = primes[3];

    long long result = get_count_for_all_merges(freq, 0, factorial, max_res_len, max_res_digits);
    return result;
}

void solve(unordered_map<long long, pair<short int, array<int, 4>>> const & res_digit) {
    long long n;
    cin >> n;

    array<long long, 19> factorial{};
    factorial[0] = 1;
    for(int i = 1; i <= 18; i++) {
        factorial[i] = factorial[i-1] * i;
    }

    array<long long, 10> results{};
    for(auto const & el : res_digit) {
        results[el.second.first] += get_digits_num(n, el.first, el.second.second, factorial);
    }

    short int n_digit = get_digit(n);
    if(n_digit != 0 && n_digit != 1) {
        results[get_digit(n)]++;
    }

    long long temp = 1;
    while(temp <= n) {
        results[1]++;
        temp *= 10;
        temp++;
    }

    long long non0sum = 0;
    for(auto const el : results) {
        non0sum += el;
    }

    results[0] = n - non0sum;

    for(auto const & el : results) {
        cout << el << " ";
    }
    cout << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    unordered_map<long long, pair<short int, array<int, 4>>> res_digit;
    array<int, 4> digit_count{};
    limited_generate_numbers(1, 1, res_digit, digit_count, 1e18);
    res_digit.erase(1);

    int t;
    cin >> t;
    while(t--) {
        solve(res_digit);
    }

    return 0;
}