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