#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int tt = 0; tt < t; tt++) {
    long long n;
    cin >> n;
    long long steps = 0;
    bool not_pos = false;
    if (n == 1) {
      cout << steps << endl;
    } else {
      while (n > 1) {
        if (n % 3 != 0) {
          not_pos = true;
          break;
        } else {
          if (n % 6 == 0) {
            n /= 6;
            steps++;
          } else {
            n *= 2;
            steps++;
          }
        }
      }
      if (not_pos)
        cout << -1 << endl;
      else {
        cout << steps << endl;
      }
    }
  }
  return 0;
}
