
import java.util.*;
import java.lang.*;
public class Big_Vollo
{
    //m>n
    static int gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0)
            return b;
        if (b == 0)
            return a;

        // base case
        if (a == b)
            return a;

        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        int test=in.nextInt();

        while(test-- > 0){


            int n=in.nextInt();
            int arr[]=new int[n];

            for (int i=0;i<n;i++){
                arr[i]=in.nextInt();
            }

            int d=0;

            boolean visited [] =new boolean[n];
            Arrays.fill(visited,false);
            int ans[]=new int[n];

            for (int i=0;i<n;i++){
                int ind=0;
                int max=0;
                for (int j=0;j<n;j++){
                    if(!visited[j]&&gcd(d,arr[j])>max){
                        max=gcd(d,arr[j]);
                        ind=j;
                    }
                }
                d=max;
                visited[ind]=true;
                ans[i]=arr[ind];
            }
            for (int i=0;i<n;i++){
                System.out.print(ans[i]+" ");
            }
            System.out.println();


        }
    }
}
