#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
void solve(long long n) {
  long long step = 0;
  while (n != 1) {
    if (n % 2 == 0) {
      if (n % 6 != 0) {
        ans.push_back(-1);
        return;
      }
      n /= 6;
      if (n == 0) {
        ans.push_back(-1);
        return;
      }
    } else {
      n *= 2;
      if (n % 6 != 0) {
        ans.push_back(-1);
        return;
      }
    }
    ++step;
  }
  ans.push_back(step);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    solve(n);
  }
  for (int i = (0); i < (ans.size()); ++i) cout << ans[i] << endl;
  return 0;
}
