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