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

public class Template {
	
	static Scanner sc = new Scanner(System.in);
	

	public static void main(String[] args) {
		int test = sc.nextInt();
		while(test-->0) {
			int n = sc.nextInt();
			int[] a = readArray(n);
			boolean[] used = new boolean[n];
			
			int max = a[0],maxind=0;
			for(int i=0 ; i<n ; i++) {
				if(a[i]>max) {
					max=a[i]; 
					maxind = i;
				}
			}
			used[maxind] = true;
			System.out.print(max + " ");
			int truec = 1;
			while(truec<n) {
				int maxgcd = -1, maxgcdind = -1;
				for(int i=0 ; i<n ; i++) {
					if(used[i]==false && gcd(a[i],max)>gcd(maxgcd,max)) {
						maxgcd = a[i] ; 
						maxgcdind = i;
					}
				}
				used[maxgcdind] = true;
				truec++;
				max = maxgcd;
				System.out.print(maxgcd + " ");
			}
			System.out.println();
		}
	}
	
	public static int[] readArray(int n) {
		int[] a = new int[n];
		for(int i=0 ; i<n ; i++) {
			a[i] = sc.nextInt();
		} return a;
	}
	
	public static String giveBinary(long n) {
		StringBuffer sb = new StringBuffer();
		for(int i=20 ; i>=0 ; i--) {
			if(n>=Math.pow(2,i)) {
				n-= Math.pow(2, i);
				sb.append("1");
			}else sb.append("0");
		}
		return sb.toString();
	}
	
	public static int gcd(int a , int b) {
		if(a==0) return b;
		else return gcd(b%a,a);
	}

}
