//package Random;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Random {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            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 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;
        }
    }

    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        StringBuilder ans = new StringBuilder("");

        while(t-->0){

            int n = sc.nextInt();
            int[] arr = new int[n];
            int index=-1;
            int max =0;

            for(int i=0;i<n;i++) {
                arr[i] = sc.nextInt();
                if(arr[i]>max){
                    index=i;
                    max=arr[i];
                }
            }

            boolean[] vis = new boolean[n];

            //Arrays.sort(arr);

            int[] dp = new int[n];
            dp[0]=arr[index];
            vis[index]=true;
            int temp = arr[index];
            max =0;
            index=-1;
            for(int i=1;i<n;i++){
                max=0;
                index=-1;
                for(int j=0;j<n;j++){
                    if(vis[j]==false && gcd(arr[j],temp)>max ){
                        //vis[j]=true;
                        max=gcd(arr[j],temp);
                        index=j;
                        //dp[i]=arr[j];
                    }
                    /*
                    else if(gcd(arr[j],temp)==max && vis[j]==false && arr[index]>arr[j]){
                        index=j;
                    }
                    
                     */
                }
                vis[index]=true;
                dp[i]=arr[index];
                temp=max;
            }

            for(int i=0;i<n;i++){
                ans.append(dp[i]+" ");
            }

            ans.append('\n');

        }
        System.out.println(ans);


    }

    static int gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0)
            return b;
        if (b == 0)
            return a;

        // base case
        if (a == b)
            return a;

        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }


}
