#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll binom[30][30];
map<int, int> fingerprint(ll k) {
  map<int, int> ret;
  int i = 2;
  while (k != 0) {
    ret[k % i]++;
    k /= i;
    i++;
  }
  return ret;
}
ll calc(map<int, int> m, int len) {
  ll ret = 1;
  ll ileposzlo = 0;
  auto pt = m.end();
  pt--;
  if (pt->first > len) return 0;
  for (ll rem = len; rem >= 1; rem--) {
    ret *= binom[len - rem + 1 - ileposzlo + 1][m[rem] + 1];
    ileposzlo += m[rem];
  }
  return ret;
}
int main() {
  binom[0][0] = 1;
  for (int i = 1; i < 30; i++)
    for (int j = 1; j < 30; j++)
      binom[i][j] = binom[i - 1][j] + binom[i - 1][j - 1];
  int t;
  scanf("%d", &t);
  while (t--) {
    ll k;
    scanf("%lld", &k);
    auto p = fingerprint(k);
    int len = 0;
    for (auto x = p.begin(); x != p.end(); x++) len += x->second;
    ll ret = calc(p, len);
    if (p.find(0) != p.end()) {
      p.erase(p.find(0));
      ret -= calc(p, len - 1);
    }
    cout << ret - 1 << endl;
  }
}
