#include <bits/stdc++.h>
using namespace std;
using db = long double;
using pll = pair<long long, long long>;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t, i, x, j, y, z, k, n;
  long long tt;
  cin >> tt;
  for (long long _tt = 0; _tt < tt; _tt++) {
    cin >> n;
    z = 0;
    while (n != 1) {
      if (n % 6 == 0)
        n /= 6;
      else if ((n * 2) % 6 == 0)
        n *= 2;
      else {
        z = -1;
        break;
      }
      z++;
    }
    cout << z << '\n';
  }
}
