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

public class Main {


    public static void main(String[] args) {
        try {
            int T = sc.nextInt();
            for (int t = 0; t < T; t++) {
                solve();
            }
        } finally {
            out.close();
        }
    }


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

    private static void solve() {

        int n = sc.nextInt();
        int[] arr = sc.nextArr(n);
        int[] arr2 = new int[n];
        int k = 0;
        for (int num : arr) {
            arr2[k++] = num;
        }
        boolean[] vis = new boolean[n];
        int[] ans = new int[n];
        int max = Integer.MIN_VALUE;
        int maxIdx = 0;

        for (int i = 0; i < n; i++) {
            if (arr[i] > max) {
                max = arr[i];
                maxIdx = i;
            }
        }
        vis[maxIdx] = true;

        ans[0] = max;
        int i = 1;
        while (i < n) {
            for (int j = 0; j < n; j++) {
                if (!vis[j]) {
                    arr[j] = gcd(ans[i - 1], arr[j]);
                }
            }
            max = Integer.MIN_VALUE;
            maxIdx = 0;
            for (int j = 0; j < n; j++) {
                if (!vis[j]) {
                    if (arr[j] > max) {
                        max = arr[j];
                        maxIdx = j;
                    }
                }
            }
            vis[maxIdx] = true;
            ans[i++] = arr2[maxIdx];
        }

        for (int a : ans) {
            out.print(a + " ");
        }
        out.print("\n");
    }

    // -----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int[] nextArr(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            return arr;
        }

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

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

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

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

    static int[] toArr(List<Integer> list) {
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    static int mod = 1000000007;
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
    static MyScanner sc = new MyScanner();
}
