#include <bits/stdc++.h>
using namespace std;
long long T, n, fac[1001], cnt[100], prefix = 0;
void fact() {
  fac[0] = 1;
  for (long long i = 1; i <= 20; i++) {
    fac[i] = i * fac[i - 1];
  }
}
void Solve() {
  long long temp = n, i = 2, tot = 0;
  while (temp > 0) {
    cnt[temp % i]++;
    tot++;
    temp /= i;
    i++;
  }
  prefix += cnt[0] + cnt[1];
  long long sot = 1;
  for (long long i = 2; i <= tot + 1; i++) {
    sot *= prefix;
    prefix += cnt[i];
    prefix--;
  }
  for (long long i = 0; i <= 20; i++) {
    sot /= fac[cnt[i]];
  }
  long long sec = 0;
  if (cnt[0] > 0) {
    sec = cnt[0];
    prefix = cnt[0] + cnt[1] - 1;
    for (long long i = 2; i <= tot; i++) {
      sec *= prefix;
      prefix += cnt[i];
      prefix--;
    }
    for (long long i = 0; i <= 20; i++) {
      sec /= fac[cnt[i]];
    }
  }
  cout << sot - sec - 1 << '\n';
}
int main() {
  cin >> T;
  fact();
  while (T--) {
    memset(cnt, 0, sizeof cnt);
    cin >> n;
    Solve();
  }
}
