#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, min;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    for (min = 0; n % 3 == 0 && n > 1; min++) {
      if (n % 6 == 0)
        n /= 6;
      else
        n *= 2;
    }
    if (n == 1)
      cout << min << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
