#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modInverse(long long int A, long long int M) {
  return power(A, M - 2, M);
}
void solve() {
  long long int n;
  cin >> n;
  int ans = 0;
  if (n == 1) {
    cout << 0 << "\n";
    return;
  }
  while (n != 1) {
    if (n % 3 != 0) {
      cout << -1 << "\n";
      return;
    }
    if (n % 6 == 0)
      n /= 6;
    else
      n *= 2;
    ans++;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) solve();
  return 0;
}
