#include <bits/stdc++.h>
using namespace std;
template <class T>
struct rge {
  T b, e;
};
template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}
struct debug {
  template <class T>
  debug& operator<<(const T&) {
    return *this;
  }
};
using ll = long long;
template <typename T>
void min_self(T& a, T b) {
  a = min(a, b);
}
template <typename T>
void max_self(T& a, T b) {
  a = max(a, b);
}
vector<int> calc(ll n) {
  vector<int> ans;
  int p = 2;
  while (n > 0) {
    ans.push_back(n % p);
    n /= p;
    p++;
  }
  sort(ans.rbegin(), ans.rend());
  return ans;
}
ll go(vector<int> v) {
  ll ans = 1;
  int n = v.size();
  map<int, int> cnt;
  debug() << "[ "
          << "v"
             ": "
          << (v) << " ] ";
  for (int i = 0; i < n; i++) {
    int x = v[i];
    ans *= max(0, n - max(0, (x - 1)) - i);
    ans /= ++cnt[x];
  }
  return ans;
}
void test_case() {
  ll n;
  cin >> n;
  vector<int> v = calc(n);
  if (v.back() == 0) {
    vector<int> nv = v;
    nv.pop_back();
    cout << go(v) - go(nv) - 1 << '\n';
  } else {
    cout << go(v) - 1 << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    test_case();
  }
}
