#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, i, n1, n, k, k1, j;
  cin >> t;
  vector<long long> ans, m;
  for (i = 1; i <= t; i++) {
    k = 0;
    k1 = 0;
    cin >> n;
    n1 = n;
    if (n == 1) {
      ans.push_back(0);
      continue;
    }
    while (n1 > 1) {
      for (j = 3; j >= 1; j--)
        if (n1 % j == 0) {
          if (j == 3) {
            k++;
            n1 = n1 / j;
            break;
          }
          if (j == 2) k1++;
          n1 = n1 / j;
          break;
        }
      if (k == 0 or j == 1) break;
    }
    if (k == 0 or k < k1 or j == 1) {
      ans.push_back(-1);
      continue;
    }
    if (k >= k1) ans.push_back(k + k - k1);
  }
  for (i = 0; i < ans.size(); i++) cout << ans[i] << endl;
}
