#include <bits/stdc++.h>
using namespace std;
long long ncr[31][31];
long long get(long long n) {
  vector<long long> cnt(31);
  long long curr = 2;
  while (n) {
    cnt[(n % curr)]++;
    n /= curr;
    curr++;
  }
  curr--;
  long long ans = 1, ans2 = 1;
  long long has = 1;
  for (long long i = curr - 1; i > 0; i--) {
    long long need = cnt[i];
    assert(has >= need);
    ans *= ncr[has][need];
    has -= need;
    has++;
  }
  has = 0;
  for (long long i = curr - 1; i > 0; i--) {
    long long need = cnt[i];
    ans2 *= ncr[has][need];
    has -= need;
    has++;
  }
  ans -= ans2;
  return ans - 1;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ncr[0][0] = 1;
  for (long long i = 1; i < 31; i++) {
    ncr[i][0] = 1;
    for (long long j = 1; j <= i; j++) {
      ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1];
    }
  }
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    cout << get(n) << '\n';
  }
  return 0;
}
