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