#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    char s2[100];
    int temp1 = 0, a = 0, c = 0, d = 0, b = 0;
    while (n >= 1) {
      if (n % 6 == 3) n *= 2, a++;
      s2[temp1++] = (char)(n % 6 + 48);
      n /= 6;
      if (s2[temp1 - 1] == '1')
        d++;
      else if (s2[temp1 - 1] == '0')
        0;
      else
        b++;
    }
    if (s2[temp1 - 1] == '1' && d == 1 && b == 0) {
      cout << temp1 - 1 + a << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
