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
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
typedef long long ll;
#define vc vector
#define st first
#define nd second
#define pll pair<ll, ll>
#define sz(a) (ll)a.size()
#define all(a) a.begin(), a.end()
#define pu push
#define pub push_back
#define pob pop_back
#define em emplace
#define emb emplace_back
const ll M = 1e9;

ll f(ll x) {
	ll ret = 1;
	while (x > 0) {
		ret *= x % 10;
		x /= 10;
	}
	return ret;
}

void getDigits(ll x, vc<ll> &d) {
	d.clear();
	while (x > 0) {
		d.pub(x % 10);
		x /= 10;
	}
}

bitset<M> dvb;
unordered_set<ll> ss, dv;
vc<ll> fct;
vc<vc<pll>> res;

bool isdiv(ll x) {
	if (x < M)
		return dvb[x];
	return dv.find(x) != dv.end();
}

ll g(ll x) {
	if (not isdiv(x))
		return 0;
	while (x >= 10)
		x = f(x);
	return x;
}

ll gg(ll x) {
	while (x >= 10)
		x = f(x);
	return x;
}

void rec(ll cur = 1, ll k = 0, ll mlt = 1) {
	if (gg(mlt) != 0)
		ss.insert(mlt);
	if (cur > 9 or k >= 18)
		return;
	for (ll i = 0; k + i <= 18; i++) {
		rec(cur + 1, k + i, mlt);
		mlt *= cur;
	}
}

void rec2(ll L, ll cur = 1, ll k = 0, ll mlt = 1, ll cmb = 1) {
	if (not isdiv(mlt))
		return;
	if (cur == 10) {
		res[k].pub({mlt, cmb});
		return;
	} else if (cur == 10)
		return;
	for (ll i = 0; k + i <= L; i++) {
		rec2(L, cur + 1, k + i, mlt, cmb * (fct[k + i] / fct[k] / fct[i]));
		mlt *= cur;
	}
}

void preprocess() {
	rec();
	for (ll x : ss) {
		for (ll i = 1; i * i <= x; i++)
			if (x % i == 0) {
				if (i < M)
					dvb[i] = true;
				else
					dv.insert(i);
				if (x / i < M)
					dvb[x / i] = true;
				else
					dv.insert(x / i);
			}
	}

	ll C = 19;
	fct.resize(C + 1);
	fct[0] = fct[1] = 1;
	for (ll i = 2; i <= C; i++)
		fct[i] = fct[i - 1] * i;
	res.resize(C);
	rec2(17);
	for (ll i = 0; i < 18; i++) {
		sort(all(res[i]));
		vc<pll> tmp;
		for (auto &[x, cmb] : res[i]) {
			if (not isdiv(x))
				continue;
			if (not tmp.empty() and tmp.back().st == x)
				tmp.back().nd += cmb;
			else
				tmp.pub({x, cmb});
		}
		swap(tmp, res[i]);
	}
}

void get(ll L, ll s, vc<ll> &target) {
	target.assign(10, 0);
	if (not isdiv(s))
		return;
	for (auto &[x, cmb] : res[L])
		target[g(s * x)] += cmb;
}

void program() {
	ll n;
	cin >> n;
	vc<ll> d;
	getDigits(n, d);
	ll k = sz(d);
	vc<ll> ans(10, 0);
	vc<ll> a;
	for (ll i = 1; i < k; i++) {
		get(i, 1, a);
		for (ll j = 0; j < 10; j++)
			ans[j] += a[j];
	}
	ll mlt = 1;
	for (ll i = k - 1; i >= 0; i--) {
		if (d[i] == 0)
			break;
		for (ll j = 1; j < d[i]; j++) {
			get(i, mlt * j, a);
			for (ll l = 0; l < 10; l++)
				ans[l] += a[l];
		}
		mlt *= d[i];
	}
	ans[gg(n)]++;
	ans[0] = n;
	for (ll i = 1; i < 10; i++)
		ans[0] -= ans[i];
	for (ll i = 0; i < 10; i++)
		cout << ans[i] << " ";
	cout << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	preprocess();
	ll T;
	cin >> T;
	while (T--)
		program();
	return 0;
}