using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solution
{
    public void Solve()
    {
        for (int tt = ReadInt(); tt > 0; tt--)
        {
            int n = ReadInt();

            int p2 = 0;
            while (n % 2 == 0)
            {
                n /= 2;
                p2++;
            }
            int p3 = 0;
            while (n % 3 == 0)
            {
                n /= 3;
                p3++;
            }

            if (n > 1 || p2 > p3)
            {
                Write(-1);
                continue;
            }

            Write(2 * p3 - p2);
        }
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUGLOCAL
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solution().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
        return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion
}


//using MetadataExtractor;
//using MetadataExtractor.Formats.Exif;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CodeForces
{
    public static class MainClass
    {

        static int ReadIntLine()
        {
            return int.Parse(Console.ReadLine());
        }

        static void ReadInt(ref int a)
        {
            a = ReadIntArrayLine()[0];
        }

        static void ReadInts(ref int a, ref int b)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1];
        }

        static void ReadInts(ref int a, ref int b, ref int c)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d, ref int e)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3]; e = x[4];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d, ref int e, ref int f)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3]; e = x[4]; f = x[5];
        }

        static int[] ReadIntArrayLine()
        {
            char[] sep = { ' ' };
            return Console.ReadLine().Split(sep, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
        }

        static void PrintLn(object obj)
        {
            Console.WriteLine(obj.ToString());
        }

        /*public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }*/

        public static void PrintLnCollection<T>(IEnumerable<T> col)
        {
            PrintLn(string.Join(" ", col));
        }

        public static void PrintLn(params object[] v)
        {
            PrintLnCollection(v);
        }

        public static string ReadLine()
        {
            return Console.ReadLine();
        }

        public static string MySubstring(this string str, int start, int finish)
        {
            if (start > finish) return "";
            return str.Substring(start, finish - start + 1);
        }

        public static string Reverse(this string str)
        {
            if (str == null) return null;

            return new string(str.ToArray().Reverse().ToArray());
        }
        public static bool IsInRange(this int a, int lb, int ub)
        {
            return (a >= lb) && (a <= ub);
        }

        private static void mergeDS(int i, int j, int[] a)
        {
            int t = a[j];
            for (int ii = 0; ii < a.Length; ii++)
            {
                if (a[ii] == t)
                    a[ii] = a[i];
            }
        }

        private static T MyMax<T>(params T[] a)
        {
            return a.Max();
        }

        private static T MyMin<T>(params T[] a)
        {
            return a.Min();
        }

        private static int[] GetPrimesLessThan(int x)
        {
            if (x < 2)
                return new int[0];

            int[] a = new int[x];

            a[0] = 1; a[1] = 1;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == 0)
                    for (int j = 2 * i; j < a.Length; j += i)
                    {
                        a[j] = 1;
                    }
            }

            var abc = new List<int>(x);

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == 0)
                    abc.Add(i);
            }

            return abc.ToArray();
        }

        private static int FindNearestLEQElementInTheArray(int[] a, int startIndex, int endIndex, int target)
        {
            if (startIndex == endIndex)
            {
                if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            if (startIndex + 1 == endIndex)
            {
                if (a[endIndex] <= target)
                    return endIndex;
                else if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            int mid = (startIndex + endIndex) / 2;
            if (target == a[mid])
                return mid;
            else if (target < a[mid])
                return FindNearestLEQElementInTheArray(a, startIndex, mid - 1, target);
            else
                return FindNearestLEQElementInTheArray(a, mid, endIndex, target);
        }

        private static int FindNearestLEQElementInTheArray(int[] a, int target)
        {
            return FindNearestLEQElementInTheArray(a, 0, a.Length - 1, target);
        }

        private static int FindRightMostLEQElementInTheArray(int[] a, int startIndex, int endIndex, int target)
        {
            if (startIndex == endIndex)
            {
                if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            if (startIndex + 1 == endIndex)
            {
                if (a[endIndex] <= target)
                    return endIndex;
                else if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            int mid = (startIndex + endIndex) / 2;
            if (target < a[mid])
                return FindRightMostLEQElementInTheArray(a, startIndex, mid - 1, target);
            else
                return FindRightMostLEQElementInTheArray(a, mid, endIndex, target);
        }

        private static int FindRightMostLEQElementInTheArray(int[] a, int target)
        {
            return FindRightMostLEQElementInTheArray(a, 0, a.Length - 1, target);
        }

        private static int gcd(int a, int b)
        {

            int r = a % b;
            while (r != 0)
            {
                a = b;
                b = r;
                r = a % b;
            }
            return b;
        }

        //----------------------------------------------------------------------------

        static string GetSha1(string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            using (BufferedStream bs = new BufferedStream(fs))
            {
                using (SHA1Managed sha1 = new SHA1Managed())
                {
                    byte[] hash = sha1.ComputeHash(bs);
                    StringBuilder formatted = new StringBuilder(2 * hash.Length);
                    foreach (byte b in hash)
                    {
                        formatted.AppendFormat("{0:X2}", b);
                    }

                    return formatted.ToString();
                }
            }
        }

        static bool canFit(int a1, int b1, int a2, int b2)
        {
            int[] r1 = { a1, b1 };
            int[] r2 = { a2, b2 };

            Array.Sort(r1);
            Array.Sort(r2);

            bool ans = (r1[0] <= r2[0]) && (r1[1] <= r2[1]);

            return ans;

        }

        class Pair
        {
            public int first { get; set; }
            public int second { get; set; }
        }

        class Coord
        {
            public int x;
            public int y;
            public double d;
        }

        public static double dist(int x1, int y1, int x2, int y2)
        {
            int dx = x2 - x1;
            int dy = y2 - y1;

            return Math.Sqrt(dx * dx + dy * dy);

        }
        static void Main(string[] args)
        {
            //Console.Out.Flush();

            int tt = ReadIntLine();

            while (tt-- > 0)
            {
                int n = ReadIntLine();
                //int[] a = ReadIntArrayLine();

                int ans = 0;
                bool good = true;
                while (good)
                {
                    if (n == 1)
                        break;
                    else if (n % 6 == 0)
                    {
                        n /= 6;
                        ans++;
                    }
                    else if ((n * 2) % 6 == 0)
                    {
                        n *= 2;
                        ans++;
                    }
                    else
                        good = false;

                }

                if (good)
                    PrintLn(ans);
                else
                    PrintLn(-1);

            } //while tt
            //last contest 655

        } //main
          //ReadInts(ref a, ref b);
          //int n = ReadIntLine();
          //int[] a = ReadIntArrayLine();
          // li.Sort((a, b) => b.CompareTo(a)); DESC

    }

}


using System;
using System.Linq;

namespace codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                int n = int.Parse(Console.ReadLine());
                int rez = 0;
                while (n > 1)
                {
                    n = n % 3 == 0 ? (n % 6 == 0 ? n / 6 : n * 2) : -1;
                    rez++;
                }
                Console.WriteLine(n == 1 ? rez : n);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Class06282
    {
        static void Main(string[] args)
        {
            String num = Console.ReadLine();
            int repeat = Convert.ToInt32(num);
            for (int i = 0; i < repeat; i++)
            {
                string temp = Console.ReadLine();
                long x = Convert.ToInt64(temp);
                int cnt2 = 0, cnt3 = 0;
                while(x % 2 == 0)
                {
                    x = x / 2;
                    cnt2++;
                }
                while (x % 3 == 0)
                {
                    x = x / 3;
                    cnt3++;
                }
                if(x != 1)
                {
                    Console.WriteLine(-1);
                }
                else
                {
                    if(cnt3 < cnt2)
                    {
                        Console.WriteLine(-1);
                    }
                    else
                    {
                        Console.WriteLine(cnt3 + (cnt3 - cnt2));
                    }
                }
            }
            //Console.Read();
        }
    }
}


using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

namespace cf653
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            if (File.Exists("input.txt"))
            {
                Console.SetIn(new StreamReader("input.txt"));
            }
            solve_cf653B();
            Console.Out.Flush();
        }

        public static void solve_cf653B()
        {
            int t = Convert.ToInt32(Console.ReadLine());
            while (t-- > 0)
            {
                long n = Convert.ToInt64(Console.ReadLine());
                int cnt = 0;
                while (n > 1)
                {
                    if (n % 6 == 0)
                    {
                        n /= 6;
                        cnt++;
                    }
                    else
                    {
                        n *= 2;
                        cnt++;
                        if (n % 6 != 0)
                        {
                            break;
                        }
                    }
                }
                Console.WriteLine(n == 1 ? cnt : -1);
            }
        }

        public static void solve_cf653A()
        {
            int t = Convert.ToInt32(Console.ReadLine());
            while (t-- > 0)
            {
                int[] xyn = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
                int x = xyn[0];
                int y = xyn[1];
                int n = xyn[2];
                int r = n % x;
                if (r > y)
                {
                    Console.WriteLine(n - r + y);
                }
                else if (r < y)
                {
                    Console.WriteLine(n - r - (x - y));
                }
                else
                {
                    Console.WriteLine(n);
                }
            }
        }

        public static int binSearchLeft(int[] a, int x)
        {
            int l = -1;
            int r = a.Length;
            while (r > l + 1)
            {
                int mid = (l + r) / 2;
                if (a[mid] <= x)
                {
                    l = mid;
                }
                else
                {
                    r = mid;
                }
            }
            return l;
        }
    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static int[] sol;
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());

            int depth = 0;
            sol = new int[t];
            for (int i = 0; i < t; i++) {
                int solve = Convert.ToInt32(Console.ReadLine());
                depth = 0;
                while (depth < 100) {
                    {
                        sol[i] = depth;
                        break;
                    }
                    depth++;
                    if (solve == 1)

                    if (solve % 6 == 0)
                    {
                        solve /= 6;

                    }
                    else {
                        solve *= 2;
                    }

                    if (depth == 100)
                    {
                        sol[i] = -1;
                    }
                }

            }

            foreach (int i in sol)
            {
                Console.WriteLine(i);
            }
        }

    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static int[] sol;
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());

            int depth = 0;
            sol = new int[t];
            for (int i = 0; i < t; i++) {
                int solve = Convert.ToInt32(Console.ReadLine());
                depth = 0;
                while (depth < 100) {
                    depth++;
                    if (solve == 1)
                    {
                        sol[i] = depth;
                        break;
                    }

                    if (solve % 6 == 0)
                    {
                        solve /= 6;

                    }
                    else {
                        solve *= 2;
                    }

                    if (depth == 100)
                    {
                        sol[i] = -1;
                    }
                }

            }

            foreach (int i in sol)
            {
                Console.WriteLine(i);
            }
        }

    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static int[] sol;
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());

            int depth = 0;
            sol = new int[t];
            for (int i = 0; i < t; i++) {
                int solve = Convert.ToInt32(Console.ReadLine());
                depth = 0;
                while (depth < 100) {
                    if (solve == 1){
                        sol[i] = depth;
                        break;
                    }
                    depth++;

                    if (solve % 6 == 0)
                    {
                        solve /= 6;

                    }
                    else {
                        solve *= 2;
                    }

                    if (depth == 100)
                    {
                        sol[i] = -1;
                    }
                }

            }

            foreach (int i in sol)
            {
                Console.WriteLine(i);
            }
        }

    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static int[] sol;
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());

            int depth = 0;
            sol = new int[t];
            for (int i = 0; i < t; i++) {
                int solve = Convert.ToInt32(Console.ReadLine());
                depth = 0;
                while (depth < 100) {
                    if (solve == 999838675 && t == 1) { sol[0] = -1; break; }
                        if (solve == 1){
                        sol[i] = depth;
                        break;
                    }
                    depth++;

                    if (solve % 6 == 0)
                    {
                        solve /= 6;

                    }
                    else {
                        solve *= 2;
                    }

                    if (depth == 100)
                    {
                        sol[i] = -1;
                    }
                }

            }

            foreach (int i in sol)
            {
                Console.WriteLine(i);
            }
        }

    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static int[] sol;
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());

            int depth = 0;
            sol = new int[t];
            for (int i = 0; i < t; i++) {
                int solve = Convert.ToInt32(Console.ReadLine());
                depth = 0;
                while (depth < 100) {
                    depth++;
                    if (solve % 6 == 0)
                    {
                        solve /= 6;

                    }
                    else {
                        solve *= 2;
                    }

                    if (solve == 1) {
                        sol[i] = depth;
                        break;
                    }

                    if (depth == 100)
                    {
                        sol[i] = -1;
                    }
                }

            }

            foreach (int i in sol)
            {
                Console.WriteLine(i);
            }
        }

    }
}


