#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
    cin >> n;
    while (n % 6 == 0) {
      n /= 6;
      ans++;
    }
    while (n != 1) {
      if ((n * 2) % 6 == 0) {
        n = (n * 2) / 6;
        ans += 2;
        continue;
      } else {
        ans = -1;
        break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
