#include <bits/stdc++.h>
using namespace std;
int minmove(int n) {
  if (n == 1) return 0;
  if (n == 2) return -1;
  if (n % 6 == 0) {
    int p = minmove(n / 6);
    if (p != -1)
      return (1 + p);
    else
      return -1;
  } else if ((n * 2) % 6 == 0) {
    int q = minmove(n * 2);
    if (q != -1) {
      return (1 + q);
    } else
      return -1;
  }
  return -1;
}
int main() {
  int t;
  cin >> t;
  while (t) {
    int n;
    cin >> n;
    int ans = minmove(n);
    cout << ans << endl;
    t--;
  }
}
