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