import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws Exception {
        int t = readInt();
        while(t-->0) {
            int n = readInt();
            int[] a = readIntArray();
            Arrays.sort(a);
            int[] b = new int[n];
            b[0] = a[n-1];
            int[] v = new int[n];
            v[n-1] = 1;
            int gcd = b[0];
            for(int i=1 ; i<n ; i++) {
                int ind = -1;
                int mxg = 0;
                for (int j = 0; j < n; j++) {
                    if(v[j]==0) {
                        int g = gcd(gcd, a[j]);
                        if(g>=mxg) {
                            mxg = g;
                            ind = j;
                        }
                    }
                }
                v[ind]=1;
                b[i] = a[ind];
            }
            for (int i = 0; i < n; i++) {
                System.out.print(b[i]+" ");
            }
            System.out.println();
        }
    }

    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }


    private static String readString() throws Exception {
        return br.readLine();
    }

    private static int readInt() throws Exception{
        return Integer.parseInt(readString());
    }

    private static long readLong() throws Exception{
        return Long.parseLong(readString());
    }

    private static double readDouble() throws Exception{
        return Double.parseDouble(readString());
    }

    private static String[] readStringArray() throws Exception {
        return br.readLine().split(" ");
    }

    private static List<String> readStringList() throws Exception {
        return Arrays.asList(readStringArray());
    }

    private static List<Integer> readIntList() throws Exception {
        return readStringList().stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
    }

    private static int[] readIntArray() throws Exception {
        return readIntList().stream().mapToInt(i -> i).toArray();
    }

    private static List<Long> readLongList() throws Exception {
        return readStringList().stream().map(s -> Long.parseLong(s)).collect(Collectors.toList());
    }

    private static long[] readLongArray() throws Exception {
        return readLongList().stream().mapToLong(i -> i).toArray();
    }

    private static List<Double> readDoubleList() throws Exception {
        return readStringList().stream().map(s -> Double.parseDouble(s)).collect(Collectors.toList());
    }

    private static double[] readDoubleArray() throws Exception {
        return readDoubleList().stream().mapToDouble(i -> i).toArray();
    }
}