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