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
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;

#define debug 0

typedef long long LL;

int N = 1000000;
vector<LL> rmult;

LL mult(int v) {
  if (v < 10) return v;
  
  LL r = 1;
  while (v > 0) {
    r *= v % 10;
    v /= 10;
  }

  return r;
}

LL recurse(LL v) {
  if (v < 10) return v;
  
  LL r = 1;
  while (v > 0) {
    r *= rmult[v % N];
    v /= N;
  }

  return recurse(r);
}


vector<vector<LL>> poww;

LL pow(int a, int b) {
  LL res = 1;
  while (b--) res *= a;
  return res;
}

vector<LL> factt;

LL fact(int a) {
  LL res = 1;
  while (a > 1) res *= a--;
  return res;
}

// k - digits used
// n - num digits
// l - digit considered
//
// multiplication result, bipol -> count

typedef map<pair<LL, LL>, LL> LMAP;

map<pair<int, int>, LMAP> ccompute;

LMAP compute(int n, int l);
const LMAP& compute_get(int n, int l) {
  const auto& it = ccompute.find(make_pair(n, l));
  if (it != ccompute.end()) return it->second;

  ccompute[make_pair(n, l)] = compute(n, l);
  return ccompute[make_pair(n, l)];
}

LMAP compute(int n, int l) {
  LMAP res;
  if (l == 9) {
    res[make_pair(poww[l][n], factt[n])] = 1;
  } else {
    for (int s=0; s<=n; ++s) {
      LL v = poww[l][s];
      const auto& u = compute_get(n-s, l+1);
      for (const auto& el: u) {
        res[make_pair(el.first.first * v, el.first.second * factt[s])] += el.second;
      }
    }
  }
  return std::move(res);
}

map<LL, LL> compute(int n) {
  map<LL, LL> res;
  for (const auto& el: compute_get(n, 0)) {
    res[el.first.first] += factt[n] / el.first.second * el.second;
  }
  return std::move(res);
}

map<int, map<LL, LL>> cccompute;
const map<LL, LL>* ccompute_get(int n) {
  auto it = cccompute.find(n);
  if (it != cccompute.end()) return &(it->second);

  cccompute[n] = std::move(compute(n));
  return &(cccompute.find(n)->second);
}

void doit() {
  LL n; scanf(" %lld", &n);
  
  vector<int> digits;
  while (n > 0) {
    digits.push_back(n % 10);
    n /= 10;
  }

  vector<LL> res(10, 0);
  
  res[0]--;

  LL mult = 1;
  while (!digits.empty()) {
    int v = digits.back(); digits.pop_back();

    const map<LL, LL>* counts = ccompute_get(digits.size());

    if (digits.size() > 0) {
      for (const auto& el: *counts) {
        res[0] -= el.second;
        res[recurse(el.first)] += el.second;
      }
    }
    for (int i=0; i<v; ++i) {
      for (const auto& el: *counts) {
        res[ recurse(el.first * i * mult) ] += el.second;
      }
    }
    mult *= v;

  }
  res[ recurse(mult) ]++;

  for (LL el: res) {
    printf("%lld ", el);
  }
  printf("\n");
}

int main() {
  for (int i=0; i<10; ++i) {
    poww.emplace_back();
    for (int j=0; j<20; ++j) poww.back().push_back(pow(i,j));
  }

  for (int i=0; i<20; ++i) factt.push_back(fact(i));

  rmult.resize(N);
  for (int i=0; i<N; ++i) rmult[i] = mult(i); 

  int t; scanf(" %d", &t);
  while (t--) doit();
  return 0;
}