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