#include <bits/stdc++.h>
using namespace std;
long long Fac(int n) {
  long long ret = 1;
  if (n < 2) return ret;
  for (int i = 2; i <= n; i++) ret *= i;
  return ret;
}
int main() {
  int T, Hest, sisa[27];
  long long K, ans, tmp, pembagi;
  cin >> T;
  while (T--) {
    memset(sisa, 0, sizeof(sisa));
    cin >> K;
    for (Hest = 2; K > 0; Hest++) {
      sisa[K % Hest]++;
      K /= Hest;
    }
    vector<int> pengali;
    tmp = sisa[0] + sisa[1];
    ans = 1;
    pembagi = Fac(sisa[0]) * Fac(sisa[1]);
    for (int i = 2; i < Hest; i++) {
      pengali.push_back(tmp);
      ans *= tmp;
      tmp += sisa[i];
      pembagi *= Fac(sisa[i]);
      tmp--;
    }
    if (sisa[0]) {
      tmp = 1;
      for (int i = 0; i < pengali.size() - 1; i++) {
        tmp *= pengali[i] - 1;
      }
      tmp *= sisa[0];
      ans -= tmp;
    }
    cout << ans / pembagi - 1 << endl;
  }
}
