import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;     // System.out is a PrintWriter
import java.util.ArrayList;
import java.util.Arrays;
// import java.util.ArrayList;
import java.util.Collections;    //  for ArrayList sorting mainly
// import java.util.HashMap;
// import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;

public class B {
    public static void main(String[] args) throws Exception {
        FastScanner scn = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int tc = scn.nextInt();
        while (tc-- > 0) {
            int N = scn.nextInt();
            ArrayList<Integer> arr = new ArrayList<>();
            int gcdMax = 0, pos = -1;
            for (int i = 0; i < N; i++) {
                arr.add(scn.nextInt());
                if (arr.get(i) > gcdMax) {
                    gcdMax = arr.get(i);
                    pos = i;
                }
            }
            arr.remove(pos);
            out.print(gcdMax + " ");
            while (arr.size() > 0) {
                int mxGcd = arr.get(0);
                pos = 0;
                for (int i = 0, sz = arr.size(); i < sz; i++) {
                    int g1 = gcd(mxGcd, gcdMax), g2 = gcd(arr.get(i), gcdMax);
                    if (g1 < g2) {
                        mxGcd = arr.get(i);
                        pos = i;
                    } else if (g1 == g2 && mxGcd < arr.get(i)) {
                        mxGcd = arr.get(i);
                        pos = i;
                    }
                }
                gcdMax = gcd(gcdMax, mxGcd);
                out.print(arr.remove(pos) + " ");
            }
            out.println();
        }
        out.close();
    }

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

    static class Pair implements Comparable<Pair> {
        int ff, ss; // default 0 --> first and second
        
        public Pair(int first, int second) {
            this.ff = first;
            this.ss = second;
        }

        @Override
        public int compareTo(Pair o) {
            if (ff != o.ff)
                return ff - o.ff;
            return ss - o.ss;
        }

        @Override
        public String toString() {
            return "[" + ff + ", " + ss + "]";
        }
    }

    static final Random random = new Random();

    static void ruffleSort(int[] arr, boolean reverse) {
        for (int i = 0, N = arr.length; i < N; i++) {
            int randIdx = random.nextInt(N);
            // swap
            int temp = arr[randIdx];
            arr[randIdx] = arr[i];
            arr[i] = temp;
        }
        Arrays.sort(arr);   // NOTE use quick-sort for primitive types, merge-sort for objects implementing Comparable/Comparator
        if (reverse) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    
    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        FastScanner() {
            this.br = new BufferedReader(new InputStreamReader(System.in));
            this.st = new StringTokenizer("");
        }
        FastScanner(String file) {
            try {
                java.io.File inpFile = new java.io.File(file);
                this.br = new BufferedReader(new java.io.FileReader(inpFile));
            } catch (FileNotFoundException err) {
                err.printStackTrace();
            }
            this.st = new StringTokenizer("");
        }

        String next() {
            while (!st.hasMoreTokens())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException err) {
                    err.printStackTrace();
                }
            return st.nextToken();
        }

        String nextLine() {
            if (st.hasMoreTokens()) {
                return st.nextToken("").trim();
            }
            try {
                return br.readLine().trim();
            } catch (IOException err) {
                err.printStackTrace();
            }
            return "";
        }

        byte nextByte() {
            return Byte.parseByte(next());
        }

        short nextShort() {
            return Short.parseShort(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        float nextFloat() {
            return Float.parseFloat(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        int[] readIntArray(final int N) {
            int[] arr = new int[N];
            for (int i = 0; i < N; i++)
                arr[i] = nextInt();
            return arr;
        }
    }
}
