#include <bits/stdc++.h>
using namespace std;
long long cnt[100];
void solve() {
  vector<long long> s(0);
  long long x;
  cin >> x;
  for (long long i = 2; i <= 21; i++) {
    if (!x) break;
    s.push_back(x % i);
    x /= i;
  }
  sort(s.begin(), s.end(), greater<long long>());
  long long ans = 0;
  long long res = 1;
  long long n = s.size();
  for (long long i = 0; i < s.size(); i++) {
    long long num = max(0LL, n - max(0LL, s[i] - 1) - i);
    long long cnt = 0;
    for (long long j = i; j >= 0; j--) {
      if (s[i] == s[j]) cnt++;
    }
    res = res * num / cnt;
  }
  ans += res;
  if (s.back() == 0) {
    res = 1;
    s.pop_back();
    n--;
    for (long long i = 0; i < s.size(); i++) {
      long long num = max(0LL, n - max(0LL, s[i] - 1) - i);
      long long cnt = 0;
      for (long long j = i; j >= 0; j--) {
        if (s[i] == s[j]) cnt++;
      }
      res = res * num / cnt;
    }
    ans -= res;
  }
  cout << ans - 1 << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
