#include <bits/stdc++.h>
int main() {
  long long int t;
  scanf("%lld\n", &t);
  while (t--) {
    long long int n, a = 0;
    scanf("%lld\n", &n);
    while (1) {
      while (n % 6 == 0) {
        n = n / 6;
        a++;
      }
      if (n % 3 == 0) {
        n = n * 2;
        a++;
      }
      if (n == 1) {
        printf("%lld\n", a);
        break;
      } else if (n % 6 != 0) {
        printf("-1\n");
        break;
      }
    }
  }
  return 0;
}
