#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, two = 0, three = 0, moves = 0, hold;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    two = 0;
    three = 0;
    moves = 0;
    while (true) {
      hold = n;
      if (n % 2 == 0) {
        two++;
        n = n / 2;
      }
      if (n % 3 == 0) {
        three++;
        n = n / 3;
      }
      if (hold == n) break;
    }
    if (three < two || n != 1) {
      cout << -1 << endl;
    } else {
      moves += (three - two);
      moves += three;
      cout << moves << endl;
    }
  }
  return 0;
}
