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