#include <bits/stdc++.h>
using namespace std;
void func(long long int N, long long int* cnt) {
  if (N == 1) return;
  if (N % 6 == 0) {
    (*cnt)++;
    func(N / 6, cnt);
  } else if (N % 2 == 0) {
    (*cnt) = -1;
    return;
  } else {
    if (N % 3 != 0) {
      (*cnt) = -1;
      return;
    }
    if (N % 3 == 0) {
      (*cnt) += 2;
      func(N / 3, cnt);
    }
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long int N;
    cin >> N;
    long long int cnt = 0;
    func(N, &cnt);
    cout << cnt << endl;
  }
  return 0;
}
