import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int T = in.nextInt();
            while (T-- > 0)
            {
                solveOne(in, out);
            }
        }

        private void solveOne(Scanner in, PrintWriter out)
        {
            int N = in.nextInt();
            int nums[] = CPUtils.readIntArray(N, in);
            boolean used[] = new boolean[N];

            long gcd = 0;
            for (int j = 0; j < N; j++)
            {
                int idx_win = -1;
                long local_gcd = 0;
                for (int idx = 0; idx < nums.length; idx++)
                {
                    if (used[idx]) continue;
                    long next_gcd = CPUtils.gcd(gcd, nums[idx]);
                    if (next_gcd >= local_gcd)
                    {
                        local_gcd = next_gcd;
                        idx_win = idx;
                    }
                }
                gcd = local_gcd;
                used[idx_win] = true;
                out.print(nums[idx_win] + " ");
            }
            out.println();
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

        public static long gcd(long n1, long n2)
        {
            if (n2 == 0)
            {
                return n1;
            }
            return gcd(n2, n1 % n2);
        }

    }
}

