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