#include <bits/stdc++.h>
using namespace std;
unsigned long long fact(int n) {
  unsigned long long x = 1;
  for (int i = 1; i <= n; i++) x *= i;
  return x;
}
void run() {
  unsigned long long n;
  cin >> n;
  int arr[25];
  fill(arr, arr + 25, 0);
  int x = 2;
  while (n) {
    arr[n % x]++;
    n /= x++;
  }
  for (int i = 1; i < 25; i++) arr[i] += arr[i - 1];
  unsigned long long result = 1;
  for (int i = 1; i < x - 1; i++) {
    result *= arr[i] - i + 1;
  }
  result /= fact(arr[0]);
  for (int i = 1; i < 25; i++) {
    result /= fact(arr[i] - arr[i - 1]);
  }
  if (arr[0] > 0) {
    for (int i = 0; i < 25; i++) arr[i]--;
    unsigned long long red = 1;
    for (int i = 1; i < x - 2; i++) {
      red *= arr[i] - i + 1;
    }
    red /= fact(arr[0]);
    for (int i = 1; i < 25; i++) {
      red /= fact(arr[i] - arr[i - 1]);
    }
    result -= red;
  }
  cout << result - 1 << endl;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) run();
}
