#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
  int x = 0, neg = 1;
  char op = getchar();
  while (!isdigit(op)) {
    if (op == '-') neg = -1;
    op = getchar();
  }
  while (isdigit(op)) {
    x = 10 * x + op - '0';
    op = getchar();
  }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  int T = read();
  while (T--) {
    int n = read(), ans = 0;
    while (n % 3 == 0) {
      if (n & 1) {
        ans += 2;
        n = 2 * n / 6;
      } else {
        ans++;
        n /= 6;
      }
    }
    printf("%d\n", n == 1 ? ans : -1);
  }
  return 0;
}
