

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

public class Main
{
    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(); 
        } 
  
        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; 
        } 
    }

     public static long[] randomize(long arr[])
    {
        Random rand = new Random();
for (int i = 0; i < arr.length; ++i) {
   int index = rand.nextInt(arr.length - i);
   long tmp = arr[arr.length - 1 - i];
   arr[arr.length - 1 - i] = arr[index];
   arr[index] = tmp;
}

return arr;
    } 
    static long mod = 1000000007;

  
    public static void main (String[] args) throws java.lang.Exception
    {
        FastReader sc = new FastReader();
        
        int test=sc.nextInt();
        
        while(test-->0)
        {

            int n = sc.nextInt();
            int a[] = new int[n];
            int currgcd=0;
            int indexgcd=-1;
            for(int i=0;i<n;i++)
            {
                a[i]=sc.nextInt();
                if(currgcd<a[i])
                {currgcd = a[i];
                    indexgcd = i;
                    }
            }
              
            List<Integer> ans = new ArrayList<>();
            ans.add(currgcd);
            a[indexgcd]=-1;

            while(true)
            {
                int index=-1;
                int maxgcd=-1;
                for(int i=0;i<n;i++)
                {
                    if(a[i]==-1)
                        continue;
                    
                     int gcdnow = gcd(currgcd, a[i]);
                    if(maxgcd==-1)
                    {
                        maxgcd = gcdnow;
                        index=i;
                    }
                    else if(gcdnow>maxgcd)
                    {
                        maxgcd = gcdnow;
                        index=i;
                    }


                }
                if(index==-1)
                    break;

                ans.add(a[index]);
                a[index]=-1;
                currgcd = maxgcd;
            }

            for(int i:ans)
                System.out.print(i+" ");

            System.out.println();
         
        }
    }
    
    
    public static int gcd(int a, int b)
    {
        if(b%a==0)
            return a;

        return gcd(b%a, a);
    }
    
}

