#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a;
  while (n--) {
    int count = 0;
    cin >> a;
    while (a % 6 == 0) {
      a = a / 6;
      count++;
    }
    while (a % 3 == 0) {
      a = a / 3;
      count = count + 2;
    }
    if (a == 1) {
      cout << count << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}
