#include <bits/stdc++.h>
int a[22], fac[22][22], b[22], c[22];
inline void mul(int a[], int x) {
  switch (x) {
    case 4:
      a[2] += 2;
      break;
    case 6:
      a[2]++;
      a[3]++;
      break;
    case 8:
      a[2] += 3;
      break;
    case 9:
      a[3] += 2;
      break;
    case 10:
      a[2]++;
      a[5]++;
      break;
    case 12:
      a[2] += 2;
      a[3]++;
      break;
    case 14:
      a[2]++;
      a[7]++;
      break;
    case 15:
      a[3]++;
      a[5]++;
      break;
    case 16:
      a[2] += 4;
      break;
    case 18:
      a[2]++;
      a[3] += 2;
      break;
    case 20:
      a[2] += 2;
      a[5]++;
      break;
    default:
      a[x]++;
  }
}
inline void mul(int a[], int b[]) {
  for (int i = 2; i <= 20; i++) b[i] += a[i];
}
inline void div(int a[], int b[]) {
  for (int i = 2; i <= 20; i++) b[i] -= a[i];
}
inline void cpy(int a[], int b[]) {
  for (int i = 2; i <= 20; i++) b[i] = a[i];
}
inline long long pow(long long a, int n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans *= a;
    a *= a;
    n >>= 1;
  }
  return ans;
}
inline long long calc(int a[]) {
  long long ans = 1;
  for (int i = 1; i <= 20; i++) ans *= pow(i, a[i]);
  return ans;
}
int main() {
  int i, t;
  long long n, ans;
  bool flg;
  for (i = 2; i <= 20; i++) cpy(fac[i - 1], fac[i]), mul(fac[i], i);
  scanf("%d", &t);
  while (t--) {
    scanf("%lld", &n);
    memset(a, 0, sizeof a);
    memset(b, 0, sizeof b);
    memset(c, 0, sizeof c);
    for (i = 2; n; i++) a[n % i]++, n /= i;
    n = i - 2;
    flg = a[0];
    for (i = 0; i <= n; i++) div(fac[a[i]], c);
    for (i = 1; i <= n; i++) a[i] += a[i - 1];
    for (i = 1; i <= n; i++) mul(b, a[i] - i + 1);
    mul(c, b);
    ans = calc(b);
    mul(c, a[0]);
    for (i = 1; i < n; i++) {
      if (a[i] == i) {
        flg = false;
        break;
      }
      mul(c, a[i] - i);
    }
    if (flg) ans -= calc(c);
    ans--;
    printf("%lld\n", ans);
  }
  return 0;
}
