#include <bits/stdc++.h>
using namespace std;
void solve(int &m) {
  int count = 0;
  bool flag;
again:
  if (m % 6 == 0) {
    m /= 6;
    count++;
    goto again;
  } else if (m % 3 == 0) {
    m /= 3;
    count += 2;
    goto again;
  } else if (m != 1)
    cout << -1 << endl;
  else
    cout << count << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  int i = 0;
  int n[t];
  while (i < t) {
    cin >> n[i];
    if (n[i] == 1)
      cout << 0 << endl;
    else
      solve(n[i]);
    i++;
  }
  return 0;
}
