#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, x, y, n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int cnt = 0, f = 0;
    while (n >= 6) {
      if (n % 3 == 0 && n % 6 != 0) {
        cnt++;
        n = n * 2;
      } else if (n % 6 == 0) {
        cnt++;
        n = n / 6;
      } else {
        f = 1;
        break;
      }
    }
    if (n == 3)
      printf("%d\n", cnt + 2);
    else if (n == 6)
      printf("%d\n", cnt + 1);
    else if (n == 1)
      printf("%d \n", cnt);
    else
      printf("-1\n");
  }
}
