import java.util.*; 

public class B {
	
	static int gcd(int a, int b) 
	{ 
	    if (b == 0) 
	        return a; 
	    return gcd(b, a % b);  
	      
	} 
	static int[] sort(int arr[]) {
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	for(int i:arr)
    		list.add(i);
    	Collections.sort(list);
    	for(int i = 0;i<arr.length;i++)
    		arr[i] = list.get(i);
    	return arr;
    }

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		for(int tt = 0;tt<t;tt++) {
			int n = scan.nextInt();
			int arr[] = new int[n];
			ArrayList <Integer> ans = new ArrayList <Integer>();
			for(int i = 0;i<n;i++)
				arr[i] = scan.nextInt();
			
			int max = arr[0];
			int pos = 0;
			for(int i = 1;i<n;i++) {
				if(arr[i] > max) {
					max = arr[i];
					pos = i;
				}
			}
			ans.add(max);
			int gcdval = max;
			arr[pos] = 0;
			while(ans.size()!=n) {
				int maxgcd = 0;
				int maxpos = -1;

				for(int i= 0;i<n;i++) {
					if(arr[i]!=0) {
						int g = gcd(gcdval,arr[i]);
						if(g>maxgcd) {
							maxgcd = g;
							maxpos = i;
						}
					}
				}

				ans.add(arr[maxpos]);
				arr[maxpos] = 0;
				gcdval = maxgcd;
			}
			for(int i:ans)
				System.out.print(i+" ");
			System.out.println();
		}

	}
 
}
