#include <bits/stdc++.h>
using namespace std;
bool check(int a, int b) {
  if (b % a == 0) return true;
  return false;
}
void solve() {
  int n;
  cin >> n;
  bool can = true;
  int counter = 0;
  while (n > 1) {
    if (n % 6 == 0) {
      n /= 6;
      counter++;
    } else if ((n * 2) % 6 == 0) {
      n *= 2;
      counter++;
    } else {
      can = false;
      break;
    }
  }
  if (can)
    cout << counter << endl;
  else
    cout << -1 << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
