#include <bits/stdc++.h>
using namespace std;
int t;
long long k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> k;
    vector<long long> v;
    int d = 2;
    while (k) {
      v.emplace_back(k % d);
      k /= d++;
    }
    sort(v.rbegin(), v.rend());
    unordered_map<long long, int> cnt;
    int n = v.size();
    long long ans = 1;
    for (int i = 0; i < n; i++) {
      cnt[v[i]]++;
      ans = ans * max(0LL, n - max(0LL, v[i] - 1) - i) / cnt[v[i]];
    }
    if (v.back() == 0) {
      cnt.clear();
      long long temp = 1;
      for (int i = 0; i < n - 1; i++) {
        cnt[v[i]]++;
        temp = temp * max(0LL, n - 1 - max(0LL, v[i] - 1) - i) / cnt[v[i]];
      }
      ans -= temp;
    }
    cout << ans - 1 << "\n";
  }
}
