#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
vector<long long> getFinger(long long n) {
  vector<long long> ans;
  long long div = 2;
  while (n > 0) {
    ans.push_back(n % div);
    n /= div;
    ++div;
  }
  sort(ans.begin(), ans.end());
  return ans;
}
long long fact(long long num) {
  long long ans = 1;
  for (long long i = 1; i <= num; ++i) {
    ans *= i;
  }
  return ans;
}
long long getCount(vector<long long> v) {
  vector<long long> used(100, 0);
  int pos = 0;
  long long winx = 1;
  for (long long i = 0; i < v.size(); ++i) {
    ++used[v[i]];
    while (pos < v.size() && v[pos] < (i + 2)) {
      ++pos;
    }
    winx *= (pos - i);
  }
  for (long long elem : used) {
    winx /= fact(elem);
  }
  return winx;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v = getFinger(n);
    long long ans = getCount(v);
    if (v[0] == 0) {
      v.erase(v.begin());
      ans -= getCount(v);
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
