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