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