#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long n;
vector<int> FP;
int s;
long long getme() {
  long long ans = 1, acum = 1;
  for (int i = (0); i < (int)(s); i++) {
    if (FP[i] == 0) break;
    long long act = s - (FP[i] - 1) - i;
    if (i && FP[i] == FP[i - 1])
      acum++;
    else
      acum = 1;
    ans *= act;
    ans /= acum;
  }
  return ans;
}
int main() {
  int tc;
  scanf("%d", &tc);
  for (int cs = (0); cs < (int)(tc); cs++) {
    scanf("%lld", &n);
    FP.clear();
    for (int d = (2); d < (int)(30); d++) {
      FP.push_back(n % d);
      n /= d;
      if (!n) break;
    }
    sort(FP.begin(), FP.end());
    reverse(FP.begin(), FP.end());
    s = ((int)FP.size());
    long long ans = getme();
    if (FP[s - 1] == 0) {
      FP.pop_back();
      s--;
      ans -= getme();
    }
    printf("%lld\n", ans - 1);
  }
  return 0;
}
