#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 100;
int cnt[MAXN];
int pre[MAXN];
ll fact[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = fact[i - 1] * i;
  }
  int kase;
  cin >> kase;
  while (kase--) {
    memset(cnt, 0, sizeof(cnt));
    memset(pre, 0, sizeof(pre));
    ll n;
    cin >> n;
    int idx = 2;
    while (n) {
      cnt[n % idx]++;
      n /= idx;
      idx++;
    }
    pre[0] = cnt[0];
    for (int i = 1; i <= idx; i++) {
      pre[i] = pre[i - 1] + cnt[i];
    }
    ll same = 1;
    for (int i = 0; i < idx; i++) {
      same *= fact[cnt[i]];
    }
    ll ans = 1;
    idx--;
    for (int i = 1; i < idx; i++) {
      ans *= (pre[i] - i + 1);
    }
    ll cost = pre[0];
    for (int i = 0; i < idx; i++) {
      pre[i]--;
    }
    for (int i = 1; i < idx - 1; i++) {
      cost *= (pre[i] - i + 1);
    }
    cout << (ans - cost) / same - 1 << endl;
  }
  return 0;
}
