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
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
//#pragma clang diagnostic ignored "-Wunused-const-variable"
#include<vector>
#include<iostream>
#include<stack>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<tuple>
#include<bitset>
#include<queue>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<assert.h>
#include<ctime>
#include <cassert>
#include <thread>
#include <mutex>
#include <semaphore>
#include <chrono>
//#define int long long
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
ll gcd(ll i, ll j) {
	if (i < j)swap(i, j);
	if (j == 0)return i;
	else return gcd(j, i % j);
}
#ifdef _MY
ll __builtin_popcount(ll x) { return x ? (__builtin_popcount(x >> 1) + (x & 1)) : 0; }
#endif
template<typename T> inline T getint() {
	T val = 0;
	char c;

	bool neg = false;
	while ((c = getchar()) && !(c >= '0' && c <= '9')) {
		neg |= c == '-';
	}

	do {
		val = (val * 10) + c - '0';
	} while ((c = getchar()) && (c >= '0' && c <= '9'));

	return val * (neg ? -1 : 1);
}
const ll INF = 1e7 + 100;
const int mod = 998244353, mod1 = 1000000007;
const ld eps = 1e-7, pi = acos(-1);
const int maxT = 503, maxN = 5'010, A = 311;
ll bp(ll et, ll b) {
	ll res = 1;
	for (int i = 30; i >= 0; --i) {
		res = (res * res) % mod;
		if ((b & (1 << i)) != 0)res = (res * et) % mod;
	}
	return res;
}
mt19937 mt_rand(time(0));
void panic() {
	cout << "No\n";
	exit(0);
}
ld get_time() {
	ll tmr = clock();
	return (ld)tmr / CLOCKS_PER_SEC;
}
int pl(int a, int b) {
	a += b;
	if (a >= mod)
		a -= mod;
	return a;
}
int mul(ll a, ll b) {
	return a * b % mod;
}

ll simple_solve_chi(ll x) {
	while (x >= 10) {
		ll y = 1;
		while (x) {
			y *= x % 10;
			x /= 10;
		}
		x = y;
	}
	return x;
	/*while (true) {
		string ss = to_string(x);
		ll rnew = 1;
		for (auto x : ss) {
			rnew *= x - '0';
		}
		if (rnew == x) {
			return rnew;
		}
		x = rnew;
	}*/
}
vector<ll> simple_solve(ll n) {
	vector<ll>a(10);
	if (n > 1e7) {
		return a;
	}
	for (ll i = 1; i <= n; ++i) {
		ll res = simple_solve_chi(i);
		a[res]++;
		/*	if (res == 5) {
				cout << i << " " << res << endl;
			}*/
	}
	//for (auto x : a) {
	//	cout << x << " ";
	//}
	return a;
}
ll PW[30];
ll c_cnt[30][30];
void init() {
	PW[0] = 1;
	for (int i = 1; i < 30; ++i) {
		PW[i] = PW[i - 1] * 10;
	}
	for (int i = 0; i < 30; ++i)c_cnt[0][i] = c_cnt[i][0] = 1;
	for (int i = 1; i < 30; ++i) {
		for (int j = 1; j < 30; ++j) {
			c_cnt[i][j] = c_cnt[i - 1][j] + c_cnt[i][j - 1];
		}
	}
}

