#include <bits/stdc++.h>
using namespace std;
long long inline read() {
  long long num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}
const long long maxn = 110, mod = 998244353;
long long T, n, a[maxn], len, f[maxn], s[maxn], t[maxn];
signed main() {
  T = read();
  while (T--) {
    n = read();
    len = 0;
    for (long long i = 2; n; i++) a[++len] = n % i, n /= i;
    for (long long i = 1; i <= len; i++) s[a[i]]++, t[a[i]]++;
    for (long long i = 1; i <= len; i++) s[i] += s[i - 1];
    f[0] = 1;
    for (long long i = 1; i <= len; i++) f[i] = f[i - 1] * (s[i] - i + 1);
    long long ans = f[len];
    for (long long i = 0; i <= len; i++)
      for (long long j = 2; j <= t[i]; j++) ans /= j;
    if (t[0]) {
      for (long long i = 0; i <= len; i++) s[i] = t[i] = 0;
      for (long long i = 1; i <= len; i++) s[a[i]]++, t[a[i]]++;
      s[0]--;
      t[0]--;
      for (long long i = 1; i <= len; i++) s[i] += s[i - 1];
      f[0] = 1;
      for (long long i = 1; i <= len - 1; i++) f[i] = f[i - 1] * (s[i] - i + 1);
      long long ans1 = f[len - 1];
      for (long long i = 0; i <= len - 1; i++)
        for (long long j = 2; j <= t[i]; j++) ans1 /= j;
      ans -= ans1;
    }
    for (long long i = 0; i <= len; i++) s[i] = t[i] = 0;
    printf("%lld\n", ans - 1);
  }
  return 0;
}
