import java.util.*;
import java.io.*;
public class vivek{    
    static class FastReader{ 
 
        BufferedReader br; 
        StringTokenizer st; 
 
        public FastReader() 
        { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
 
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
 
        char nextChar()
        {
            return next().charAt(0);
        }
 
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
 
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
 
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
 
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
 
        int[] nextArray(int rows)
        {
            int arr[] = new int[rows];            
            for (int i = 0; i < rows; i++)
            arr[i] = nextInt();
            return arr;
        }
 
        long[] nextLongArray(int rows)
        {
            long arr[] = new long[rows];            
            for (int i = 0; i < rows; i++)
            arr[i] = nextLong();
            return arr;
        }
 
        char[] nextCharArray(int rows)
        {
            char arr[] = new char[rows];
            for (int i = 0; i < rows; i++)
            arr[i] = next().charAt(0);
            return arr;
        }
 
        int[][] nextMatrix(int rows, int columns)
        {
            int mat[][] = new int[rows][columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    mat[i][j] = nextInt();
                }
            }
            return mat;
        }
    }
    static int gcd(int n1,int n2){
        int g=1;
        for(int i = 1; i <= n1 && i <= n2; ++i)
        {
            if(n1 % i==0 && n2 % i==0)
                g = i;
        }
        return g;
    }
    public static void main(String args[]){
        FastReader sc=new FastReader();
        PrintWriter p=new PrintWriter(System.out);
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt();
            int a[]=new int [n];
            int b[]=new int [n];
            for(int i=0;i<n;i++) a[i]=sc.nextInt();
            Arrays.sort(a);
            b[n-1]=a[n-1];
            for(int i=n-2;i>=0;i--){
                b[i]=gcd(a[n-1],a[i]);
            }
            int max=Integer.MIN_VALUE;
            int f=0;
            int div=a[n-1];
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(max<b[j] &&  b[j]!=0 && div % b[j]==0){
                        max=b[j];
                        f=j;
                    }
                    else if(max<b[j] && b[j]!=0 && div%b[j]!=0){
                        b[j]=gcd(div,b[j]);
                        j--;
                    }
                }
                p.print(a[f]+" ");
                div=b[f];
                b[f]=0;
                max=Integer.MIN_VALUE;
            }
            p.println();
        }
        p.flush();
    }
}