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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "bits/stdc++.h"

#define int long long
#define pi pair<long long, long long>
#define a4 array<long long, 4>

#define meow main

using namespace std;

int factorials[] = {1,
                    1,
                    2,
                    6,
                    24,
                    120,
                    720,
                    5040,
                    40320,
                    362880,
                    3628800,
                    39916800,
                    479001600,
                    6227020800,
                    87178291200,
                    1307674368000,
                    20922789888000,
                    355687428096000,
                    6402373705728000};

set<a4> to_precompute = {
    {0, 0, 0, 0}, {0, 0, 0, 1},  {0, 0, 0, 2},  {0, 0, 0, 3},  {0, 0, 0, 4},
    {0, 0, 1, 0}, {0, 0, 1, 1},  {0, 0, 1, 2},  {0, 0, 1, 3},  {0, 0, 2, 0},
    {0, 0, 2, 1}, {0, 0, 2, 2},  {0, 0, 3, 0},  {0, 0, 3, 1},  {0, 0, 4, 0},
    {0, 1, 0, 0}, {0, 1, 0, 1},  {0, 1, 0, 2},  {0, 1, 0, 3},  {0, 1, 1, 0},
    {0, 1, 1, 1}, {0, 1, 1, 2},  {0, 1, 2, 0},  {0, 1, 2, 1},  {0, 1, 3, 0},
    {0, 2, 0, 0}, {0, 2, 0, 1},  {0, 2, 0, 2},  {0, 2, 0, 3},  {0, 2, 1, 0},
    {0, 2, 1, 1}, {0, 2, 1, 2},  {0, 2, 2, 0},  {0, 2, 2, 1},  {0, 2, 3, 0},
    {0, 3, 0, 0}, {0, 3, 0, 1},  {0, 3, 0, 2},  {0, 3, 1, 0},  {0, 3, 1, 1},
    {0, 3, 2, 0}, {0, 4, 0, 0},  {0, 4, 0, 1},  {0, 4, 0, 2},  {0, 4, 1, 0},
    {0, 4, 1, 1}, {0, 4, 2, 0},  {0, 5, 0, 0},  {0, 5, 0, 1},  {0, 5, 1, 0},
    {0, 6, 0, 0}, {0, 6, 0, 1},  {0, 6, 1, 0},  {0, 7, 0, 0},  {0, 8, 0, 0},
    {1, 0, 0, 0}, {1, 0, 0, 1},  {1, 0, 0, 2},  {1, 0, 0, 3},  {1, 0, 1, 0},
    {1, 0, 1, 1}, {1, 0, 1, 2},  {1, 0, 2, 0},  {1, 0, 2, 1},  {1, 0, 3, 0},
    {1, 1, 0, 0}, {1, 1, 0, 1},  {1, 1, 0, 2},  {1, 1, 0, 3},  {1, 1, 1, 0},
    {1, 1, 1, 1}, {1, 1, 1, 2},  {1, 1, 2, 0},  {1, 1, 2, 1},  {1, 1, 3, 0},
    {1, 2, 0, 0}, {1, 2, 0, 1},  {1, 2, 0, 2},  {1, 2, 1, 0},  {1, 2, 1, 1},
    {1, 2, 2, 0}, {1, 3, 0, 0},  {1, 3, 0, 1},  {1, 3, 0, 2},  {1, 3, 1, 0},
    {1, 3, 1, 1}, {1, 3, 2, 0},  {1, 4, 0, 0},  {1, 4, 0, 1},  {1, 4, 1, 0},
    {1, 5, 0, 0}, {1, 5, 0, 1},  {1, 5, 1, 0},  {1, 6, 0, 0},  {1, 7, 0, 0},
    {2, 0, 0, 0}, {2, 0, 0, 1},  {2, 0, 0, 2},  {2, 0, 0, 3},  {2, 0, 1, 0},
    {2, 0, 1, 1}, {2, 0, 1, 2},  {2, 0, 2, 0},  {2, 0, 2, 1},  {2, 0, 3, 0},
    {2, 1, 0, 0}, {2, 1, 0, 1},  {2, 1, 0, 2},  {2, 1, 1, 0},  {2, 1, 1, 1},
    {2, 1, 2, 0}, {2, 2, 0, 0},  {2, 2, 0, 1},  {2, 2, 0, 2},  {2, 2, 1, 0},
    {2, 2, 1, 1}, {2, 2, 2, 0},  {2, 3, 0, 0},  {2, 3, 0, 1},  {2, 3, 1, 0},
    {2, 4, 0, 0}, {2, 4, 0, 1},  {2, 4, 1, 0},  {2, 5, 0, 0},  {2, 6, 0, 0},
    {3, 0, 0, 0}, {3, 0, 0, 1},  {3, 0, 0, 2},  {3, 0, 0, 3},  {3, 0, 1, 0},
    {3, 0, 1, 1}, {3, 0, 1, 2},  {3, 0, 2, 0},  {3, 0, 2, 1},  {3, 0, 3, 0},
    {3, 1, 0, 0}, {3, 1, 0, 1},  {3, 1, 0, 2},  {3, 1, 1, 0},  {3, 1, 1, 1},
    {3, 1, 2, 0}, {3, 2, 0, 0},  {3, 2, 0, 1},  {3, 2, 0, 2},  {3, 2, 1, 0},
    {3, 2, 1, 1}, {3, 2, 2, 0},  {3, 3, 0, 0},  {3, 3, 0, 1},  {3, 3, 1, 0},
    {3, 4, 0, 0}, {3, 4, 0, 1},  {3, 4, 1, 0},  {3, 5, 0, 0},  {3, 6, 0, 0},
    {4, 0, 0, 0}, {4, 0, 0, 1},  {4, 0, 0, 2},  {4, 0, 1, 0},  {4, 0, 1, 1},
    {4, 0, 2, 0}, {4, 1, 0, 0},  {4, 1, 0, 1},  {4, 1, 0, 2},  {4, 1, 1, 0},
    {4, 1, 1, 1}, {4, 1, 2, 0},  {4, 2, 0, 0},  {4, 2, 0, 1},  {4, 2, 1, 0},
    {4, 3, 0, 0}, {4, 3, 0, 1},  {4, 3, 1, 0},  {4, 4, 0, 0},  {4, 5, 0, 0},
    {5, 0, 0, 0}, {5, 0, 0, 1},  {5, 0, 0, 2},  {5, 0, 1, 0},  {5, 0, 1, 1},
    {5, 0, 2, 0}, {5, 1, 0, 0},  {5, 1, 0, 1},  {5, 1, 1, 0},  {5, 2, 0, 0},
    {5, 2, 0, 1}, {5, 2, 1, 0},  {5, 3, 0, 0},  {5, 4, 0, 0},  {6, 0, 0, 0},
    {6, 0, 0, 1}, {6, 0, 0, 2},  {6, 0, 1, 0},  {6, 0, 1, 1},  {6, 0, 2, 0},
    {6, 1, 0, 0}, {6, 1, 0, 1},  {6, 1, 1, 0},  {6, 2, 0, 0},  {6, 2, 0, 1},
    {6, 2, 1, 0}, {6, 3, 0, 0},  {6, 4, 0, 0},  {7, 0, 0, 0},  {7, 0, 0, 1},
    {7, 0, 1, 0}, {7, 1, 0, 0},  {7, 1, 0, 1},  {7, 1, 1, 0},  {7, 2, 0, 0},
    {7, 3, 0, 0}, {8, 0, 0, 0},  {8, 0, 0, 1},  {8, 0, 1, 0},  {8, 1, 0, 0},
    {8, 2, 0, 0}, {9, 0, 0, 0},  {9, 0, 0, 1},  {9, 0, 1, 0},  {9, 1, 0, 0},
    {9, 2, 0, 0}, {10, 0, 0, 0}, {10, 1, 0, 0}, {11, 0, 0, 0}, {12, 0, 0, 0}};

