#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
template <typename T>
void read(T &qq) {
  qq = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    qq = qq * 10 + ch - 48;
    ch = getchar();
  }
  qq *= f;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n;
    scanf("%lld", &n);
    int sum = 0;
    while (n >= 1) {
      if (n % 3 != 0) break;
      if (n % 6 == 0) {
        n /= 6;
        sum++;
      } else {
        n *= 2;
        sum++;
      }
    }
    if (n == 1)
      cout << sum << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
