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
#pragma GCC optimize("O2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
bool Mbg;
using namespace std;
#define vec vector
#define pb push_back
#define eb emplace_back
#define siz(vec) (int)((vec).size())
#define all(vec) (vec).begin(),(vec).end()
template<class T>
void operator +=(vec<T> &lhs,const T &rhs){lhs.push_back(rhs);}
#define pii pair<int,int>
#define x first
#define y second
#define mp make_pair
#define exc(expr) if(expr)continue;
#define stop(expr) if(expr)break;
#define ret(expr) if(expr)return;
#define ins insert
#define era erase
#define lb lower_bound
#define ub upper_bound
#define deb(var) cerr<<#var<<'='<<(var)<<"; "
#define debl(var) cerr<<#var<<'='<<(var)<<";\n"
#define int long long
#define inf (long long)(1e18)
template<class T>
bool Min(T &x,const T &y){return x>y?x=y,1:0;}
template<class T>
bool Max(T &x,const T &y){return x<y?x=y,1:0;}
const int mod=998244353;
void Add(int &x,const int &y){x=x+y<mod?x+y:x+y-mod;}
void Dec(int &x,const int &y){x=x>=y?x-y:x-y+mod;}
int fpm(int x,int y){
    int ans=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ans=1ll*x*ans%mod;return ans;
}
#define pow MyPow
int MyPow(int x,int y){
    int ans=1;for(;y;y>>=1,x*=x)if(y&1)ans*=x;return ans;
}

#define LIM 19

#define SIZ 10000
char tab[SIZ+10];
signed mty[SIZ+10];

map<int,int> rec;
char calc(int x){
    int N=x;
    if(rec.find(N)!=rec.end()){
        return rec[N];
    }
    while(x>SIZ){
        int y=1;
        while(x){
            y*=x%10;
            x/=10;
        }
        x=y;
    }
    return rec[N]=tab[x];
}

char G[60][40][20][20];

int c[10],a[10],fac[20];

int f[60][40][20][20][20];
vec<array<int,5> > g[20];

void dfs(int i,int s){
    if(i>=10){
        int ans=fac[s];
        for(int i=1;i<=9;i++){
            ans/=fac[a[i]];
        }
        f[c[2]][c[3]][c[5]][c[7]][s]+=ans;return;
    }
    int tmp[10];
    memcpy(tmp,c,sizeof c);
    for(;s<LIM;s++,a[i]++){
        dfs(i+1,s);

        if(i==2||i==3||i==5||i==7){
            ++c[i];
        }else if(i==4){
            c[2]+=2;
        }else if(i==6){
            ++c[2];
            ++c[3];
        }else if(i==8){
            c[2]+=3;
        }else if(i==9){
            c[3]+=2;
        }
    }
    a[i]=0;
    memcpy(c,tmp,sizeof c);
}
int pow2[60],pow3[40],pow5[20],pow7[20],pow9[20];
void init(){
    fac[0]=1;
    for(int i=1;i<20;i++){
        fac[i]=fac[i-1]*i;
    }
    dfs(1,0);

    pow2[0]=pow3[0]=pow5[0]=pow7[0]=pow9[0]=1;
    for(int i=1;i<60;i++){
        pow2[i]=pow2[i-1]*2;
    }
    for(int i=1;i<40;i++){
        pow3[i]=pow3[i-1]*3;
    }
    for(int i=1;i<20;i++){
        pow5[i]=pow5[i-1]*5;
        pow7[i]=pow7[i-1]*7;
        pow9[i]=pow9[i-1]*9;
    }
    for(int i=0;i<=SIZ;i++){
        if(i<=9){
            tab[i]=i;
            mty[i]=i;
        }else{
            // int y=1,x=i;
            // while(x){
                // y*=x%10;
                // x/=10;
            // }
            tab[i]=tab[mty[i]=mty[i/10]*(i%10)];
        }
    }
    for(int i=0;i<60;i++){
        for(int j=0;j<40;j++){
            for(int k=0;k<20;k++){
                for(int l=0;l<20;l++){
                    for(int N=0;N<20;N++){
                        if(f[i][j][k][l][N]){
                            g[N].pb({i,j,k,l,f[i][j][k][l][N]});
                        }
                    }
                    if(f[i][j][k][l][18]){
                        G[i][j][k][l]=calc(pow2[i]*pow3[j]*pow5[k]*pow7[l]);
                    }
                }
            }
        }
        // deb(N),debl(siz(g[N]));
    }

}

int n,ans[10];

int F[20][60][40][20][20][10];
bool Bk[20][60][40][20][20];
void Calc(int N,int M,int p2,int p3,int p5,int p7){
    if(p2&&p5){
        ans[0]+=pow9[N];return;
    }
    if(Bk[N][p2][p3][p5][p7]){
        for(int i=0;i<10;i++){
            ans[i]+=F[N][p2][p3][p5][p7][i];
        }
    }else{
        Bk[N][p2][p3][p5][p7]=1;
        for(auto [q2,q3,q5,q7,y]:g[N]){
            F[N][p2][p3][p5][p7][G[p2+q2][p3+q3][p5+q5][p7+q7]]+=y;
        }
        for(int i=0;i<10;i++){
            ans[i]+=F[N][p2][p3][p5][p7][i];
        }
    }
}
int len,bit[20];
void Dfs(int i,int mul,int p2,int p3,int p5,int p7){
    if(i>len){
        return;
    }
    for(int j=1;j<bit[i];j++){
        Calc(len-i,mul*j,p2+(j%2==0)+(j%4==0)+(j==8),p3+(j%3==0)+(j==9),p5+(j==5),p7+(j==7));
    }
    if(bit[i]){
        int j=bit[i];
        Dfs(i+1,mul*bit[i],p2+(j%2==0)+(j%4==0)+(j==8),p3+(j%3==0)+(j==9),p5+(j==5),p7+(j==7));
    }
}
void work(){
    cin>>n;
    memset(ans,0,sizeof ans);

    len=0;
    int tmp=n+1;
    while(tmp){
        bit[++len]=tmp%10;
        tmp/=10;
    }
    reverse(bit+1,bit+len+1);

    for(int i=1;i<len;i++){
        Calc(i,1,0,0,0,0);
    }
    Dfs(1,1,0,0,0,0);

    /*----fix 0----*/
    for(int i=2;i<len;i++){
        ans[0]+=9*(MyPow(10,i-1)-MyPow(9,i-1));
    }


    bool z0=0;
    for(int i=1;i<=len;i++){
        for(int j=i==1?1:0;j<bit[i];j++){
            ans[0]+=(z0||j==0)?MyPow(10,len-i):MyPow(10,len-i)-MyPow(9,len-i);
        }
        z0|=bit[i]==0;
    }
    /*--------------*/

    // assert(n==accumulate(ans,ans+10,0ll));

    for(int i=0;i<10;i++){
        cout<<ans[i]<<" \n"[i==9];
    }
}
bool Med;
signed main(){
    ios::sync_with_stdio(0),
    cin.tie(0),cout.tie(0);
    int T=1;init();
    cin>>T;while(T--)work();
    // cerr<<"Time: "<<clock()<<" ms;\n";
    // cerr<<"Memory: "<<abs(&Med-&Mbg)/1024.0/1024.0<<" MiB.\n";
}
/*
- CONTINUE, NON-STOPPING, FOR THE FAITH
- START TYPING IF YOU DON'T KNOW WHAT TO DO
- STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
*/