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