#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, counter = 0;
    cin >> n;
    while (true) {
      if (n % 6 == 0) {
        n /= 6;
        counter++;
      } else if (n % 3 == 0) {
        n *= 2;
        counter++;
      } else {
        break;
      }
    }
    if (n == 1) {
      cout << counter << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
