#include <bits/stdc++.h>
using namespace std;
int main() {
  long long fac[25];
  fac[0] = fac[1] = 1;
  for (int i = 2; i <= 20; i++) {
    fac[i] = fac[i - 1] * i;
  }
  int tc;
  cin >> tc;
  while (tc--) {
    long long k;
    cin >> k;
    bool exist = false;
    int count[25];
    for (int i = 0; i < 25; i++) {
      count[i] = 0;
    }
    int div = 2;
    while (k > 0) {
      if (k % div == 0) exist = true;
      count[k % div]++;
      k /= div;
      div++;
    }
    int div2 = div;
    long long ans = 1;
    for (int i = 24; i >= 0; i--) {
      int save;
      bool ada = false;
      if (count[i] > 0) {
        save = count[i];
        ada = true;
      }
      for (int j = 0; j < count[i]; j++) {
        int x = max(1, i);
        ans = ans * (div - x - 1);
        div--;
      }
      if (ada) ans /= fac[save];
    }
    if (exist) {
      long long tmp = 1;
      count[0]--;
      for (int i = 24; i >= 0; i--) {
        int save;
        bool ada = false;
        if (count[i] > 0) {
          save = count[i];
          ada = true;
        }
        for (int j = 0; j < count[i]; j++) {
          int x = max(1, i);
          tmp = tmp * (div2 - x - 2);
          div2--;
        }
        if (ada) tmp /= fac[save];
      }
      ans -= tmp;
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
