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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...) (void)0
#endif

namespace R = ranges;
namespace V = views;
auto ra(auto x, auto y) { return R::iota_view(x, y); }
auto rae(auto x, auto y) { return ra(x, y + 1); }
using i64 = int64_t;

vector<i64> all;
const int max_len = 18;
const int padic = 4;

struct kesz {
  static const int d2 = 3 * 18, d3 = 2 * 18, d5 = 1 * 18, d7 = 18;
  int t[d2 + 1][d3 + 1][d5 + 1][d7 + 1];
  kesz() {
    for (auto i : rae(0, d2)) {
      for (auto j : rae(0, d3)) {
        for (auto k : rae(0, d5)) {
          for (auto l : rae(0, d7)) {
            t[i][j][k][l] = -1;
          }
        }
      }
    }

    auto rek = [&](auto &&self, i64 mam, int len) -> void {
      if (len == max_len) {
        return;
      }
      int from = (len == 0 ? 1 : mam % 10);
      for (auto add : rae(from, 9)) {
        i64 new_mam = mam * 10 + add;
        all.push_back(new_mam);
        self(self, new_mam, len + 1);
      }
    };
    rek(rek, 0, 0);
    for (int i = 0; i < size(all); i += 1) {
      string s = to_string(all[i]);
      int any2 = 0, any5 = 0;
      for (auto c : s) {
        c -= '0';
        if (c % 2 == 0) {
          any2 = 1;
        }
        if (c % 5 == 0) {
          any5 = 1;
        }
      }
      if (any2 and any5) {
        swap(all[i], all.back());
        all.pop_back();
        i -= 1;
      }
    }
    debug(size(all));
  }
  bool count(array<int, 4> x) { return t[x[0]][x[1]][x[2]][x[3]] != -1; }
  int &operator[](array<int, 4> x) { return t[x[0]][x[1]][x[2]][x[3]]; }
};

kesz cache;

