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