#include <bits/stdc++.h>
using namespace std;
const int MX = 20;
long long fact[MX];
long long getAns(vector<long long> &freq, long long sz) {
  if (sz == 0) return 0;
  long long ans = 1;
  long long acum = freq[0];
  for (int i = 1; i <= sz; i++) {
    acum += freq[i];
    ans *= acum - i + 1;
  }
  for (int i = 0; i < freq.size(); i++) {
    ans /= fact[freq[i]];
  }
  return ans;
}
void solve() {
  long long x;
  cin >> x;
  vector<long long> v;
  long long i = 2;
  while (x > 0) {
    v.push_back(x % i);
    x /= i;
    i++;
  }
  vector<long long> freq(MX, 0);
  for (long long x : v) {
    freq[x]++;
  }
  long long ans = getAns(freq, v.size());
  if (freq[0] >= 1) {
    freq[0]--;
    ans -= getAns(freq, (int)v.size() - 1);
  }
  ans--;
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1;
  for (long long i = 1; i < MX; i++) {
    fact[i] = i * fact[i - 1];
  }
  int T;
  cin >> T;
  for (int t = 0; t < (int)T; t++) {
    solve();
  }
  return 0;
}
