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