#include <bits/stdc++.h>
using namespace std;
long long C[30][30];
long long solve(vector<int> &v) {
  long long ans = 1;
  int len = 0;
  for (int i = 0; i < v.size(); i++) {
    if (i == 0)
      len++;
    else if (v[i] == v[i - 1])
      len++;
    else
      len = 1;
    ans *= (v.size() - max(v[i], 1) + 1) - i;
    ans /= len;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    vector<int> v;
    map<int, int> m;
    long long n;
    cin >> n;
    for (long long i = 2; i <= 25; i++) {
      if (!n) break;
      v.push_back(n % i);
      n /= i;
    }
    sort(v.begin(), v.end(), greater<int>());
    long long ans = solve(v);
    if (v.back() == 0) {
      v.pop_back();
      ans -= solve(v);
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