array<map<a4, array<int, 10>>, 18> computed;

int get_mul(int n) {
  int res = 1;
  while (n) {
    res *= n % 10;
    n /= 10;
  }
  return res;
}

int val(int n) {
  while (n >= 10)
    n = get_mul(n);
  return n;
}

array<map<int, int>, 17> a;
char sum[57][37][19][19];
int _a[18][57][37][19][19];
array<vector<pair<a4, int>>, 17> A;

int cnttttt = 0;

void add_to_a4(a4 &a, int n) {
  while (n % 2 == 0) {
    a[0]++;
    n /= 2;
  }
  while (n % 3 == 0) {
    a[1]++;
    n /= 3;
  }
  while (n % 5 == 0) {
    a[2]++;
    n /= 5;
  }
  while (n % 7 == 0) {
    a[3]++;
    n /= 7;
  }
}

void solve() {

  // cerr << ++cnttttt << '\n';
  // auto _a = a;
  // auto _sum = sum;
  string m;
  cin >> m;
  string old_m = m;
  int last = INT_MAX;
  for (int i = m.size() - 1; i > 0; i--) {
    if (m[i] == '0') {
      if (m[i - 1] != '0')
        m[i - 1]--;
      last = i;
    }
  }

  for (int i = m.size() - 1; i >= last; i--) {
    m[i] = '9';
  }

  vector<int> res(10);
  a4 curr_mul = {0, 0, 0, 0};

  for (int ind = 0; ind < m.size(); ind++) {
    if (m[ind] == '0')
      continue;
    int vl = m[ind] - '0';
    if (ind == m.size() - 1) {
      for (int j = 1; j <= vl; j++) {
        auto tmp = curr_mul;
        add_to_a4(tmp, j);
        res[sum[tmp[0]][tmp[1]][tmp[2]][tmp[3]]]++;
      }
      break;
    }

    for (int i = 1; i < vl; i++) {
      auto _cm = curr_mul;
      add_to_a4(_cm, i);
      if (to_precompute.count(_cm)) {
        auto mmmmm = computed[m.size() - 1 - ind][_cm];
        for (int i = 1; i < 10; i++) {
          res[i] += mmmmm[i];
        }
      } else {
        for (auto [key, value] : A[m.size() - 2 - ind]) {
          key[0] += curr_mul[0];
          key[1] += curr_mul[1];
          key[2] += curr_mul[2];
          key[3] += curr_mul[3];
          add_to_a4(key, i);
          res[sum[key[0]][key[1]][key[2]][key[3]]] += value;
        }
      }
    }

    if (vl != 0) {
      auto mmmmm = computed[m.size() - 1 - ind][{0, 0, 0, 0}];
      for (int i = 1; i < 10; i++)
        res[i] += mmmmm[i];
    }

    add_to_a4(curr_mul, vl);
  }

  res[0] = 0;
  for (int i = 0; i < m.size(); i++) {
    res[0] *= 10;
    res[0] += old_m[i] - '0';
  }

  for (int i = 1; i < 10; i++)
    res[0] -= res[i];

  for (auto r : res)
    cout << r << ' ';
  cout << '\n';
}

