#include <bits/stdc++.h>
using namespace std;
int T;
long long n;
long long C(int a, int b) {
  int res = a;
  bool used[30];
  for (int i = 0; i < 30; i++) used[i] = 0;
  for (int i = a - 1; i >= a - b + 1; i--) {
    res *= i;
    for (int j = b; j >= 1; j--) {
      if (!used[j] && res % j == 0) {
        used[j] = 1;
        res /= j;
      }
    }
  }
  return res;
}
long long Minus(vector<long long> v) {
  long long res = 1, sum = v.size() - 1;
  int ji = 0, last = v[0];
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == last)
      ji++;
    else {
      res *= C(sum - last + (last > 0), ji);
      last = v[i];
      sum -= ji;
      ji = 1;
    }
  }
  return res;
}
long long solve() {
  vector<long long> v;
  v.clear();
  long long cnt = 2, ans = 1;
  int ji = 0, last;
  while (n) {
    v.push_back(n % cnt);
    n /= cnt++;
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  v.push_back(-1);
  last = v[0];
  long long sum = v.size() - 1;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == last)
      ji++;
    else {
      ans *= C(sum - last + (last > 0), ji);
      sum -= ji;
      ji = 1;
      last = v[i];
    }
  }
  if (v[v.size() - 2] == 0 && v[0] != v.size() + 1) {
    v.pop_back();
    v.pop_back();
    v.push_back(0);
    ans -= Minus(v);
  }
  return ans - 1;
}
int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    cout << solve() << endl;
  }
  return 0;
}
