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

typedef long long ll;

vector<ll> base = {1,7,49,343,5,35,1715,175,3,21,147,15,75,9,63,441,315,1575,77175,27,189,1323,135,3375,81,1361367,1121144263281,243,59535,729,2187,321489,137781,413343,177147,1162261467,2,14,98,686,33614,6,42,294,18,126,882,6174,43218,2117682,378,2646,162,1134,133413966,486,23814,1166886,71442,214326,13122,642978,3281116734,4,28,1372,67228,12,84,4116,1411788,36,1764,12348,324,2268,111132,972,8748,61236,236196,8,2744,941192,24,168,1176,72,216,648,1944,666792,122472,1417176,69441624,16,112,784,268912,48,144,49392,432,148176,3111696,27216,11664,34992,314928,46294416,32,224,184473632,672,32928,288,14112,691488,864,42336,18144,2667168,914838624,23328,163296,13226976,64,448,368947264,192,1344,1382976,1728,4148928,1778112,139968,2939328,3779136,3416267673274176,128,896,6272,131712,19361664,24192,93312,13716864,1792,1843968,112896,116169984,7112448,1229312,3687936,13824,96768,41472,124416,4444263936,9483264,1741824,4182119424,1792336896,14336,18432,1161216,1194891264,1492992,28672,23612624896,331776,8192,221184,13884223438848,11239424,33718272,71663616,3211264,9633792,393216,11329339392,9437184,1783627776,169869312,3699376128,226492416,128421199872,1438916737499136,2113929216,1811939328,913217421312,112717121716224,727326941773824};

class my_arr {
public:
	ll arr[20000];
	int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
	int off1, off2, off3, off4, off5, off6, off7, off8, off9, off10;
	int total = 0;
	my_arr(int _d1, int _d2, int _d3, int _d4, int _d5, int _d6, int _d7, int _d8, int _d9, int _d10) :
	d1(_d1), d2(_d2), d3(_d3), d4(_d4), d5(_d5), d6(_d6), d7(_d7), d8(_d8), d9(_d9), d10(_d10) {
		off1 = d2 * d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off2 = d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off3 = d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off4 = d5 * d6 * d7 * d8 * d9 * d10;
		off5 = d6 * d7 * d8 * d9 * d10;
		off6 = d7 * d8 * d9 * d10;
		off7 = d8 * d9 * d10;
		off8 = d9 * d10;
		off9 = d10;
		off10 = 1;
		total = d1 * d2 * d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		for (int i = 0; i < total; ++i) arr[i] = 0;
	}
	void reset(int _d1, int _d2, int _d3, int _d4, int _d5, int _d6, int _d7, int _d8, int _d9, int _d10) {
		d1 = _d1, d2 = _d2, d3 = _d3, d4 = _d4, d5 = _d5, d6 = _d6, d7 = _d7, d8 = _d8, d9 = _d9, d10 = _d10;
		off1 = d2 * d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off2 = d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off3 = d4 * d5 * d6 * d7 * d8 * d9 * d10;
		off4 = d5 * d6 * d7 * d8 * d9 * d10;
		off5 = d6 * d7 * d8 * d9 * d10;
		off6 = d7 * d8 * d9 * d10;
		off7 = d8 * d9 * d10;
		off8 = d9 * d10;
		off9 = d10;
		off10 = 1;
		total = d1 * d2 * d3 * d4 * d5 * d6 * d7 * d8 * d9 * d10;
		for (int i = 0; i < total; ++i) arr[i] = 0;
	}
	ll get(int _d1, int _d2, int _d3, int _d4, int _d5, int _d6, int _d7, int _d8, int _d9, int _d10) {
		return arr[_d1 * off1 + _d2 * off2 + _d3 * off3 + _d4 * off4 + _d5 * off5 + _d6 * off6 + _d7 * off7 + _d8 * off8 + _d9 * off9 + _d10 * off10];
	}
	ll& set(int _d1, int _d2, int _d3, int _d4, int _d5, int _d6, int _d7, int _d8, int _d9, int _d10) {
		return arr[_d1 * off1 + _d2 * off2 + _d3 * off3 + _d4 * off4 + _d5 * off5 + _d6 * off6 + _d7 * off7 + _d8 * off8 + _d9 * off9 + _d10 * off10];
	}
};

