#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const int inf = 0x3f3f3f;
const int maxn = 3e5 + 5;
const int mod = 998244353;
int k[maxn];
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, x, y;
    cin >> n;
    int ans = 0;
    ;
    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';
  }
}