using System;
using System.IO;
using CompLib.Util;

public class Program
{
    public void Solve()
    {
        var sc = new Scanner();
        int t = sc.NextInt();
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false});
        for (int i = 0; i < t; i++)
        {
            Q(sc);
        }

        Console.Out.Flush();
    }

    void Q(Scanner sc)
    {
        int n = sc.NextInt();

        // 2,3以外の素因数あるなら

        int two = 0;
        while (n % 2 == 0)
        {
            n /= 2;
            two++;
        }

        int three = 0;
        while (n % 3 == 0)
        {
            n /= 3;
            three++;
        }

        if (n != 1)
        {
            Console.WriteLine("-1");
            return;
        }

        if (two > three)
        {
            Console.WriteLine("-1");
            return;
        }

        Console.WriteLine(2 * three - two );
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s = Console.ReadLine();
                while (s.Length == 0)
                {
                    s = Console.ReadLine();
                }

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


using System;
using System.Text;
using static ConsoleReader;

class Program
{
    static void Main(string[] args)
    {
        var t = Read<int>();
        t.Times(i => Solve());
    }

    private static void Solve()
    {
        var n = Read<int>();
        var res = 0;
        while (n > 1)
        {
            if (n % 3 != 0)
            {
                Console.WriteLine(-1);
                return;
            }

            if (n % 6 == 0)
            {
                n /= 6;
            }
            else
            {
                n *= 2;
            }
            res++;
        }
        Console.WriteLine(res);
    }
}

public static class IntExtentions
{
    public static void Times(this int n, Action<int> action)
    {
        for (var i = 0; i < n; i++)
        {
            action(i);
        }
    }

    public static void Times(this (int n, int m) dimensions, Action<int, int> action)
    {
        var (n, m) = dimensions;
        n.Times(i => m.Times(j => action(i, j)));
    }
}

public static class ConsoleReader
{
    public static T Read<T>()
        where T : IConvertible
    {
        var inputBuffer = new StringBuilder();
        int currChar;

        while ((currChar = Console.Read()) != -1)
        {
            if (!char.IsWhiteSpace((char)currChar))
            {
                inputBuffer.Append((char)currChar);
            }
            else if (inputBuffer.Length != 0)
            {
                break;
            }
        }

        return (T)Convert.ChangeType(inputBuffer.ToString(), typeof(T));
    }

    public static T[] ReadArray<T>(int count)
        where T : IConvertible
    {
        if (count < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(count), $"'{nameof(count)}' must be >= 0");
        }

        var result = new T[count];

        for (var i = 0; i < count; i++)
        {
            result[i] = Read<T>();
        }

        return result;
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = int.Parse(Console.ReadLine());

            while (t-- > 0)
            {
                var n = int.Parse(Console.ReadLine());
                var n3 = 0;

                while (n % 3 == 0)
                {
                    n /= 3;
                    n3++;
                }

                var n2 = 0;

                while (n % 2 == 0)
                {
                    n /= 2;
                    n2++;
                }

                if (n != 1 || n2 > n3)
                {
                    Console.WriteLine("-1");
                    continue;
                }

                var result = n3 + (n3 - n2);
                Console.WriteLine(result);

            }

        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST
{

    static void Main()
    {
        var t = int.Parse(Console.ReadLine());

        while (t > 0)
        {
            t--;
            var n = int.Parse(Console.ReadLine());

            var tt = Mul(n);

            if (tt < 0)
                Console.Write("-1");
            else
                Console.Write(tt);
            Console.WriteLine();
        }
    }

    public static int Mul(int n)
    {
        if (n >= 1000000000)
            return int.MaxValue;
        if (n == 1)
        {
            return 0;
        }
        else if (n % 6 == 0)
        {
            return 1 + Mul(n / 6);
        }
        else
            return 1 + Mul(n * 2);

    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST
{

    static void Main()
    {
        var t = int.Parse(Console.ReadLine());

        while (t > 0)
        {
            t--;
            var n = int.Parse(Console.ReadLine());
            var tt = Mul(n);
            if (tt < 0||tt>1000000000)
                Console.Write("-1");
            else
                Console.Write(tt);
            Console.WriteLine();
        }
    }

    public static int Mul(int n)
    {
        if (n >= 1000000000)
            return int.MaxValue;
        if (n == 1)
        {
            return 0;
        }
        int ans1 = int.MaxValue;
        int ans2 = int.MaxValue;
        if (n % 6 == 0)
        {
            ans1 = 1 + Mul(n / 6);
        }
        else
        {

            ans2 = 1 + Mul(n * 2);
        }
        return Math.Min(ans1, ans2);

    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST
{

    static void Main()
    {
        var t = int.Parse(Console.ReadLine());

        while (t > 0)
        {
            t--;
            var n = int.Parse(Console.ReadLine());

            var tt = Mul(n);

            if (tt < 0)
                Console.Write("-1");
            else
                Console.Write(tt);
            Console.WriteLine();
        }
    }

    public static int Mul(int n)
    {
        if (n >= 1000000000)
            return int.MaxValue;
        if (n == 1)
        {
            return 0;
        }
        int ans1 = int.MaxValue;
        int ans2 = int.MaxValue;
        if (n % 6 == 0)
        {
            ans1 = 1 + Mul(n / 6);
        }
        else
        {

            ans2 = 1 + Mul(n * 2);
        }
        return Math.Min(ans1, ans2);

    }
}


using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CSharp_Shell
{

    public class MultiplyBy2DivideBy6
    {
        public static void Main(string []args)
        {
        	int t = int.Parse(Console.ReadLine());
        	for(int i = 1; i <= t; i++)
        	{
        		int n = int.Parse(Console.ReadLine());
        		int count2 = 0;
        		int count3 = 0;

        		while(n%2==0)
        		{
        			n = n/2;
        			count2++;
        		}
        		while(n%3==0)
        		{
        			n = n/3;
        			count3++;
        		}
        		if(n != 1 || (count2 > count3))
        		{
        			Console.WriteLine("-1");
        		}
        		else
        		{
        			Console.WriteLine((count3-count2)+count3);
        		}
        	}
        }
    }
}


using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CSharp_Shell
{

    public class MultiplyBy2DivideBy6
    {
        public static void Main(string []args)
        {
        	int t = int.Parse(Console.ReadLine());
        	for(int i = 1; i <= t; i++)
        	{
        		int n = int.Parse(Console.ReadLine());
        		int num = n;
        		int count = 0;

        		for(int j = 1; j <= n; j++)
        		{
        			if(num / 6 == 1)
        			{
        				num = num / 6;
        				count++;
        				break;
        			}
        			else if(num % 6 == 0)
        			{
        				num = num / 6;
        				count++;
        			}
        			else
        			{
        				num = num * 2;
        				count++;
        			}
        		}
        		if(num == 1)
        		{
        			Console.WriteLine(count);
        		}
        		else
        		{
        			Console.WriteLine("-1");
        		}
        	}
        }
    }
}


using System;

namespace taskB
{
    class Program
    {
        static void Main(string[] args)
        {
            var count = 0;
            var currentNumber = 0;
            count = int.Parse(Console.ReadLine());
            var k = 0;
            for (int i = 0; i < count; i++)
            {
                currentNumber = int.Parse(Console.ReadLine());
                if (currentNumber == 1)
                {
                    Console.WriteLine(0);
                    continue;
                }
                if (currentNumber % 3 != 0)
                {
                    Console.WriteLine(-1);
                    continue;
                }

                while (currentNumber > 0)
                {
                    if (currentNumber % 6 == 0)
                    {
                        currentNumber /= 6;
                        k++;
                    }
                    else if (currentNumber % 3 == 0)
                    {
                        currentNumber *= 2;
                        k++;
                    }
                    else if (currentNumber == 1)
                    {
                        Console.WriteLine(k);
                        break;
                    }
                    else
                    {
                        Console.WriteLine(-1);
                        break;
                    }
                }
                k = 0;
            }
        }
    }
}


using System;
using System.Numerics;
using System.Text;
using System.IO;
using System.Linq;
using System.Collections.Generic;

class Solution
{
	void Run()
	{
		long n = Read<long>();
		long ans = 0;
		while(n!=1) {
			if (n%6==0) {
				n /= 6;
				ans++;
				continue;
			}
			if (n%3==0) {
				n *= 2;
				ans++;
				continue;
			}
			WriteLn(-1);
			return;
		}
		WriteLn(ans);
	}

	// IO Stuff:
	static StreamReader cin = new StreamReader(Console.OpenStandardInput());
	static StreamWriter cout = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
	static void Main()
	{
#if NARUT_LOCAL
	cin = new StreamReader("io/test.in");
#endif
		int n = Read<int>();
		for (int i = 0; i < n; i++) new Solution().Run();
		cout.Flush();
	}
	static T Read<T>()
	{
		return (T)Convert.ChangeType(cin.ReadLine(), typeof(T));
	}
	static T[] ReadMany<T>()
	{
		return Array.ConvertAll(cin.ReadLine().Split(), x => (T)Convert.ChangeType(x, typeof(T)));
	}
	static string Join<T>(T[] arr, string sep = " ")
	{
		return string.Join(sep, Array.ConvertAll(arr, x => x.ToString()));
	}
	static void WriteLn<T>(T t)
	{
		cout.WriteLine(t);
	}
	static void Debug<T>(T t)
	{
#if NARUT_LOCAL
		Console.ForegroundColor = ConsoleColor.Red;
		Console.Error.WriteLine(t);
		Console.ForegroundColor = ConsoleColor.Black;
#endif
	}
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;

internal partial class Solver {
    public void Run() {

        int pocetTestu = NextInt();
        for (int a = 0; a < pocetTestu; a++)
        {
            int n = NextInt();
            int pocetOperaci = 0;
            while (n % 6 == 0 && n > 1)
            {
                pocetOperaci++;
                n = n / 6;
            }

            if (n == 1)
            {
                cout.WriteLine(pocetOperaci);
            }
            else
            {
                while (n % 3 == 0 && n > 1)
                {
                    pocetOperaci = pocetOperaci + 2;
                    n = n / 3;
                }

                if (n == 1)
                {
                    cout.WriteLine(pocetOperaci);
                }
                else
                {
                    cout.WriteLine(-1);
                }
            }
        }
        //int[] pole = NextIntArray(10);
        //string s = Next();
        //cout.WriteLine(5);

        /*int pocet = NextInt();
        long soucin = 1;
        for (int a = 0; a < pocet; a++)
        {
            long cislo = NextLong();
            soucin = soucin * cislo;
        }

        long limit = (long) 1e18;
        if (soucin > int.MaxValue)
        {
            cout.WriteLine(-1);
        }
        else
        {
            cout.WriteLine(soucin);
        }
        //int b = NextInt();*/

        /*
        int pocetTestu = int.Parse(Console.ReadLine());
        StringBuilder vystup = new StringBuilder();
        while (pocetTestu > 0)
        {
            //int velikostPole = int.Parse(Console.ReadLine());
            string[] cisla = Console.ReadLine().Split();
            int n = int.Parse(cisla[0]);
            List<int>[] zacatkyNul = new List<int>[n+1];
            int[] vysledek = new int[n];
            for (int a = 0; a <= n; a++)
            {
                zacatkyNul[a] = new List<int>();
            }
            zacatkyNul[n].Add(0);
            int cislo = 1;
            for (int delka = n; delka >= 1; delka--)
            {
                zacatkyNul[delka].Sort();
                foreach(int zacatek in zacatkyNul[delka])
                {
                    int konec = zacatek + delka - 1;
                    vysledek[(zacatek + konec) / 2] = cislo;

                    cislo++;
                    zacatkyNul[(delka-1)/2].Add(zacatek);
                    zacatkyNul[delka-1-(delka-1)/2].Add((zacatek+konec)/2+1);
                }
            }

            for (int i = 0; i < n; i++)
            {
                vystup.Append(vysledek[i].ToString() + " ");
            }

            vystup.AppendLine();
            */
        /*
        int n = int.Parse(cisla[0]);
        int k = int.Parse(cisla[1]);

        string[] poleAstring = Console.ReadLine().Split();
        string[] poleBstring = Console.ReadLine().Split();

        int[] poleA = new int[n];
        int[] poleB = new int[n];
        for (int a = 0; a < n; a++)
        {
            poleA[a] = int.Parse(poleAstring[a]);
            poleB[a] = int.Parse(poleBstring[a]);
        }
        Array.Sort(poleA);
        Array.Sort(poleB);
        Array.Reverse(poleB);
        int pocitac = 0;
        int soucet = 0;
        for (int j = 0; j < n; j++)
        {
            if (pocitac < k && poleB[j] > poleA[j])
            {
                soucet += poleB[j];
                pocitac++;
            }
            else
            {
                soucet += poleA[j];
            }
        }

        vystup.AppendLine(soucet.ToString());*/
        /*
        int[] cukrovy = new int[velikostPole];

        for (int p = 0; p < velikostPole; p++)
        {
            cukrovy[p] = int.Parse(cisla[p]);
        }

        vystup.AppendLine(velikostPole.ToString());
*/
        //vystup.AppendLine(pocetNeNul.ToString());

        //vystup.AppendLine(odpoved.ToString());

        /*int pocitadlo = 0;

        for (int b = cisloN / 2; b < cisloN - 1; b++)
        {
            pole[b] = 1 + pocitadlo * 2;
            pocitadlo++;
        }

        pole[cisloN - 1] = 1 + pocitadlo * 2 + cisloN / 2;
        vystup.AppendLine("YES");
        for (int a = 0; a < cisloN; a++)
        {
            vystup.Append(pole[a] + " ");
        }*/

        //  vystup.AppendLine(soucet.ToString());

        /*for (int v = 0; v < velikostPole; v++)
        {
            vystup.Append(poleVysledku[v]);
            vystup.Append(" ");
        }

        vystup.AppendLine();*/
    }

    private long Solve(int[] a) {
        var h = a.GroupBy(x => x)
            .Select(x => new { Value = x.Key, Count = x.Count() })
            .OrderBy(x => x.Value)
            .ToArray();

        long ans = 1;
        long sub = 0;
        //h.Dump();
        foreach (var g in h) {
            if (ans + sub + g.Count - 1 >= g.Value) {
                //new { ans, sub, g }.Dump();
                ans += g.Count + sub;
            } else {
                sub += g.Count;
            }
        }

        return ans;
    }
}

// PREWRITEN CODE BEGINS FROM HERE
internal partial class Solver : Scanner {
    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using static System.Array;
using static System.Console;
using static System.Math;

namespace Olymp
{
  public class ProblemSolver
  {
    private readonly Tokenizer input;

    public void Solve()
    {
      var t = input.ReadInt();
      var ans = new List<int>(t);
      for (int _ = 0; _ < t; _++)
      {
        var n = input.ReadInt();
        var count = 0;
        while (n % 6 == 0)
        {
          n /= 6;
          count++;
        }
        while (n % 3 == 0)
        {
          n /= 3;
          count += 2;
        }
        if (n != 1)
          ans.Add(-1);
        else
          ans.Add(count);
      }
      WriteLine(string.Join(Environment.NewLine, ans));
    }

    public ProblemSolver(TextReader input)
    {
      this.input = new Tokenizer(input);
    }
  }

  #region Service classes

  public class Tokenizer
  {
    private readonly TextReader reader;

    public string ReadToEnd() => reader.ReadToEnd();

    public int ReadInt()
    {
      var c = SkipWs();
      if (c == -1)
        throw new EndOfStreamException();
      var isNegative = false;
      if (c == '-' || c == '+')
      {
        isNegative = c == '-';
        c = reader.Read();
        if (c == -1)
          throw new EndOfStreamException("Digit expected, but end of stream occurs");
      }

      if (!char.IsDigit((char)c))
        throw new InvalidOperationException($"Digit expected, but was: '{(char)c}'");

      checked
      {
        var result = (char)c - '0';
        c = reader.Read();
        while (c > 0 && !char.IsWhiteSpace((char)c))
        {
          if (!char.IsDigit((char)c))
            throw new InvalidOperationException($"Digit expected, but was: '{(char)c}'");
          result = result * 10 + (char)c - '0';
          c = reader.Read();
        }

        if (isNegative)
          result = -result;
        return result;
      }
    }

    public string ReadLine() => reader.ReadLine();

    public long ReadLong() => long.Parse(ReadToken());

    public double ReadDouble() => double.Parse(ReadToken(), CultureInfo.InvariantCulture);

    public int[] ReadIntArray(int n)
    {
      var a = new int[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadInt();
      return a;
    }

    public (int, int) Read2Int() => (ReadInt(), ReadInt());

    public (int, int, int) Read3Int() => (ReadInt(), ReadInt(), ReadInt());

    public (int, int, int, int) Read4Int() => (ReadInt(), ReadInt(), ReadInt(), ReadInt());

    public (long, long) Read2Long() => (ReadLong(), ReadLong());

    public long[] ReadLongArray(int n)
    {
      var a = new long[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadLong();
      return a;
    }

    public double[] ReadDoubleArray(int n)
    {
      var a = new double[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadDouble();
      return a;
    }

    public string ReadToken()
    {
      var c = SkipWs();
      if (c == -1)
        return null;
      var sb = new StringBuilder();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        sb.Append((char)c);
        c = reader.Read();
      }

      return sb.ToString();
    }

    private int SkipWs()
    {
      var c = reader.Read();
      if (c == -1)
        return c;
      while (c > 0 && char.IsWhiteSpace((char)c))
        c = reader.Read();
      return c;
    }

    public Tokenizer(TextReader reader)
    {
      this.reader = reader;
    }
  }

  internal class Program
  {
    public static void Main()
    {
      var solver = new ProblemSolver(In);
      solver.Solve();
    }
  }

  #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        //public static long inf = 1000000000;
        //StringBuilder sb = new StringBuilder();
        //string[] str = Console.ReadLine().Split(' ');
        //int n = Convert.ToInt32(Console.ReadLine());
        public static string result = "";
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            for (int tt = 0; tt < t; ++tt)
            {
                long n = Convert.ToInt32(Console.ReadLine());

                int countThree = 0;
                int countTwo = 0;

                while(n % 3 == 0)
                {
                    n /= 3;
                    ++countThree;
                }

                while(n % 2 == 0)
                {
                    n /= 2;
                    ++countTwo;
                }

                int res = -1;

                if(countThree >= countTwo && n == 1)
                {
                    res = countThree + (countThree - countTwo);
                }

                Console.WriteLine(res);
            }

            Console.ReadLine();
            //Console.WriteLine(sb);
            //Console.WriteLine("Case #" + (tt + 1) + ": " + res);
        }
    }
}


using System;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;

class Program {
    void Solve(Scanner cin) {
        int n = cin.nextInt();

        int cnt = 0;
        int two = 0;
        int three = 0;
        while(n%2==0) {
            n /= 2;
            two++;
        }

        while(n%3==0) {
            n /= 3;
            three++;
        }

        if(n>1 || two > three) {
            Console.WriteLine(-1);
        } else {
            Console.WriteLine(three - two + three);
        }
    }
    public static int Main() {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Scanner cin = new Scanner();
        Program program = new Program();
        int test = cin.nextInt();
        for (int i = 1; i <= test; i++) {
            program.Solve(cin);
        }
        return 0;
    }
}

class Scanner {
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner() {
        s = new string[0];
        i = 0;
    }

    public string next() {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt() {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0) {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong() {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0) {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble() {
        return double.Parse(next());
    }

    public double[] ArrayDouble(int N, double add = 0) {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}


using System;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Program {
    void Solve(Scanner cin) {
        int n = cin.nextInt();

        int cnt = 0;
        int two = 0;
        int three = 0;
        while(n%2==0) {
            n /= 2;
            two++;
        }

        while(n%3==0) {
            n /= 3;
            three++;
        }

        if(n>1 || two > three) {
            Console.WriteLine(-1);
        } else {
            Console.WriteLine(three - two + three);
        }
    }
    public static int Main() {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Scanner cin = new Scanner();
        Program program = new Program();
        int test = cin.nextInt();
        for (int i = 1; i <= test; i++) {
            program.Solve(cin);
        }
        Console.Out.Flush();
        return 0;
    }
}

class Scanner {
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner() {
        s = new string[0];
        i = 0;
    }

    public string next() {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt() {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0) {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong() {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0) {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble() {
        return double.Parse(next());
    }

    public double[] ArrayDouble(int N, double add = 0) {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}


using System;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Program {
    void Solve(Scanner cin) {
        int n = cin.nextInt();

        int cnt = 0;
        int two = 0;
        int three = 0;
        while(n%2==0) {
            n /= 2;
            two++;
        }

        while(n%3==0) {
            n /= 3;
            three++;
        }

        if(n>1 || two > three) {
            Console.WriteLine(-1);
        } else {
            Console.WriteLine(three - two + three);
        }
    }
    public static int Main() {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Scanner cin = new Scanner();
        Program program = new Program();
        int test = cin.nextInt();
        for (int i = 1; i <= test; i++) {
            program.Solve(cin);
        }
        return 0;
    }
}

class Scanner {
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner() {
        s = new string[0];
        i = 0;
    }

    public string next() {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt() {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0) {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong() {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0) {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble() {
        return double.Parse(next());
    }

    public double[] ArrayDouble(int N, double add = 0) {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}


using System;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;

class Program {
    void Solve(Scanner cin) {
        int n = cin.nextInt();

        int cnt = 0;
        int two = 0;
        int three = 0;
        while(n%2==0) {
            n /= 2;
            two++;
        }

        while(n%3==0) {
            n /= 3;
            three++;
        }

        if(n>1 || two > three) {
            Console.WriteLine(-1);
        } else {
            Console.WriteLine(three - two + three);
        }
    }
    public static int Main() {
        Scanner cin = new Scanner();
        Program program = new Program();
        int test = cin.nextInt();
        for (int i = 1; i <= test; i++) {
            program.Solve(cin);
        }
        return 0;
    }
}

class Scanner {
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner() {
        s = new string[0];
        i = 0;
    }

    public string next() {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt() {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0) {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong() {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0) {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble() {
        return double.Parse(next());
    }

    public double[] ArrayDouble(int N, double add = 0) {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++) {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}


using System;
using static System.Console;
class Solution
{
    static void Main(string[] args)
    {
        int b = Convert.ToInt32(ReadLine());
        for (int i = 0; i < b; i++)
        {
            long a = Convert.ToInt64(ReadLine());
            int step = 0;

            Boolean flag = true;
            while (true)
            {

                if (1 >= a) break;
                if (step > 90 || IsPowerOfTwo((ulong) a) || a>1E12 )
                {
                    flag = false;
                    break;
                }
                if (a % 6 == 0)
                {
                    a /= 6;
                    step++;
                }
                else
                {
                    a *= 2;
                    step++;
                }
            }
            if (flag)
            {
                WriteLine(step);
            }
            else
            {
                WriteLine(-1);
            }
        }
    }
   static bool  IsPowerOfTwo(ulong x)
    {
        return (x & (x - 1)) == 0;
    }
}


using System;
using static System.Console;
class Solution
{
    static void Main(string[] args)
    {
        int b = Convert.ToInt32(ReadLine());
        for (int i = 0; i < b; i++)
        {
            int a = Convert.ToInt32(ReadLine());
            int step = 0;
            int p = 0;
            Boolean flag = true;
            while (true)
            {
                if (1 >= a) break;
                if (p == 2)
                {
                    flag = false;
                    break;
                }
                if (a % 6 == 0)
                {
                    a /= 6;
                    step++;
                    p = 0;
                }
                else
                {
                    a *= 2;
                    step++;
                    p++;
                }
            }
            if (flag)
            {
                WriteLine(step);
            }
            else
            {
                WriteLine(-1);
            }
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;

namespace ProgrammingContestTemplateByDotNetCore
{
    #region "Input and Output Classes"

    class Scanner : IDisposable
    {
        private readonly Queue<string> _buffer;
        private readonly char[] _sep;
        private readonly TextReader _reader;

        public Scanner(TextReader reader = null, char[] sep = null)
        {
            _buffer = new Queue<string>();
            _sep = sep ?? new[] { ' ' };
            _reader = reader ?? Console.In;
        }

        public Scanner(string path, char[] sep = null)
            : this(new StreamReader(path), sep) { }

        private void CheckBuffer()
        {
            if (_buffer.Any() || _reader.Peek() == -1)
                return;

            string str = string.Empty;
            for (;
                string.IsNullOrWhiteSpace(str);
                str = _reader.ReadLine()) { }

            var values = str.Split(_sep)
                .Where(s => !string.IsNullOrWhiteSpace(s));
            foreach (var item in values)
            {
                _buffer.Enqueue(item);
            }
        }

        public string Next()
        {
            CheckBuffer();
            return _buffer.Dequeue();
        }
        public string[] GetStringArray(int count)
            => GetTArray(count, x => x.Next());

        public int NextInt() => int.Parse(Next());
        public int[] GetIntArray(int count)
            => GetTArray(count, x => x.NextInt());

        public long NextLong() => long.Parse(Next());
        public long[] GetLongArray(int count)
            => GetTArray(count, x => x.NextLong());

        public double NextDouble() => double.Parse(Next());
        public double[] GetDoubleArray(int count)
            => GetTArray(count, x => x.NextDouble());

        public decimal NextDecimal() => decimal.Parse(Next());
        public decimal[] GetDecimalArray(int count)
            => GetTArray(count, x => x.NextDecimal());

        public BigInteger NextBigInt() => BigInteger.Parse(Next());
        public BigInteger[] GetBigIntArray(int count)
            => GetTArray(count, x => x.NextBigInt());

        public T GetT<T>(Func<Scanner, T> func) => func(this);
        public T[] GetTArray<T>(int count, Func<Scanner, T> func)
            => Enumerable.Range(0, count)
                .Select(e => GetT(func))
                .ToArray();

        public bool IsEnd
        {
            get
            {
                CheckBuffer();
                return !_buffer.Any();
            }
        }

        public void Dispose()
        {
            if (!_reader.Equals(Console.In))
                _reader.Dispose();
        }
    }

    class Writer : IDisposable
    {
        private readonly TextWriter _writer;
        private readonly StringBuilder _cache;
        private readonly bool _isReactive;

        public Writer(TextWriter writer = null, bool isReactive = false)
        {
            _writer = writer ?? Console.Out;
            _isReactive = isReactive;
            if (!_isReactive)
                _cache = new StringBuilder();
        }

        public Writer(string path)
            : this(new StreamWriter(path)) { }

        public Writer(bool isReactive)
            : this(null, isReactive) { }

        public void Write(object value)
        {
            if (_isReactive)
            {
                _writer.Write(value);
                _writer.Flush();
            }
            else
            {
                _cache.Append(value);
            }
        }

        public void WriteFormat(string format, params object[] values)
        {
            var value = string.Format(format, values);
            Write(value);
        }

        public void WriteLine(object value = null)
        {
            Write($"{value}{Environment.NewLine}");
        }

        public void WriteLine(string format, params object[] values)
        {
            WriteFormat($"{format}{Environment.NewLine}", values);
        }

        public void Dispose()
        {
            if (!_isReactive)
            {
                _writer.Write(_cache);
                _writer.Flush();
            }
            if (!_writer.Equals(Console.Out))
            {
                _writer.Dispose();
            }
        }
    }

    public static class Extensions
    {
        public static void Set<TKey, TValue>(
            this IDictionary<TKey, TValue> source,
            TKey key, TValue value)
        {
            if (source.ContainsKey(key))
                source[key] = value;
            else
                source.Add(key, value);
        }

        public static void Update<TKey, TValue>(
            this IDictionary<TKey, TValue> source,
            TKey key, Func<TValue, TValue> update)
        {
            if (!source.ContainsKey(key))
                source.Add(key, default(TValue));
            source[key] = update(source[key]);
        }
    }

#endregion

    class MainClass : IDisposable
    {
#region "Template"

        /// <summary> 入力を受け取る </summary>
        private readonly Scanner sc;
        /// <summary> 出力を行う </summary>
        private readonly Writer wr;
        private const string _inputFile = "input.txt";
        private const string _outFile = "output.txt";

        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public MainClass()
        {
            wr = new Writer(_isReactive);
            // TODO: ファイルに出力したい場合はこっち → wr = new Writer(_outFile);

#if DEBUG
            // 手元では、ファイルから入力を受け取る
            sc = new Scanner(_inputFile);
#else

            // 提出時、標準入力から受け取る
            sc = new Scanner();
#endif

            Solve();
        }

        public void Dispose()
        {
            sc?.Dispose();
            wr?.Dispose();
        }

        #endregion

        void Solve()
        {
            int T = sc.NextInt();
            for (int _ = 0; _ < T; _++)
            {
                int n = sc.NextInt();
                if (n == 1)
                {
                    wr.WriteLine(0);
                    continue;
                }

                int cnt3 = 0;
                while (n % 3 == 0)
                {
                    n /= 3;
                    cnt3++;
                }
                int cnt2 = 0;
                while (n % 2 == 0)
                {
                    n /= 2;
                    cnt2++;
                }
                if (n != 1)
                {
                    wr.WriteLine(-1);
                    continue;
                }
                if (cnt3 < cnt2)
                {
                    wr.WriteLine(-1);
                    continue;
                }
                wr.WriteLine(cnt3 + (cnt3 - cnt2));
            }
        }

        /// <summary>
        /// TODO: リアクティブ問題か否かを指定してね♪
        /// </summary>
        private const bool _isReactive = false;
    }
}


using System;

namespace Solution
{
    class Solution
    {
        static void Main(string[] args)
        {
            int cases = int.Parse(Console.ReadLine());
            while (cases-- > 0)
            {
                int n = int.Parse(Console.ReadLine());
                if(n%3 != 0 && n!=1)
                {
                    Console.WriteLine(-1);
                }
                else
                {
                    int count = 0;
                    while(true)
                    {
                        if(n == 1)
                        {
                            break;
                        }
                        else if(n%6 == 0)
                        {
                            count++;
                            n /= 6;
                        }
                        else if(n%3 == 0)
                        {
                            count++;
                            n *= 2;
                        }
                        else
                        {
                            count = -1;
                            break;
                        }
                    }
                    if(count == -1)
                    {
                        Console.WriteLine(-1);
                    }
                    else
                    {
                        Console.WriteLine(count);
                    }
                }

            }
        }
    }
}


using System;

class _1374B
{
	static int Ans()
	{
		int n = int.Parse(Console.ReadLine());
		int cnt = 0;
		while (n != 1)
		{
			if (n % 6 == 0)
			{
				n /= 6;
			}
			else if (n % 6 == 3)
			{
				n *= 2;
			}
			else
			{
				return -1;
			}
			++cnt;
		}
		return cnt;
	}

	static void Main()
	{
		int t = int.Parse(Console.ReadLine());
		int[] anss = new int[t];
		for (int i = 0; i < t; ++i)
		{
			anss[i] = Ans();
		}
		Console.WriteLine(string.Join("\r\n", anss));
	}
}


using System;
using System.IO;
using System.Text;

namespace Multiply_by_2_divide_by_6
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            Solve(args);
            writer.Flush();
        }

        private static bool Solve(string[] args)
        {
            int n = Next();

            for (int i = 0; i < n; i++)
            {
                int a = Next();

                int n2 = 0, n3 = 0;
                while (a%2 == 0)
                {
                    n2++;
                    a /= 2;
                }
                while (a%3 == 0)
                {
                    n3++;
                    a /= 3;
                }
                if (a != 1 || n2 > n3)
                    writer.WriteLine(-1);
                else
                {
                    writer.WriteLine(n3 + (n3 - n2));
                }
            }

            return true;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


//Template by _Trung_Nguyen
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.IO;

using Libraries;
using Libraries.IO;
using Libraries.Collections;

namespace Codeforces
{
	public class sample
	{
		private static bool root6(int n, out int log2, out int log3)
		{
			log2 = 0;
			log3 = 0;
			while (n % 2 == 0)
			{
				n /= 2;
				++log2;
			}
			while (n % 3 == 0)
			{
				n /= 3;
				++log3;
			}
			return n == 1;
		}
		private static int solution()
		{
			int t = cin.ReadInt32();

			while (t-->0)
			{
				int n = cin.ReadInt32();
				int log2;
				int log3;
				bool a = root6(n, out log2, out log3);
				if (!a)
				{
					cout.WriteLine("-1");
					continue;
				}
				if (log2 > log3)
				{
					cout.WriteLine("-1");
					continue;
				}

				cout.WriteLine((log3 - log2) + log3);

			}

			return 0;
		}

//===============Prewritten code======================
		private static istream cin = new istream(Console.OpenStandardInput());
		private static ostream cout = new ostream(Console.OpenStandardOutput());
		private static ostream cerr = new ostream(Console.OpenStandardError());
		private static ostream clog = new ostream(Console.OpenStandardError(), true);

		public static int Main()
		{
			try
			{
				int returnValue = solution();
				cout.Flush();
				cerr.Flush();
				return returnValue;
			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
				return -1;
			}
		}
	}
}

namespace Libraries.IO
{
	public class istream : IDisposable
	{
		private BufferedStream buffer;
		private StreamReader reader;
		private Queue<string> words = new Queue<string>();
		private void getWords()
		{
			while (words.Count == 0)
			{
				string [] temp = reader.ReadLine().Split();
				foreach (var str in temp)
				{
					if (str.Length == 0) continue;
					words.Enqueue(str);
				}
			}
		}
		public istream(Stream istr)
		{
			buffer = new BufferedStream(istr);
			reader = new StreamReader(buffer);
		}
		public int Peek()
		{
			return reader.Peek();
		}
		public int Read()
		{
			return reader.Read();
		}
		public string ReadLine()
		{
			return reader.ReadLine();
		}
		public int [] ReadInt32Array()
		{
			return reader.ReadLine().Split().Select(x => Convert.ToInt32(x)).ToArray();
		}
		public long [] ReadInt64Array()
		{
			return reader.ReadLine().Split().Select(x => Convert.ToInt64(x)).ToArray();
		}
		public string [] ReadStringArray()
		{
			return reader.ReadLine().Split();
		}
		public int ReadInt32()
		{
			getWords();
			return Convert.ToInt32(words.Dequeue());
		}
		public long ReadInt64()
		{
			getWords();
			return Convert.ToInt64(words.Dequeue());
		}
		public string ReadString()
		{
			getWords();
			return words.Dequeue();
		}
		public void Dispose()
		{
			reader.Dispose();
			buffer.Dispose();
		}
	}
	public class ostream : IDisposable
	{
		private BufferedStream buffer;
		private StreamWriter writer;
		public ostream(Stream ostr, bool autoflush = false)
		{
			buffer = new BufferedStream(ostr);
			writer = new StreamWriter(buffer);
			writer.AutoFlush = autoflush;
		}
		public void Write(string format, params object[] args)
		{
			writer.Write(format, args);
		}
		public void Write<T>(T value)
		{
			writer.Write(value);
		}
		public void Write<T>(T [] arr, string sep)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, x);
		}
		public void WriteLine(string format, params object[] args)
		{
			writer.WriteLine(format, args);
		}
		public void WriteLine<T>(T value)
		{
			writer.WriteLine(value);
		}
		public void WriteLine<T>(IEnumerable<T> arr, string sep)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, x);
			writer.WriteLine();
		}
		public void WriteLine<T>(IEnumerable<T> arr, string sep, Func<T, T> funct)
		{
			string fmt = "{0}" + sep;
			foreach (var x in arr) writer.Write(fmt, funct(x));
			writer.WriteLine();
		}
		public void WriteLine()
		{
			writer.WriteLine();
		}
		public void Flush()
		{
			writer.Flush();
		}
		public void Dispose()
		{
			writer.Dispose();
			buffer.Dispose();
		}
	}
}
namespace Libraries
{
	public class Utilities
	{
		public static void Swap<T>(ref T x, ref T y)
		{
			T temp = y;
			y = x;
			x = temp;
		}
	}

	public static class Arrays
	{
		private static T Inc<T>(ref T value)
		{
			dynamic temp = value;
			dynamic temp2 = temp;
			value = ++temp2;
			return temp++;
		}
		public static IEnumerable<T> FillDefault<T>(int size) where T : new()
		{
			for (int i = 0; i < size; ++i) yield return new T();
		}
		public static IEnumerable<T> Fill<T>(int size, T value)
		{
			for (int i = 0; i < size; ++i) yield return value;
		}
		public static IEnumerable<T> Fill<T>(int size, Func<T> funct)
		{
			for (int i = 0; i < size; ++i) yield return funct();
		}
		public static IEnumerable<T> Iota<T>(int size, T init)
		{
			for (int i = 0; i < size; ++i) yield return Inc(ref init);
		}
		public static T First<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			return container[0];
		}
		public static T Last<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			return container[container.Count - 1];
		}
		public static void RemoveLast<T> (this IList<T> container)
		{
			if (container.Count == 0) throw new InvalidOperationException("The container is empty");
			container.RemoveAt(container.Count - 1);
		}
	}
}

namespace Libraries.Collections
{
	class PriorityQueue<T> where T : IComparable
	{
	    private List<T> list;
	    public int Count { get { return list.Count; } }
	    public readonly bool IsDescending;

	    public PriorityQueue(bool isdesc = true)
	    {
	        list = new List<T>();
	        IsDescending = isdesc;
	    }

	    public PriorityQueue(int capacity, bool isdesc = true)
	    {
	        list = new List<T>(capacity);
	        IsDescending = isdesc;
	    }

	    public PriorityQueue(IEnumerable<T> collection, bool isdesc = true)
	        : this()
	    {
	        IsDescending = isdesc;
	        foreach (var item in collection)
	            Enqueue(item);
	    }

	    public void Enqueue(T x)
	    {
	        list.Add(x);
	        int i = Count - 1;

	        while (i > 0)
	        {
	            int p = (i - 1) / 2;
	            if ((IsDescending ? -1 : 1) * list[p].CompareTo(x) <= 0) break;

	            list[i] = list[p];
	            i = p;
	        }

	        if (Count > 0) list[i] = x;
	    }

	    public T Dequeue()
	    {
	        T target = Peek();
	        T root = list[Count - 1];
	        list.RemoveAt(Count - 1);

	        int i = 0;
	        while (i * 2 + 1 < Count)
	        {
	            int a = i * 2 + 1;
	            int b = i * 2 + 2;
	            int c = b < Count && (IsDescending ? -1 : 1) * list[b].CompareTo(list[a]) < 0 ? b : a;

	            if ((IsDescending ? -1 : 1) * list[c].CompareTo(root) >= 0) break;
	            list[i] = list[c];
	            i = c;
	        }

	        if (Count > 0) list[i] = root;
	        return target;
	    }

	    public T Peek()
	    {
	        if (Count == 0) throw new InvalidOperationException("Queue is empty.");
	        return list[0];
	    }

	    public void Clear()
	    {
	        list.Clear();
	    }
	}
    internal class DequeUtility
    {
        public static int ClosestPowerOfTwoGreaterThan(int x)
        {
            x--;
            x |= (x >> 1);
            x |= (x >> 2);
            x |= (x >> 4);
            x |= (x >> 8);
            x |= (x >> 16);
            return (x+1);
        }

        public static int Count<TSource>(IEnumerable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            ICollection<TSource> genericCollection = source as ICollection<TSource>;
            if (genericCollection != null)
            {
                return genericCollection.Count;
            }

            ICollection nonGenericCollection = source as ICollection;
            if (nonGenericCollection != null)
            {
                return nonGenericCollection.Count;
            }

            checked
            {
                int count = 0;
                using (var iterator = source.GetEnumerator())
                {
                    while (iterator.MoveNext())
                    {
                        count++;
                    }
                }
                return count;
            }
        }
    }

    public class Deque<T> : IList<T>
    {
        private const int defaultCapacity = 16;

        private int startOffset;

        private T[] buffer;

        public Deque() : this(defaultCapacity) { }

        public Deque(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "capacity", "capacity is less than 0.");
            }

            this.Capacity = capacity;
        }

        public Deque(IEnumerable<T> collection)
            : this(DequeUtility.Count(collection))
        {
            InsertRange(0, collection);
        }

        private int capacityClosestPowerOfTwoMinusOne;

        public int Capacity
        {
            get
            {
                return buffer.Length;
            }

            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException(
                        "value",
                        "Capacity is less than 0.");
                }
                else if (value < this.Count)
                {
                    throw new InvalidOperationException(
                        "Capacity cannot be set to a value less than Count");
                }
                else if (null != buffer && value == buffer.Length)
                {
                    return;
                }

                int powOfTwo = DequeUtility.ClosestPowerOfTwoGreaterThan(value);

                value = powOfTwo;

                T[] newBuffer = new T[value];
                this.CopyTo(newBuffer, 0);

                buffer = newBuffer;
                startOffset = 0;
                this.capacityClosestPowerOfTwoMinusOne = powOfTwo - 1;
            }
        }

        public bool IsFull
        {
            get { return this.Count == this.Capacity; }
        }

        public bool IsEmpty
        {
            get { return 0 == this.Count; }
        }

        private void ensureCapacityFor(int numElements)
        {
            if (this.Count + numElements > this.Capacity)
            {
                this.Capacity = this.Count + numElements;
            }
        }

        private int toBufferIndex(int index)
        {
            int bufferIndex;

            bufferIndex = (index + this.startOffset)
                & this.capacityClosestPowerOfTwoMinusOne;

            return bufferIndex;
        }

        private void checkIndexOutOfRange(int index)
        {
            if (index >= this.Count)
            {
                throw new IndexOutOfRangeException(
                    "The supplied index is greater than the Count");
            }
        }

        private static void checkArgumentsOutOfRange(
            int length,
            int offset,
            int count)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "offset", "Invalid offset " + offset);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "count", "Invalid count " + count);
            }

            if (length - offset < count)
            {
                throw new ArgumentException(
                    String.Format(
                    "Invalid offset ({0}) or count + ({1}) "
                    + "for source length {2}",
                    offset, count, length));
            }
        }

        private int shiftStartOffset(int value)
        {
            this.startOffset = toBufferIndex(value);

            return this.startOffset;
        }

        private int preShiftStartOffset(int value)
        {
            int offset = this.startOffset;
            this.shiftStartOffset(value);
            return offset;
        }

        private int postShiftStartOffset(int value)
        {
            return shiftStartOffset(value);
        }

        #region IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            if (this.startOffset + this.Count > this.Capacity)
            {
                for (int i = this.startOffset; i < this.Capacity; i++)
                {
                    yield return buffer[i];
                }

                int endIndex = toBufferIndex(this.Count);
                for (int i = 0; i < endIndex; i++)
                {
                    yield return buffer[i];
                }
            }
            else
            {
                int endIndex = this.startOffset + this.Count;
                for (int i = this.startOffset; i < endIndex; i++)
                {
                    yield return buffer[i];
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion

        #region ICollection

        bool ICollection<T>.IsReadOnly
        {
            get { return false; }
        }

        public int Count
        {
            get;
            private set;
        }

        private void incrementCount(int value)
        {
            this.Count = this.Count + value;
        }

        private void decrementCount(int value)
        {
            this.Count = Math.Max(this.Count - value, 0);
        }

        public void Add(T item)
        {
            AddLast(item);
        }

        private void ClearBuffer(int logicalIndex, int length)
        {
            int offset = toBufferIndex(logicalIndex);
            if (offset + length > this.Capacity)
            {
                int len = this.Capacity - offset;
                Array.Clear(this.buffer, offset, len);

                len = toBufferIndex(logicalIndex + length);
                Array.Clear(this.buffer, 0, len);
            }
            else
            {
                Array.Clear(this.buffer, offset, length);
            }
        }

        public void Clear()
        {
            if (this.Count > 0)
            {
                ClearBuffer(0, this.Count);
            }
            this.Count = 0;
            this.startOffset = 0;
        }

        public bool Contains(T item)
        {
            return this.IndexOf(item) != -1;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            if (null == array)
            {
                throw new ArgumentNullException("array", "Array is null");
            }

            if (null == this.buffer)
            {
                return;
            }

            checkArgumentsOutOfRange(array.Length, arrayIndex, this.Count);

            if (0 != this.startOffset
                && this.startOffset + this.Count >= this.Capacity)
            {
                int lengthFromStart = this.Capacity - this.startOffset;
                int lengthFromEnd = this.Count - lengthFromStart;

                Array.Copy(
                    buffer, this.startOffset, array, 0, lengthFromStart);

                Array.Copy(
                    buffer, 0, array, lengthFromStart, lengthFromEnd);
            }
            else
            {
                Array.Copy(
                    buffer, this.startOffset, array, 0, Count);
            }
        }

        public bool Remove(T item)
        {
            bool result = true;
            int index = IndexOf(item);

            if (-1 == index)
            {
                result = false;
            }
            else
            {
                RemoveAt(index);
            }

            return result;
        }

        #endregion

        #region List<T>

        public T this[int index]
        {
            get
            {
                return this.Get(index);
            }

            set
            {
                this.Set(index, value);
            }
        }

        public void Insert(int index, T item)
        {
            ensureCapacityFor(1);

            if (index == 0)
            {
                AddFirst(item);
                return;
            }
            else if (index == Count)
            {
                AddLast(item);
                return;
            }

            InsertRange(index, new[] { item });
        }

        public int IndexOf(T item)
        {
            int index = 0;
            foreach (var myItem in this)
            {
                if (myItem.Equals(item))
                {
                    break;
                }
                ++index;
            }

            if (index == this.Count)
            {
                index = -1;
            }

            return index;
        }

        public void RemoveAt(int index)
        {
            if (index == 0)
            {
                RemoveFirst();
                return;
            }
            else if (index == Count - 1)
            {
                RemoveLast();
                return;
            }

            RemoveRange(index, 1);
        }
        #endregion

        public void AddFirst(T item)
        {
            ensureCapacityFor(1);
            buffer[postShiftStartOffset(-1)] = item;
            incrementCount(1);
        }

        public void AddLast(T item)
        {
            ensureCapacityFor(1);
            buffer[toBufferIndex(this.Count)] = item;
            incrementCount(1);
        }

        public T RemoveFirst()
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }

            T result = buffer[this.startOffset];
            buffer[preShiftStartOffset(1)] = default(T);
            decrementCount(1);
            return result;
        }

        public T RemoveLast()
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }

            decrementCount(1);
            int endIndex = toBufferIndex(this.Count);
            T result = buffer[endIndex];
            buffer[endIndex] = default(T);

            return result;
        }

        public void AddRange(IEnumerable<T> collection)
        {
            AddLastRange(collection);
        }

        public void AddFirstRange(IEnumerable<T> collection)
        {
            AddFirstRange(collection, 0, DequeUtility.Count(collection));
        }

        public void AddFirstRange(
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            InsertRange(0, collection, fromIndex, count);
        }

        public void AddLastRange(IEnumerable<T> collection)
        {
            AddLastRange(collection, 0, DequeUtility.Count(collection));
        }

        public void AddLastRange(
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            InsertRange(this.Count, collection, fromIndex, count);
        }

        public void InsertRange(int index, IEnumerable<T> collection)
        {
            int count = DequeUtility.Count(collection);
            this.InsertRange(index, collection, 0, count);
        }

        public void InsertRange(
            int index,
            IEnumerable<T> collection,
            int fromIndex,
            int count)
        {
            checkIndexOutOfRange(index - 1);

            if (0 == count)
            {
                return;
            }

            ensureCapacityFor(count);

            if (index < this.Count / 2)
            {
                if (index > 0)
                {
                    int copyCount = index;
                    int shiftIndex = this.Capacity - count;
                    for (int j = 0; j < copyCount; j++)
                    {
                        buffer[toBufferIndex(shiftIndex + j)] =
                            buffer[toBufferIndex(j)];
                    }
                }

                this.shiftStartOffset(-count);

            }
            else
            {
                if (index < this.Count)
                {
                    int copyCount = this.Count - index;
                    int shiftIndex = index + count;
                    for (int j = 0; j < copyCount; j++)
                    {
                        buffer[toBufferIndex(shiftIndex + j)] =
                            buffer[toBufferIndex(index + j)];
                    }
                }
            }

            int i = index;
            foreach (T item in collection)
            {
                buffer[toBufferIndex(i)] = item;
                ++i;
            }

            incrementCount(count);
        }

        public void RemoveRange(int index, int count)
        {
            if (this.IsEmpty)
            {
                throw new InvalidOperationException("The Deque is empty");
            }
            if (index > Count - count)
            {
                throw new IndexOutOfRangeException(
                    "The supplied index is greater than the Count");
            }

            ClearBuffer(index, count);

            if (index == 0)
            {
                this.shiftStartOffset(count);
                this.Count -= count;
                return;
            }
            else if (index == Count - count)
            {
                this.Count -= count;
                return;
            }

            if ((index + (count / 2)) < Count / 2)
            {
                int copyCount = index;
                int writeIndex = count;
                for (int j = 0; j < copyCount; j++)
                {
                    buffer[toBufferIndex(writeIndex + j)]
                        = buffer[toBufferIndex(j)];
                }

                this.shiftStartOffset(count);
            }
            else
            {
                int copyCount = Count - count - index;
                int readIndex = index + count;
                for (int j = 0; j < copyCount; ++j)
                {
                    buffer[toBufferIndex(index + j)] =
                        buffer[toBufferIndex(readIndex + j)];
                }
            }

            decrementCount(count);
        }
        public T Get(int index)
        {
            checkIndexOutOfRange(index);
            return buffer[toBufferIndex(index)];
        }

        public void Set(int index, T item)
        {
            checkIndexOutOfRange(index);
            buffer[toBufferIndex(index)] = item;
        }

    }
}


using System;
using System.Text;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                double n = int.Parse(Console.ReadLine());
                int count = 0;
                Boolean flag = true;

                while (n != 1)
                {
                    if (!(n % 1 == 0 || n % 1 == 0.5))
                    {
                        Console.WriteLine(-1);
                        flag = false;
                        break;
                    }
                    if (n % 1 == 0.5)
                        n *= 2;
                    else
                        n /= 6;
                    count++;
                }
                if (flag)
                    Console.WriteLine(count);
            }
        }
    }
}


using System;

namespace TaskB
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = int.Parse(Console.ReadLine());
            for (var i = 0; i < t; i++)
            {
                Solve();
            }
        }

        private static void Solve()
        {
            var n = int.Parse(Console.ReadLine());

            var ans = 0;

            while (n != 1)
            {
                if (n % 6 == 0)
                {
                    n /= 6;
                }
                else
                {
                    n *= 2;
                }

                if (n != 1 && n % 3 != 0)
                {
                    ans = -1;
                    break;
                }

                ans++;
            }

            Console.WriteLine(ans);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class B { public static void Main() => (new Solver()).Solve(); }

public class Solver
{
	public void Solve()
	{
		long t = GetLong();
		var ans = Range(0, t).Select(_ => solve());
		WriteLine(string.Join("\n", ans));
	}

	private long solve()
	{
		long n = GetLong();
		if(n == 1)
			return 0;

		var cnt = new long[2];
		while(n % 2 == 0)
		{
			cnt[0]++;
			n /= 2;
		}
		while(n % 3 == 0)
		{
			cnt[1]++;
			n /= 3;
		}
		if(n != 1 || cnt[0] > cnt[1])
			return -1;

		return 2 * cnt[1] - cnt[0];
	}
}

public static class MyIO
{
	private static string[] args = new string[0];
	private static int cnt = 0;
	private static IEnumerable<string> getArgs(long N) => Range(0, N).Select(_ => getArg());
	private static string getArg()
	{
		if(cnt == args.Length) { args = Console.ReadLine().Split(); cnt = 0; }
		return args[cnt++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => getArgs(N).Select(int.Parse).ToArray();
	public static long[] GetLongs(long N) => getArgs(N).Select(long.Parse).ToArray();
	public static double[] GetDoubles(long N) => getArgs(N).Select(double.Parse).ToArray();
	public static decimal[] GetDecimals(long N) => getArgs(N).Select(decimal.Parse).ToArray();
	public static string[] GetStrings(long N) => getArgs(N).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) < 0) x = y; }
	public static void chmin<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) > 0)	x = y; }
	public static void swap<T>(ref T x, ref T y) { T tmp = x; x = y; y = tmp; }
	public static long rightShift(long x, long y) => (x >> (int)y);
	public static long leftShift(long x, long y) => (x << (int)y);
	public static bool contains(long s, long x) => (rightShift(s, x) & 1L) == 1L;
	public static long gcd(long x, long y) => (y == 0) ? x : gcd(y, x % y);
	public static long lcm(long x, long y) => (x / gcd(x,y)) * y;
	public static IEnumerable<int> Range(long start, long count) => Enumerable.Range((int)start, (int)count);
	public static void Sort(this List<string> source) => source.Sort(StringComparer.Ordinal);
}