//Sort Array,
//        Take largest as first element
//        gcd1 = largest
//
//        find in remaining array the maximum gcd of(gcd1, ai)
//        b1, b2
//        gcd2 = gcd(b1, b2)
//
//        iterate in remaining array and find maximum of (gcd2, ai);
//
//        once the gcd == 1 , add all remaining elemnt to result array




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

public class CF669B {
   public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        BufferedReader br = new BufferedReader(new FileReader("test.dat"));
       int t = Integer.parseInt(br.readLine());
       while (t-- > 0) {
           int n = Integer.parseInt(br.readLine());
			int arr[] = new int[n];
			StringTokenizer st = new StringTokenizer(br.readLine());
			for(int i = 0 ; i < n ; i++)
				arr[i] = Integer.parseInt(st.nextToken());
			findAndPrint(arr, n);           
       }
   }

		public static void print(int arr[], int n) {
			for (int i = 0 ; i < n ; i++){
				System.out.print(arr[i]+" ");
			}
		System.out.println();
	
	
}


   














public static void findAndPrint(int arr[], int n) {

		Arrays.sort(arr);
		int resultArr[] = new int[n];
		resultArr[0] = arr[n-1];
		arr[n-1] = -1;
		int gcd = resultArr[0];
		int i = 1;
		for( ; i < n ; i++) {
			if(gcd == 1) break;


			resultArr[i] = getNumWithMaxGCD(arr, gcd, n);
			gcd = GCD(gcd, resultArr[i]);
		}

		int counter = 0;
		for( ; i < n ; i++){
		while(arr[counter]==-1) counter++;
		resultArr[i] = arr[counter++];
		}
		print(resultArr, n);
	}
    public static int GCD(int a, int b){
		if(b>a){
		  return GCD(b,a);
		}
		if(b == 0) return a;
		else return GCD(b, a%b);
	}
   public static int getNumWithMaxGCD(int arr[], int gcd, int n) {
		 int maxGCD = 0;
	       int pos = -1;
      for(int i = 0 ; i < n ; i++){
			if(arr[i] != -1){
				int currentGCD = GCD(arr[i], gcd);
				if(currentGCD > maxGCD){
					maxGCD = currentGCD;
					pos = i;
                }
					
           }
		}
		int temp = arr[pos];
		arr[pos] = -1;
		return temp;
   }

   }
