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

#define ll unsigned long long

unordered_map <string, ll> vis;
ll cnt_root(int e2, int e3, int e5, int e7)
{
  string hash = to_string(e2)+"#"+to_string(e3)+"#"+to_string(e5)+"#"+to_string(e7);
  if(vis.find(hash) != vis.end()) return vis[hash];
  
  ll M = 1;
  for(int i=1; i<=e2; i++) M *= 2LL;
  for(int i=1; i<=e3; i++) M *= 3LL;
  for(int i=1; i<=e5; i++) M *= 5LL;
  for(int i=1; i<=e7; i++) M *= 7LL;
  
  while(M >= 10)
  {
    ll ret=1, temp=M;
    while(temp) ret *= (temp%10), temp/=10;
    M = ret;
  }
  return vis[hash] = M;
}
// bit 0 -> czy zaczete
// bit 1 - > czy wyzerowane
// bity [2->7] -> e2 (max 3*18 < 64)
// bity [8->13] -> e3 (max 2*18 < 64)
// bity [14->18] -> e5 (max 18 < 32)
// bity [19->23] -> e7 (max 18 < 32)
inline int zakoduj(bool st, bool term, int e2, int e3, int e5, int e7)
{
  return ((int)st | ((int)term<<1) | ((e2&63)<<2) | ((e3&63)<<8) | ((e5&31)<<14) | ((e7&31)<<19));
}
inline void odkoduj(int it, bool &st, bool &term, int &e2, int &e3, int &e5, int &e7)
{
  st = it&1; term = (it>>1)&1;
  e2 = (it>>2)&63; e3 = (it>>8)&63;
  e5 = (it>>14)&31; e7 = (it>>19)&31;
}
inline int wypchnij(int prev, int i)
{
  bool st,term; int e2,e3,e5,e7;
  odkoduj(prev,st,term,e2,e3,e5,e7);

  if(!st && !i) return prev;
  else if(st)
  {
    if(term) return prev;
    if(i == 0)
    {
      term=1; e2=e3=e5=e7=0;
      return zakoduj(st,term,e2,e3,e5,e7);
    }
  }

  st = true;
  if(i == 2) e2++;
  else if(i == 3) e3++;
  else if(i == 4) e2+=2;
  else if(i == 5) e5++;
  else if(i == 6) e2++,e3++;
  else if(i == 7) e7++;
  else if(i == 8) e2+=3;
  else if(i == 9) e3+=2;

  if(!term && st && e2 > 0 && e5 > 0) term=1,e2=e3=e5=e7=0;
  return zakoduj(st,term,e2,e3,e5,e7);
}
inline int find_root(int it)
{
  bool st,term;  int e2,e3,e5,e7;
  odkoduj(it,st,term,e2,e3,e5,e7);
  if(!st) return -1;
  if(term) return 0;
  return cnt_root(e2,e3,e5,e7);
}
unordered_map <ll, array <ll,10>> base;
inline ll zip(int rem, int state)
{ 
  return (((ll)rem) << 32) | state; 
}
array <ll,10> fill_dp(int rem, int st)
{
  ll val = zip(rem,st);
  if(base.find(val) != base.end()) return base[val];

  unordered_map <int,ll> dp; dp[st] = 1;

  for(int i=0; i<rem; i++)
  {
    unordered_map <int,ll> next;
    for(auto p : dp)
    {
      int prev = p.first; ll il = p.second;
      for(int i=0; i<=9; i++) next[wypchnij(prev,i)] += il;
    }
    dp = next;
  }
  array <ll,10> ret = {0,0,0,0,0,0,0,0,0,0};
  for(auto p : dp)
  {
    int root = find_root(p.first);
    if(root < 0 || root >= 10) continue;
    ret[root] += p.second;
  }
  base[val] = ret;
  return ret;
}
array <ll,10> solve(string s)
{
  int n = (int)s.size(); unordered_map <int,ll> dp;
  dp[zakoduj(0,0,0,0,0,0)]=1; array <ll,10> ret = {0,0,0,0,0,0,0,0,0,0};

  for(int pos=0; pos<n; pos++)
  {
    unordered_map <int,ll> next;
    int lim = s[pos]-'0';
    for(auto p : dp)
    {
      int prev = p.first; ll il = p.second;
      for(int i=0; i<=lim; i++)
      {
        int act = wypchnij(prev,i);
        if(i == lim) next[act] += il;
        else
        {
          int rem = n-pos-1;
          array <ll,10> tmp = fill_dp(rem,act);
          for(int m=0; m<10; m++) ret[m] += (il*tmp[m]);
        }
      }
    }
    dp = next;
  }
  for(auto p : dp)
  {
    int root = find_root(p.first);
    if(root < 0 || root >= 10) continue;
    ret[root] += p.second;
  }
  return ret;
}
int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

  int t; cin >> t;
  while(t--)
  {
    ll n; cin >> n;
    array <ll,10> res = solve(to_string(n));
    for(int m=0; m<=9; m++) cout << res[m] << ' ';
    cout << '\n';
  }
}