#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int cntThree = 0, cntTwo = 0, val = 1;
    while (n % (val * 3LL) == 0) {
      ++cntThree;
      val *= 3;
    }
    while (n % (val * 2LL) == 0) {
      ++cntTwo;
      val *= 2;
    }
    if (val != n || (cntTwo > cntThree))
      puts("-1");
    else
      printf("%d\n", cntThree + (cntThree - cntTwo));
  }
  return 0;
}
