#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int w;
  cin >> w;
  while (w--) {
    long long int n;
    cin >> n;
    long long int ans;
    if (n == 1) {
      cout << "0" << endl;
      continue;
    }
    if (n % 3 != 0) {
      {
        cout << "-1" << endl;
        continue;
      }
    }
    long long int count = 0, count1 = 0;
    while (1) {
      if (n % 3 == 0) {
        if (n % 2 != 0) {
          n *= 2;
          count++;
        }
        n = n / 6;
        count1++;
      } else {
        if (n == 1) {
          cout << count + count1 << endl;
          break;
        } else {
          cout << "-1" << endl;
          break;
        }
      }
    }
  }
  return (0);
}
