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

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

    public static void solve() throws Exception{
        InputStreamReader ip=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ip);
        int t = Integer.parseInt(br.readLine());
        StringBuilder asb=new StringBuilder();

       while(t-->0){
        int n =Integer.parseInt(br.readLine());
        String[] strs=(br.readLine()).trim().split(" ");
        // int n=Integer.parseInt(strs[0]),l=Integer.parseInt(strs[1]),r=Integer.parseInt(strs[2]),s=Integer.parseInt(strs[3]);
        //  String str=(br.readLine()).trim();

        int[] arr=new int[n];
        int mi=0;

        for(int i=0;i<n;i++){
            arr[i]=Integer.parseInt(strs[i]);
            if(arr[i]>arr[mi]){mi=i;}
        }

        int[] ans=new int[n];
        ans[0]=arr[mi];   arr[mi]=-1;   
        int pgcd=ans[0];

        for(int i=1;i<n;i++){

            int mgcd=-1;  int midx=-1;
            for(int j=0;j<n;j++){
                if(arr[j]!=-1){
                    int cgcd=gcd(pgcd, arr[j]);

                    if(cgcd>mgcd){  mgcd=cgcd; midx=j;  }
                }
            }

            ans[i]=arr[midx];  arr[midx]=-1;  pgcd=mgcd;
           
         }

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

       }
       System.out.println(asb);
    }
  

//******************************************************************************************************************** */
    public static void main(String[] args) throws Exception{
        solve();
        // sec();
     }

     public static void solve2() throws Exception{
        InputStreamReader ip=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ip);
        int t = Integer.parseInt(br.readLine());
        StringBuilder asb=new StringBuilder();

       while(t-->0){
        // int n =Integer.parseInt(br.readLine());
        // String[] strs=(br.readLine()).trim().split(" ");
        // int n=Integer.parseInt(strs[0]),l=Integer.parseInt(strs[1]),r=Integer.parseInt(strs[2]),s=Integer.parseInt(strs[3]);
        //  String str=(br.readLine()).trim();
        
       }
    }
    //sort
     public static void sort(long[] arr) {
		int n = arr.length;
		for (int i = 0; i < n; i++) {
			int idx = (int) Math.random() * n;
			long temp = arr[i];  arr[i] = arr[idx];  arr[idx] = temp;  //swap
		}
		Arrays.sort(arr);
	}
 
	public static void sort(int[] arr) {
		int n = arr.length;
		for (int i = 0; i < n; i++) {
			int idx = (int) Math.random() * n;
			int temp = arr[i];  arr[i] = arr[idx]; arr[idx] = temp;  //swap
		}
		Arrays.sort(arr);
	}
    //1d print
    public static void print(int[] dp) {
		for (int val : dp) {
			System.out.print(val + " ");
		}
		System.out.println();
	}
 
	public static void print(long[] dp) {
		for (long val : dp) {
			System.out.print(val + " ");
		}
		System.out.println();
	}  
    //2d print
	public static void print(long[][] dp) {
		for (long[] a : dp) {
			for (long val : a) {
				System.out.print(val + " ");
			}
			System.out.println();
		}
	}
 
	public static void print(int[][] dp) {
		for (int[] a : dp) {
			for (int val : a) {
				System.out.print(val + " ");
			}
			System.out.println();
		}
	}
 
	
}


