#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll MAXN = 1e7 + 10;
int spf[MAXN];
void sieveof() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++) {
    spf[i] = i;
  }
  for (int i = 4; i < MAXN; i += 2) {
    spf[i] = 2;
  }
  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}
ll MSum(vector<ll> arr, ll n, ll k) {
  ll sum = 0;
  ll cnt = 0, maxcnt = 0;
  for (int i = 0; i < n; i++) {
    if ((sum + arr[i]) <= k) {
      sum += arr[i];
      cnt++;
    } else if (sum != 0) {
      sum = sum - arr[i - cnt] + arr[i];
    }
    maxcnt = max(cnt, maxcnt);
  }
  return maxcnt;
}
void solve() {
  ll x, y, n, ans = 0, flag = 1;
  cin >> n;
  while (n > 1) {
    if (n % 6 == 0) {
      n /= 6;
    } else {
      n *= 2;
    }
    ans++;
  }
  n == 1 ? cout << ans << "\n" : cout << "-1\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
