import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class C669_2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t > 0) {
			t--;
			int n = sc.nextInt();
			int arr[] = new int[n];
			boolean v[] = new boolean[n];
			int max= -1, index = -1;
			for (int i = 0; i < n; i++) {
				arr[i] = sc.nextInt();
				if (arr[i] > max)  {
					max = arr[i];
					index = i;
				}
			}
			v[index] = true;
			int sol[] = new int[n];
			sol[0] = max;
			int prev = sol[0];
			for (int i = 1; i < n; i++) {
				int mGcd = 0;
				for (int j = 0; j < n; j++) {
					if (v[j]) continue;
					int temp = gcd(arr[j], prev);
					//System.out.print(prev+" "+j+"a");
					if (temp > mGcd) {
						mGcd = temp;
						index = j;
					}
				}
				v[index] = true;
				prev = mGcd;
				//System.out.println(prev);
				sol[i] = arr[index];
			}
			for (int i = 0; i < n;i++) {
				System.out.print(sol[i]+" ");
			}
			System.out.println();
		}
	}
	 static int gcd(int a, int b) 
	    { 
	      if (b == 0) 
	        return a; 
	      return gcd(b, a % b);  
	    } 
	      

}