int num_of_perm(int n, int size) {
  // leniwy solve dodajacy O(log(n)) bo nie chce mi sie klepaaaac
  int num = factorials[size];
  for (int i = 1; i < n; i++) {
    int k = 0;
    while (n % 10 == i) {
      k++;
      n /= 10;
    }
    num /= factorials[k];
  }

  return num;
}

int arr_num_of_perm(array<int, 10> n) {
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    sum += n[i];
  }

  int res = factorials[sum];

  for (int i = 0; i < 10; i++) {
    res /= factorials[n[i]];
  }

  return res;
}

int arr_get_mul(array<int, 10> n) {
  int res = 1;
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < n[i]; j++)
      res *= i;
  }
  return res;
}

array<int, 4> fact(const array<int, 10> &n) {
  array<int, 4> res = {0, 0, 0, 0};
  res[0] += n[2] + 2 * n[4] + 3 * n[8] + n[6];
  res[1] += n[3] + n[6] + 2 * n[9];
  res[2] += n[5];
  res[3] += n[7];
  return res;
}
signed meow() {
  cin.tie((ostream *)!ios::sync_with_stdio(false));

  function<void(array<int, 10> &, int, int, int)> calc =
      [&](array<int, 10> &n, int last, int size, int max_size) {
        auto tmp = fact(n);
        _a[size - 1][tmp[0]][tmp[1]][tmp[2]][tmp[3]] += arr_num_of_perm(n);
        if (size != max_size) {
          for (int i = 1; i <= last; i++) {
            n[i]++;
            calc(n, i, size + 1, max_size);
            n[i]--;
          }
        }
      };

  array<int, 10> test;
  for (int i = 0; i < 10; i++)
    test[i] = 0;
  auto _ = test;
  _[1] = 1;
  calc(_, 1, 1, 17);
  _ = test;
  _[2] = 1;
  calc(_, 2, 1, 17);
  _ = test;
  _[3] = 1;
  calc(_, 3, 1, 17);
  _ = test;
  _[4] = 1;
  calc(_, 4, 1, 17);
  _ = test;
  _[5] = 1;
  calc(_, 5, 1, 17);
  _ = test;
  _[6] = 1;
  calc(_, 6, 1, 17);
  _ = test;
  _[7] = 1;
  calc(_, 7, 1, 17);
  _ = test;
  _[8] = 1;
  calc(_, 8, 1, 17);
  _ = test;
  _[9] = 1;
  calc(_, 9, 1, 17);

  for (int i = 0; i < 17; i++) {
    for (int _2 = 0, __2 = 1; _2 < 57; _2++, __2 *= 2)
      for (int _3 = 0, __3 = 1; _3 < 37; _3++, __3 *= 3)
        for (int _5 = 0, __5 = 1; _5 < 19; _5++, __5 *= 5)
          for (int _7 = 0, __7 = 1; _7 < 19; _7++, __7 *= 7) {
            if (_a[i][_2][_3][_5][_7] != 0) {
              A[i].push_back({{_2, _3, _5, _7}, _a[i][_2][_3][_5][_7]});
              sum[_2][_3][_5][_7] = val(__2 * __3 * __5 * __7);
            }
          }
  }

  for (int i = 1; i < 18; i++) {
    for (const auto &key : to_precompute) {
      computed[i][key] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
      for (auto [key2, value] : A[i-1]) {
        auto tmp = key2;
        tmp[0] += key[0];
        tmp[1] += key[1];
        tmp[2] += key[2];
        tmp[3] += key[3];

        if(tmp[0] > 55 || tmp[1] > 36 || tmp[2] > 18 || tmp[3] > 18) continue;
        computed[i][key][sum[tmp[0]][tmp[1]][tmp[2]][tmp[3]]] += value;
      }
    }
  }

  int n;
  cin >> n;
  while (n--)
    solve();
  return 0;
}