#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long cnt = 0;
  while (n != 1) {
    if (n % 6 == 2 || n % 6 == 5) {
      cnt = -1;
      break;
    } else if (n % 6 == 0) {
      n /= 6;
      cnt++;
    } else {
      n *= 2;
      cnt++;
    }
  }
  cout << cnt << endl;
}
int main() {
  long long int t;
  cin >> t;
  for (long long int tc = 1; tc <= t; tc++) solve();
  return 0;
}
