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

#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
#ifdef DEBUG
auto &operator << (auto &o, pair<auto, auto> p) {
	return o << "(" << p.first << ", " << p.second << ")";
}
auto operator << (auto &o, auto x) -> decltype(x.end(), o) {
	o << "{"; int i = 0;
	for (auto e : x) o << ","+!i++ << e;
	return o << "}";
}
#define debug(X...) cerr << "["#X"]: ", [](auto ...$) {((cerr << $ << "; "), ...) << endl;}(X)
#else
#define debug(...) {}
#endif

constexpr int LEN = 18;
constexpr long long INF = (long long)3e18 + 100;

constexpr int P2 = 3 * LEN + 1;
constexpr int P3 = 2 * LEN + 1;
constexpr int P5 = LEN + 1;
constexpr int P7 = LEN + 1;
constexpr int ALL_STATES = 904810;
constexpr int BASE = 64;
constexpr int MAX_STATE = BASE * BASE * BASE * (P7 + 1);
constexpr int ZERO = 
	P2 + 
	(P3 - 1) * BASE + 
	(P5 - 1) * BASE * BASE +
	(P7 - 1) * BASE * BASE * BASE;

static array<int, 4> primes = {2, 3, 5, 7};

long long get_digit_product(long long n) {
	long long ret = 1;
	while (n > 0) {
		int digit = n % 10;
		n /= 10;
		ret *= digit;
	}

	return ret;
}

int state_from_array(const array<int, 4> &state) {
	int ret = 0;
	int mult = 1;
	REP (i, 4) {
		int e = state[i];
		assert(e < BASE);
		ret += mult * e;
		mult *= BASE;
	}

	return ret;
}

int convert_to_state(long long value) {
	static array<int, 10> base_states = {
		ZERO,
		0,
		1,
		BASE * 1,
		2,
		BASE * BASE * 1,
		BASE * 1 + 1,
		BASE * BASE * BASE * 1,
		3,
		BASE * 2
	};

	if (value < 10)
		return base_states[value];

	int state = 0;
	int mult = 1;
	REP (i, 4) {
		int p = primes[i];
		int e = 0;
		while (value % p == 0) {
			++e;
			value /= p;
		}

		assert(e < BASE);
		state += mult * e;
		mult *= BASE;
	}

	return state;
}

int get_next_state(int state) {
	if (state == ZERO) return state;

	long long value = 1;
	REP (i, 4) {
		int state_i = state % BASE;
		state /= BASE;

		long long take = (long long)pow(primes[i], state_i);
		if (take > INF / value)
			return ZERO;
		value *= take;
	}

	long long next_value = get_digit_product(value);
	return convert_to_state(next_value);
}

short final_digit[MAX_STATE + 1];
vector<int> all_states;
array<int, MAX_STATE + 5> state_id = {};
long long dp[LEN + 1][2][ALL_STATES];
array<long long, 10> starting_dp[LEN + 1];

short dfs(int state) {
	if (final_digit[state] != -1)
		return final_digit[state];

	int next_state = get_next_state(state);
	return final_digit[state] = dfs(next_state);
}

void preprocessing() {
	REP (i, MAX_STATE + 5)
		state_id[i] = -1;

	REP (state, MAX_STATE + 1) {
		final_digit[state] = -1;
	}

	REP (digit, 10) {
		int state = convert_to_state(digit);
		final_digit[state] = digit;
	}

	REP (p2, P2) REP (p3, P3) REP (p5, P5) REP (p7, P7) {
		int state = state_from_array({p2, p3, p5, p7});
		final_digit[state] = dfs(state);
	}

	all_states.emplace_back(ZERO);
	REP (p2, P2) REP (p3, P3) REP (p5, P5) REP (p7, P7) {
		int state = state_from_array({p2, p3, p5, p7});
		state_id[state] = ssize(all_states);
		all_states.emplace_back(state);
	}

	REP (i, LEN + 1) REP (j, 2) REP (k, ssize(all_states))
		dp[i][j][k] = -1;

	vector<array<int, 3>> visited = {};
	deque<array<int, 3>> Q = {};

	FOR (digit, 1, 9) {
		int state = convert_to_state(digit);
		int id = state_id[state];

		dp[0][0][id] = 1;
		Q.push_back({0, 0, id});
	}

	array<long long, 10> answer = {};
	int current_length = 0;
	while (!Q.empty()) {
		auto [len, smaller, id] = Q.front();
		visited.push_back({len, smaller, id});
		Q.pop_front();

		if (len > current_length) {
			starting_dp[len] = answer;
			current_length = len;
		}

		if (len == LEN)
			continue;

		int current_state = all_states[id];
		if (current_state == ZERO) continue;

		long long d = dp[len][0][id];

		int my_final_digit = final_digit[current_state];
		answer[my_final_digit] += d;

		FOR (digit, 1, 9) {
			int new_state = current_state + convert_to_state(digit);
			int new_id = state_id[new_state];

			if (new_id == -1)
				continue;

			if (dp[len + 1][0][new_id] == -1) {
				dp[len + 1][0][new_id] = 0;
				Q.push_back({len + 1, 0, new_id});
			}

			dp[len + 1][0][new_id] += d;
		}
	}

	starting_dp[LEN] = answer;

#ifdef DEBUG
	REP (len, LEN + 1)
		debug(len, starting_dp[len]);
#endif

	debug(ssize(visited));
	for (auto [i, j, k] : visited)
		dp[i][j][k] = -1;
}

vector<int> get_digits(long long n) {
	vector<int> ret = {};
	while (n) {
		int digit = n % 10;
		n /= 10;
		ret.emplace_back(digit);
	}
	reverse(ret.begin(), ret.end());
	return ret;
}

void solve(long long n) {
	vector<array<int, 3>> visited = {};
	deque<array<int, 3>> Q = {};

	++n;
	vector<int> n_digits = get_digits(n);
	debug(n_digits);

	array<long long, 10> answer = starting_dp[ssize(n_digits) - 1];

	FOR (digit, 1, n_digits[0]) {
		int state = convert_to_state(digit);
		int id = state_id[state];
		int smaller = (digit < n_digits[0]);

		dp[0][smaller][id] = 1;
		Q.push_back({0, smaller, id});
	}

	while (!Q.empty()) {
		auto [len, smaller, id] = Q.front();
		visited.push_back({len, smaller, id});
		Q.pop_front();

		int current_state = all_states[id];
		if (current_state == ZERO) continue;
		long long d = dp[len][smaller][id];

		if (len == ssize(n_digits) - 1) {
			if (smaller == 1) {
				int digit = final_digit[all_states[id]];
				answer[digit] += dp[len][smaller][id];
			}
			continue;
		}

		int limit = (smaller ? 9 : n_digits[len + 1]);
		FOR (digit, 1, limit) {
			int new_state = current_state + convert_to_state(digit);
			int new_id = state_id[new_state];
			if (new_id == 0) continue;

			int new_smaller = (smaller or digit < n_digits[len + 1]);

			if (dp[len + 1][new_smaller][new_id] == -1) {
				dp[len + 1][new_smaller][new_id] = 0;
				Q.push_back({len + 1, new_smaller, new_id});
			}
			dp[len + 1][new_smaller][new_id] += d;
		}
	}

	debug(ssize(visited));
	for (auto [i, j, k] : visited)
		dp[i][j][k] = -1;

	long long sum = 0;
	FOR (i, 1, 9) sum += answer[i];
	answer[0] = n - 1 - sum;

	REP (i, 10)
		cout << answer[i] << ' ';
	cout << '\n';
}

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

	preprocessing();
	debug(ssize(all_states));

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

}