#include <bits/stdc++.h>
long long arr[1000009];
long long arr1[1000009];
const int MOD = 1e9 + 7;
using namespace std;
bool comp(string a, string b) { return a.size() < b.size(); }
int main() {
  ios_base::sync_with_stdio(false);
  std::ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    long long count = 0;
    bool flag = true;
    while (n % 6 == 0 && n >= 1) {
      n /= 6;
      count++;
    }
    while (n % 6 != 0 && n >= 1) {
      if (n == 1) {
        cout << count << '\n';
        flag = false;
        break;
      }
      n *= 2;
      count++;
      if (n == 1) {
        cout << count << '\n';
        flag = false;
        break;
      }
      if (n % 6 == 0) {
        n /= 6;
        count++;
      }
      if (n == 1) {
        cout << count << '\n';
        flag = false;
        break;
      }
    }
    if (flag == true) cout << -1 << '\n';
  }
}
