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