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

public class Test {

    public static void main(String args[]){
       FastScanner fs= new FastScanner();
       int tc=fs.nextInt();
       while(tc-->0){
            
                int n=fs.nextInt();
                int arr[]=fs.readArray(n);
                int mx=0,mx_ind=-1;
                for(int i=0;i<n;i++){
                    if(arr[i]>mx){
                        mx=arr[i];
                        mx_ind=i;
                    }    
                }
                boolean vis[]=new boolean[n]; 
                int pref_gcd=mx;
                System.out.print(mx);
                System.out.print(" ");
                vis[mx_ind]=true;
                for(int i=1;i<n;i++){
                int mx_curr_gcd=1,mx_curr_ind=-1;  
                for(int j=0;j<n;j++){
                    if(vis[j])continue;
                    if(mygcd(pref_gcd,arr[j])>=mx_curr_gcd){
                        mx_curr_gcd=mygcd(pref_gcd,arr[j]);
                        mx_curr_ind=j;
                    }
                }
                pref_gcd=mygcd(pref_gcd,arr[mx_curr_ind]);
                vis[mx_curr_ind]=true;
                System.out.print(arr[mx_curr_ind]);
                System.out.print(" ");
                }
        
                System.out.println();
       }
    }
    
    private static int mygcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

    static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
 
}


