#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    long long int n;
    cin >> n;
    long long int counta = 0;
    if (n == 1) {
      cout << 0 << "\n";
      continue;
    }
    long long int flag = 0;
    while (n != 1) {
      if (n % 6 == 0)
        n /= 6;
      else if (n % 3 == 0) {
        n = n * 2;
      } else {
        flag = 1;
        break;
      }
      counta++;
    }
    if (!flag)
      cout << counta << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}