vector<int> digits{2, 3, 5, 7};
void solve() {
  i64 n;
  cin >> n;
  auto with0 = [&](i64 x) -> i64 {
    string s = to_string(x);
    vector dp(2, vector<i64>(2)); // dp[zero_already][smaller_already]
    dp[0][0] = 1;

    i64 res = 0;
    for (auto l : ra(0, ssize(s))) {
      vector new_dp(2, vector<i64>(2));
      for (auto was_zero : {0, 1}) {
        for (auto was_smaller : {0, 1}) {
          for (auto put : rae(0, 9)) {
            if (l == 0 and put == 0) {
              continue;
            }
            if (not was_smaller and put > s[l] - '0') {
              continue;
            }
            new_dp[was_zero or put == 0][was_smaller or put < s[l] - '0'] +=
                dp[was_zero][was_smaller];
          }
        }
      }
      if (l > 0) {
        for (auto put : rae(1, 9)) {
          new_dp[0][1] += 1;
        }
      }
      dp = new_dp;
    }
    res += dp[1][0] + dp[1][1];
    return res;
  };
  auto with25 = [&](i64 x) -> i64 {
    string s = to_string(x);
    vector dp(
        2, vector(2, vector<i64>(
                         2))); // dp[two_already][five_already][smaller_already]
    dp[0][0][0] = 1;
    i64 res = 0;
    for (auto l : ra(0, ssize(s))) {
      vector new_dp(2, vector(2, vector<i64>(2)));
      for (auto was_two : {0, 1}) {
        for (auto was_five : {0, 1}) {
          for (auto was_smaller : {0, 1}) {
            for (auto put : rae(1, 9)) {
              if (not was_smaller and put > s[l] - '0') {
                continue;
              }
              new_dp[was_two or put % 2 == 0][was_five or put % 5 == 0]
                    [was_smaller or put < s[l] - '0'] +=
                  dp[was_two][was_five][was_smaller];
            }
          }
        }
      }
      if (l > 0) {
        for (auto put : rae(1, 9)) {
          new_dp[put % 2 == 0][put % 5 == 0][1] += 1;
        }
      }
      dp = new_dp;
    }
    res += dp[1][1][0] + dp[1][1][1];
    return res;
  };

  auto repr = [&](i64 x) -> array<int, padic> {
    array<int, padic> cnt;
    R::fill(cnt, 0);

    while (x) {
      int d = x % 10;
      if (d == 0) {
        return {1, 0, 1, 0};
      }
      for (auto i : ra(0, padic)) {
        int ile = 0;
        int dziel = digits[i];
        while (d % dziel == 0) {
          d /= dziel;
          ile += 1;
        }
        cnt[i] += ile;
      }
      x /= 10;
    };
    return cnt;
  };
  for (auto i : rae(1, 9)) {
    cache[repr(i)] = i;
  }

  auto rek = [&](auto &&self, array<int, padic> cnt) -> int {
    int &res = cache[cnt];
    if (res != -1) {
      return res;
    }

    i64 new_n = (cnt[0] and cnt[2] ? 0 : 1);
    for (auto i : ra(0, padic)) {
      while (cnt[i]) {
        new_n *= digits[i];
        cnt[i] -= 1;
      }
    }

    if (new_n == 0) {
      return res = 0;
    }

    return res = self(self, repr(new_n));
  };

  auto skoncze = [&](i64 x) -> int { return rek(rek, repr(x)); };

  array<i64, 10> ans;
  R::fill(ans, 0);
  ans[0] = with0(n) + with25(n);

  // 2 3 7
  // 3 5 7

  auto with237 = [&](i64 x) {
    map<tuple<int, int, int, bool>, i64> dp;
    dp[{0, 0, 0, 0}] = 1;
    string s = to_string(x);
    for (auto l : ra(0, ssize(s))) {
      map<tuple<int, int, int, bool>, i64> new_dp;
      for (auto [a, v] : dp) {
        auto [two, three, seven, smaller] = a;
        for (auto put : rae(1, 9)) {
          if (put == 5) {
            continue;
          }
          if (not smaller and put > s[l] - '0') {
            continue;
          }
          int new_two = two, new_three = three, new_seven = seven;
          if (put == 2) {
            new_two += 1;
          } else if (put == 3) {
            new_three += 1;
          } else if (put == 4) {
            new_two += 2;
          } else if (put == 6) {
            new_two += 1;
            new_three += 1;
          } else if (put == 7) {
            new_seven += 1;
          } else if (put == 8) {
            new_two += 3;
          } else if (put == 9) {
            new_three += 2;
          }
          new_dp[{new_two, new_three, new_seven,
                  smaller or put < s[l] - '0'}] += v;
        }
      }
      if (l > 0) {
        for (auto put : rae(1, 9)) {
          if (put == 5) {
            continue;
          }
          int new_two = 0, new_three = 0, new_seven = 0;
          if (put == 2) {
            new_two += 1;
          } else if (put == 3) {
            new_three += 1;
          } else if (put == 4) {
            new_two += 2;
          } else if (put == 6) {
            new_two += 1;
            new_three += 1;
          } else if (put == 7) {
            new_seven += 1;
          } else if (put == 8) {
            new_two += 3;
          } else if (put == 9) {
            new_three += 2;
          }
          new_dp[{new_two, new_three, new_seven, 1}] += 1;
        }
      }
      dp = new_dp;
    }

    for (auto [a, v] : dp) {
      auto [two, three, seven, _] = a;
      if (two == 0) {
        continue;
      }
      auto end = rek(rek, {two, three, 0, seven});
      ans[end] += v;
    }
  };
  auto with357 = [&](i64 x) {
    map<tuple<int, int, int, bool>, i64> dp;
    dp[{0, 0, 0, 0}] = 1;
    string s = to_string(x);
    for (auto l : ra(0, ssize(s))) {
      map<tuple<int, int, int, bool>, i64> new_dp;
      for (auto [a, v] : dp) {
        auto [three, five, seven, smaller] = a;
        for (auto put : rae(1, 9)) {
          if (put % 2 == 0) {
            continue;
          }
          if (not smaller and put > s[l] - '0') {
            continue;
          }
          int new_three = three, new_five = five, new_seven = seven;
          if (put == 3) {
            new_three += 1;
          } else if (put == 5) {
            new_five += 1;
          } else if (put == 7) {
            new_seven += 1;
          } else if (put == 9) {
            new_three += 2;
          }
          new_dp[{new_three, new_five, new_seven,
                  smaller or put < s[l] - '0'}] += v;
        }
      }
      if (l > 0) {
        for (auto put : rae(1, 9)) {
          if (put % 2 == 0) {
            continue;
          }
          int new_three = 0, new_five = 0, new_seven = 0;
          if (put == 3) {
            new_three += 1;
          } else if (put == 5) {
            new_five += 1;
          } else if (put == 7) {
            new_seven += 1;
          } else if (put == 9) {
            new_three += 2;
          }
          new_dp[{new_three, new_five, new_seven, 1}] += 1;
        }
      }
      dp = new_dp;
    }
    for (auto [a, v] : dp) {
      auto [three, five, seven, _] = a;
      auto end = rek(rek, {0, three, five, seven});
      ans[end] += v;
    }
  };

  with237(n);
  with357(n);
  for (auto i : ra(0, 10)) {
    cout << ans[i] << " ";
  }
  cout << '\n';
}

int32_t main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int t = 1;
  cin >> t;
  for (auto i : ra(0, t)) {
    solve();
  }
}