ll C(int n, int k) {
	return c_cnt[n - k][k];
}
ll super_calc(int all, const vector<int>& cnt_by_chi) {
	ll res = 1;
	for (int i = 1; i < 10; ++i) {
		int v = cnt_by_chi[i];
		res *= C(all, v);
		all -= v;
	}
	return res;
}
ll calculate_how_much_i_can_rotate(const string& n_, int all, const ll n, const vector<int>& cnt_by_chi) {
	if (all < n_.size()) {
		ll res = 1;
		for (int i = 1; i < 10; ++i) {
			int v = cnt_by_chi[i];
			res *= C(all, v);
			all -= v;
		}
		return res;
	}
	return 0;
}
void recurce1(int all, vector<int>& cnt_by_chi, int can_strt_from, const vector<pair<ll, string>>& ns,
	vector<vector<ll>>& ans, const ll curr_ways) {
	if (cnt_by_chi[2] && cnt_by_chi[5]) {
		return;
	}
	if (all != 0) {
		ll x = 1;
		for (int i = 1; i < 10; ++i) {
			for (int j = 0; j < cnt_by_chi[i]; ++j) {
				x *= i;
			}
		}
		int res = simple_solve_chi(x);
		if (res != 0) {
			for (int id = 0; id < ns.size(); ++id) {
				if (all < ns[id].second.size()) {
					ans[id][res] += curr_ways;
				}
				else if (all == ns[id].second.size()) {
					ll tmpres = curr_ways;
					auto cnt_by_chi2 = cnt_by_chi;
					int tmpall = all;
					ll tmpres2 = curr_ways;
					for (int i = 0; i < ns[id].second.size(); ++i) {
						int v = ns[id].second[i] - '0';
						for (int j = v + 1; j <= 9; ++j) {
							if (cnt_by_chi2[j]) {
								tmpres -=
									tmpres2 / C(tmpall, cnt_by_chi2[j])
									* C(tmpall - 1, cnt_by_chi2[j] - 1);
							}
						}
						if (cnt_by_chi2[v] == 0) {
							break;
						}
						tmpres2 = tmpres2 / C(tmpall, cnt_by_chi2[v])
							* C(tmpall - 1, cnt_by_chi2[v] - 1);
						--tmpall;
						--cnt_by_chi2[v];
					}
					ans[id][res] += tmpres;
				}
			}
		}
	}
	for (int i = can_strt_from; i <= 9; ++i) {
		for (int j = 1; j + all <= 18; ++j) {
			cnt_by_chi[i] += j;
			recurce1(all + j, cnt_by_chi, i + 1, ns, ans,
				curr_ways * C(all + j, j));
			cnt_by_chi[i] -= j;
		}
	}
}
vector<vector<ll>> smart_solve(vector<ll> ns) {
	vector<pair<ll, string>>ns1;
	for (auto n : ns) {
		ns1.emplace_back(n, to_string(n));
	}
	vector<vector<ll>>a(ns.size(), vector<ll>(10));
	vector<int>cnt_chi(10);
	recurce1(0, cnt_chi, 1, ns1, a, 1);
	for (int id = 0; id < ns.size(); ++id) {
		a[id][0] = ns[id];
		for (int i = 1; i < 10; ++i) {
			a[id][0] -= a[id][i];
		}
	}
	return a;
}
void solve(vector<ll> ns) {
	//vector<ll>a = simple_solve(n);
	auto b = smart_solve(ns);
	for (auto x : b) {
		for (auto y : x)cout << y << " "; cout << "\n";
		//cout << x << " ";
	}
	//cout << "\n";
	/*for (int i = 0; i < 10; ++i) {
		if (a[i] == b[i]) {
			cout << i << " is OK: " << a[i] << " " << b[i] << "\n";
		}
		else {
			cout << i << " has ERROR: " << a[i] << " " << b[i] << "\n";
		}
	}*/
}

/*bool f(const vector<string>& a, double r) {
	const int n = a.size();
	const int l = a[0].size();

}
void solve() {
	int n, l;
	cin >> n >> l;
	vector<string>a(n);
	for (auto& s : a) {
		cin >> s;
	}
	for (int i = 0; i < l; ++i) {
		int c = 0;
		for (int j = 0; j < n; ++j) {
			c += a[j][i] == '.';
		}
		if (c == 0) {
			cout << "-1\n";
			return;
		}
	}
	//bool has_ans = false;
	vector<pair<double, string>>anss;
	anss.push_back({ 0, "0/1" });
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= l; ++j) {
			int g = gcd(i, j);
			if (g != 1) {
				continue;
			}
			double terr = (double)j / i;
			anss.push_back({ terr, to_string(j) + "/" + to_string(i) });
		}
	}
	sort(anss.begin(), anss.end());
	int b = 0, e = anss.size();
	while (b + 1 != e) {
		int m = (b + e) / 2;
		if (f(a, anss[m].first)) {
			b = m;
		}
		else {
			e = m;
		}
	}
	cout << anss[b].second << "\n";

}*/
int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(14);
#ifdef _MY
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);


	/*time_t start, end;
	time(&start);*/
#else
	//freopen("epic.in", "r", stdin); freopen("epic.out", "w", stdout);
#endif
	init();
	int t = 1;
	cin >> t;
	vector<ll>ns;
	while (t--) {
		ll n;
		cin >> n;
		ns.push_back(n);
	}
	//ns.assign(1000, 5e17);
	solve(ns);
#ifdef _MY
	cout << "Time taken is : " << fixed << get_time() << " sec " << "\n";
#endif
	return 0;
}