#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const int N = 105;
int a[55], b[55];
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long n, res = 0;
    cin >> n;
    while (n > 1) {
      if (n % 3 != 0) {
        res = -1;
        break;
      }
      if (n % 6 == 0) {
        n /= 6;
      } else {
        n *= 2;
      }
      res++;
    }
    cout << res << endl;
  }
  return 0;
}
