import java.io.*;
import java.util.*;

/*
polyakoff
*/

public class Main {

    static FastReader in;
    static PrintWriter out;
    static Random rand = new Random();
    static final int oo = (int) 2e9;
    static final long OO = (long) 4e18;
    static final int MOD = (int) 1e9 + 7;

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

    static void solve() {
        int n = in.nextInt();

        int[] a = new int[n];
        int[] a2 = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            a2[i] = a[i];
        }

        int[] b = new int[n];
        for (int i = 0; i < n;) {
            int max = 0;
            LinkedList<Integer> maxs = new LinkedList<>();
            for (int j = 0; j < n; j++) {
                if (a2[j] > max) {
                    max = a2[j];
                    maxs.clear();
                    maxs.add(j);
                } else if (a2[j] == max)
                    maxs.add(j);
            }
            for (int j : maxs)
                b[i++] = a[j];
            for (int j = 0; j < n; j++) {
                if (a2[j] == max)
                    a2[j] = -1;
                else
                    a2[j] = gcd(a2[j], max);
            }
        }

        for (int i = 0; i < n; i++)
            out.print(b[i] + " ");
        out.println();


    }


    public static void main(String[] args) throws Exception {
        in = new FastReader(System.in);
//        in = new FastReader(new FileInputStream("sometext.txt"));
        out = new PrintWriter(System.out);
//        out = new PrintWriter(new FileOutputStream("output.txt"));

        int T = 1;
        T = in.nextInt();
        while (T-- > 0)
            solve();

//        Thread thread = new Thread(null, () -> {
//            solve();
//        }, "Main", 1 << 28);
//        thread.start();
//        thread.join();

        out.flush();
        out.close();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

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

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

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

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }

    static void dbg(Object... args) {
        for (Object arg : args)
            out.print(" " + arg);
        out.flush();
    }

}