import java.util.*;

public class Main {

	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		int[][] in = new int[t][]; 
		for(int i=0;i<t;i++) {
			int n  = sc.nextInt();
			int[] arr = new int[n];
			for(int j=0;j<n;j++) {
				int p = sc.nextInt();
				arr[j]=p;
			}
			in[i] = arr;
		}
		for(int i=0;i<in.length;i++) {
			findans(in[i]);
			System.out.println();
		}
		
		sc.close();
	}
	public static void findans(int[] arr) {
		 int max = Integer.MIN_VALUE;
		ArrayList<Integer> list = new ArrayList<>();
		for(int i=0;i<arr.length;i++) {
			if(arr[i]>max)
				max = arr[i];
			list.add(arr[i]);
		}
		final int max1 = max;
		Collections.sort(list,new Comparator<Integer>() {
			public int compare(Integer a, Integer b) {
				return -1*Integer.compare((Integer)gcd(a,max1),(Integer)gcd(b,max1));
			}
		});
		for(int i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
	}
	public static int gcd(int a, int b) {
		int max =a*b;
		
		int res =1;
		if(b%a==0)
			return a;
		for(int i=1;i<=(int)Math.pow(max, .5);i++) {
			if(a%i==0 && b%i==0)
				res=i;
		}
		return res;
	}
}
