#include <bits/stdc++.h>
using namespace std;
void input_File() {}
bool isPrime(long long n) {
  if (n <= 1) return false;
  for (long long i = 2; i < n; i++)
    if (n % i == 0) return false;
  return true;
}
long long sumofdigits(long long n) {
  long long c = 0;
  while (n > 0) {
    c++;
    n /= 10;
  }
  return c;
}
long long MaxSubArraySumReturn(long long a[], long long n) {
  long long sum = 0, best = 0;
  for (long long i = 0; i < n; i++) {
    sum = max(a[i], a[i] + sum);
    best = max(best, sum);
  }
  return best;
}
long long fact(long long n) {
  long long res = 1;
  for (long long i = 2; i <= n; i++) res = res * i;
  return res;
}
long long nCr(long long n, long long r) {
  return fact(n) / (fact(r) * fact(n - r));
}
bool sortinrev(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.first > b.first);
}
long long countDivisors(long long n) {
  long long cnt = 0;
  for (long long i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i)
        cnt++;
      else
        cnt = cnt + 2;
    }
  }
  return cnt;
}
long long setBitCount(long long n) {
  long long ans = 0;
  while (n) n &= n - 1, ans++;
  return ans;
}
long long power(long long a, long long b) {
  long long result = 1;
  while (b) {
    if (b & 1) {
      result *= a;
      b--;
    } else {
      a *= a;
      b >>= 1;
    }
  }
  return result;
}
vector<long long> nextLargerElementUsingStack(long long a[], long long n) {
  stack<long long> s;
  vector<long long> ans;
  ans.push_back(-1);
  s.push(a[n - 1]);
  for (long long i = n - 2; i >= 0; i--) {
    if (a[i] > s.top()) {
      while (!s.empty() and a[i] > s.top()) s.pop();
      if (s.empty())
        ans.push_back(-1);
      else
        ans.push_back(s.top());
      s.push(a[i]);
    } else if (a[i] < s.top()) {
      ans.push_back(s.top());
      s.push(a[i]);
    }
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  if (n == 1) {
    cout << 0 << "\n";
    return;
  }
  if (n == 3) {
    cout << 2 << "\n";
    return;
  }
  if (n < 6 or n % 3) {
    cout << -1 << "\n";
    return;
  }
  long long count = 0;
  while (n > 1) {
    if (n <= 1) break;
    if (n % 6 == 0)
      n /= 6, count++;
    else if ((n * 2 % 6) == 0)
      n *= 2, count++;
    else {
      cout << -1 << "\n";
      return;
    }
  }
  cout << count << "\n";
}
signed main() {
  input_File();
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long isTestCasePresent = 1;
  if (isTestCasePresent == 0) {
    long long t = 1;
    while (t--) {
      solve();
    }
  } else {
    long long t;
    cin >> t;
    while (t--) {
      solve();
    }
  }
  return 0;
}
