#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 69, mod = 1e9 + 9;
long long n, m, x, y, a, b, t, k, ans, sum, cur, rao;
char c;
long long fix[106];
vector<long long> v, v1, v2;
long long C[100][100], fac[100];
void count() {
  fac[0] = 1;
  for (int i = 1; i <= 20; i++) fac[i] = fac[i - 1] * i;
  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= i; j++) C[j][i] = fac[i] / fac[j] / fac[i - j];
  }
}
void cl() {
  for (int i = 0; i <= 20; i++) fix[i] = 0;
  v.clear();
  v1.clear();
  ans = 0;
}
void solve() {
  cin >> n;
  k = 2, x = n, rao = 0;
  while (x > 0) {
    v.push_back(x % k);
    x /= k;
    k++;
    rao++;
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (!fix[v[i]]) v1.push_back(v[i]);
    fix[v[i]]++;
  }
  k--;
  for (int i = 0; i < v1.size(); i++) {
    if (v1[i] == 0) continue;
    fix[v1[i]]--;
    long long ans1 = 1, shev = 1;
    for (int j = v1.size() - 1; v1[j] > 0 && j >= 0; j--) {
      if (!fix[v1[j]]) continue;
      ans1 *= C[fix[v1[j]]][k - v1[j] - shev];
      shev += fix[v1[j]];
    }
    ans += ans1;
    fix[v1[i]]++;
  }
  cout << ans - 1 << endl;
  cl();
}
void input() {
  int T;
  cin >> T;
  while (T--) solve();
}
int main() {
  count();
  input();
}
