#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int M = 2e6 + 5;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long ksm(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int a[100];
long long n, k;
long long C(int n, int m) {
  if (n > m) return 0;
  long long res = 1;
  for (int i = 0; i < n; i++) res = res * (m - i) / (i + 1);
  return res;
}
long long solve(int l) {
  if (l > n) return 0;
  long long res = 1, cnt = n - l + 1;
  int num = 1;
  for (int i = n; i >= l; i--) {
    if (a[i] != a[i - 1]) {
      res *= C(num, cnt - (a[i] - 1));
      cnt -= num;
      num = 1;
    } else
      num++;
  }
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(a, 0, sizeof(a));
    scanf("%lld", &k);
    for (n = 1; k; n++) {
      a[n] = k % (n + 1);
      k /= (n + 1);
    }
    n--;
    sort(a + 1, a + n + 1);
    long long res = solve(1);
    if (a[1] == 0) res -= solve(2);
    printf("%lld\n", max(res - 1, (long long)0));
  }
  return 0;
}
