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