ll fact[20];

const int MAXN = 1e3 + 7;
pair<ll, int> arr[MAXN];

int rs[MAXN][10];
const int N = 1e8 + 7;
int8_t calc[N];

int main() {
	ios_base::sync_with_stdio(0);
	int t; cin >> t;
	for (int i = 0; i < t; ++i) {
		ll x; cin >> x;
		arr[i] = {x, i};
	}
	sort(arr, arr + t);
	if (arr[t - 1].first <= 1e8) {
		int idx = 0;
		vector<int> cnt(10);
		for (int i = 0; i < t; ++i) {
			while (idx < arr[i].first) {
				++idx;
				int cp = idx;
				if (cp >= 10) {
					int a = cp;
					int x = 1;
					while (a) {
						x *= (a % 10);
						a /= 10;
					}
					cp = calc[x];
				}
				++cnt[cp];
				calc[idx] = cp;
			}
			for (int j = 0; j < 10; ++j) rs[arr[i].second][j] = cnt[j];
		}
		for (int i = 0; i < t; ++i) {
			for (int j = 0; j < 10; ++j) cout << rs[i][j] << " ";
			cout << "\n";
		}
		return 0;
	}
	// cool solution, although too slow for 10 ;<
	fact[0] = 1;
	for (int i = 1; i <= 18; ++i) fact[i] = fact[i - 1] * i;
	// generate patterns
	set<vector<int> > patterns;
	for (ll &v : base) {
		ll cp = v;
		int cnt2 = 0, cnt3 = 0, cnt5 = 0, cnt7 = 0;
		while (cp % 2 == 0) {
			cp /= 2;
			++cnt2;
		}
		while (cp % 3 == 0) {
			cp /= 3;
			++cnt3;
		}
		while (cp % 5 == 0) {
			cp /= 5;
			++cnt5;
		}
		while (cp % 7 == 0) {
			cp /= 7;
			++cnt7;
		}
		for (int i = cnt5 + cnt7; i <= 18; ++i) {
			for (int i4 = 0; i4 <= cnt2 / 2; ++i4) {
				for (int i8 = 0; i8 <= (cnt2 - i4 * 2) / 3; ++i8) {
					for (int i9 = 0; i9 <= cnt3 / 2; ++i9) {
						for (int i6 = 0; i6 <= min(cnt2 - i4 * 2 - i8 * 3, cnt3 - i9 * 2); ++i6) {
							if (!i) continue;
							vector<int> res(9);
							res[3] = i4;
							res[7] = i8;
							res[8] = i9;
							res[5] = i6;
							res[1] = cnt2 - i4 * 2 - i8 * 3 - i6;
							res[2] = cnt3 - i9 * 2 - i6;
							res[4] = cnt5;
							res[6] = cnt7;
							int total = 0;
							for (int c = 1; c < 9; ++c) total += res[c];
							if (total > i) continue;
							res[0] = i - total;
							patterns.insert(res);
						}
					}
				}
			}
		}
	}
	for (int tt = 0; tt < t; ++tt) {
		ll n = arr[tt].first;
		string val = to_string(n);
		val = "#" + val;
		int sz = val.size() - 1;
		vector<ll> cnt(10);
		my_arr dp = my_arr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		my_arr dp2 = my_arr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		for (auto p : patterns) {
			int len = 0;
			for (int &v : p) len += v;
			if (len > sz) {
				// none of the values are good
				continue;
			}
			if (len < sz) {
				// all of the values are good
				ll ans = fact[len];
				for (int &v : p) ans /= fact[v];
				ll prod = 1;
				for (int i = 0; i < 9; ++i) {
					for (int j = 0; j < p[i]; ++j) prod *= (i + 1);
				}
				while (prod >= 10) {
					ll a = prod;
					ll x = 1;
					while (a) {
						x *= (a % 10);
						a /= 10;
					}
					prod = x;
				}
				cnt[prod] += ans;
				continue;
			}
			// digit dp over 'len' digits long integer
			dp.reset(2, p[0] + 1, p[1] + 1, p[2] + 1, p[3] + 1, p[4] + 1, p[5] + 1, p[6] + 1, p[7] + 1, p[8] + 1);
			dp2.reset(2, p[0] + 1, p[1] + 1, p[2] + 1, p[3] + 1, p[4] + 1, p[5] + 1, p[6] + 1, p[7] + 1, p[8] + 1);
			dp2.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) = 1;
			for (int i = 1; i <= len; ++i) {
				for (bool b : {0, 1}) {
					for (int digit = 1; digit <= (!b ? val[i] - '0' : 9); ++digit) {
						bool is_smaller = b | (digit < (val[i] - '0'));
						for (int i1 = 0; i1 <= p[0] - (digit == 1); ++i1) {
							for (int i2 = 0; i2 <= p[1] - (digit == 2); ++i2) {
								for (int i3 = 0; i3 <= p[2] - (digit == 3); ++i3) {
									for (int i4 = 0; i4 <= p[3] - (digit == 4); ++i4) {
										for (int i5 = 0; i5 <= p[4] - (digit == 5); ++i5) {
											for (int i6 = 0; i6 <= p[5] - (digit == 6); ++i6) {
												for (int i7 = 0; i7 <= p[6] - (digit == 7); ++i7) {
													for (int i8 = 0; i8 <= p[7] - (digit == 8); ++i8) {
														for (int i9 = 0; i9 <= p[8] - (digit == 9); ++i9) {
															dp.set(is_smaller, i1 + (digit == 1), i2 + (digit == 2), i3 + (digit == 3), i4 + (digit == 4), i5 + (digit == 5), i6 + (digit == 6), i7 + (digit == 7), i8 + (digit == 8), i9 + (digit == 9)) +=
															dp2.get(b, i1, i2, i3, i4, i5, i6, i7, i8, i9);
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
				for (int _i = 0; _i < dp2.total; ++_i) dp2.arr[_i] = dp.arr[_i];
				for (int _i = 0; _i < dp2.total; ++_i) dp.arr[_i] = 0;
			}
			// update cnt
			ll prod = 1;
			for (int i = 0; i < 9; ++i) {
				for (int j = 0; j < p[i]; ++j) prod *= (i + 1);
			}
			while (prod >= 10) {
				ll a = prod;
				ll x = 1;
				while (a) {
					x *= (a % 10);
					a /= 10;
				}
				prod = x;
			}
			for (bool b : {0, 1}) {
				for (int i1 = 0; i1 <= p[0]; ++i1) {
					for (int i2 = 0; i2 <= p[1]; ++i2) {
						for (int i3 = 0; i3 <= p[2]; ++i3) {
							for (int i4 = 0; i4 <= p[3]; ++i4) {
								for (int i5 = 0; i5 <= p[4]; ++i5) {
									for (int i6 = 0; i6 <= p[5]; ++i6) {
										for (int i7 = 0; i7 <= p[6]; ++i7) {
											for (int i8 = 0; i8 <= p[7]; ++i8) {
												for (int i9 = 0; i9 <= p[8]; ++i9) {
													cnt[prod] += dp2.get(b, i1, i2, i3, i4, i5, i6, i7, i8, i9);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		// loop through all the bases <= n and kick them out from cnt if they're reachable from patterns
		for (ll &v : base) {
			if (v > n) continue;
			bool found = 0;
			vector<int> vpatt(9);
			string s = to_string(v);
			for (char &c : s) ++vpatt[c - '0' - 1];
			for (auto p : patterns) {
				bool ok = 1;
				for (int i = 0; i < 9; ++i) {
					if (p[i] != vpatt[i]) ok = 0;
				}
				if (ok) {
					found = 1;
					break;
				}
			}
			if (!found) {
				ll cp = v;
				while (cp >= 10) {
					ll a = cp;
					ll x = 1;
					while (a) {
						x *= (a % 10);
						a /= 10;
					}
					cp = x;
				}
				++cnt[cp];
			}
		}
		cnt[0] = n;
		for (int i = 1; i < 10; ++i) cnt[0] -= cnt[i];
		for (ll &v : cnt) cout << v << " ";
		cout << "\n";
	}
}