import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) {
       MyScanner sk = new MyScanner();
       out = new PrintWriter(new BufferedOutputStream(System.out));
       int totalNumOfCases = sk.nextInt();
        for (int i = 0; i < totalNumOfCases; i++) {
            int n = sk.nextInt();
            Vector<Integer> vec = new Vector<>();
            Vector<Integer> finals = new Vector<>();
            int greatest = 0;
            for (int j = 0; j < n; j++) {
                int h = sk.nextInt();
                if (j != 0 && h > vec.get(greatest)) greatest = j;
                vec.add(h);
            }
            finals.add(vec.get(greatest));
            vec.set(greatest, 0);
            int max = finals.get(0);
             for (int k = 1; k < vec.size(); k++) {
                int largNum = 0, index = 0;
                for (int p = 0; p < vec.size(); p++) {
                    if (vec.get(p) != 0 && gcd(max, vec.get(p)) > largNum) {
                        largNum = gcd(max, vec.get(p));
                        index = p;
                    }
                }
                finals.add(vec.get(index));
                vec.set(index, 0);
             }
             for (Integer m: finals) {
                 out.print(m + " ");
             }
            out.println();
       // Stop writing your solution here. -------------------------------------
        }
        out.close();
    }
    public static int gcd (int num, int num2) {
        if (num % num2 == 0) return num2;
        return gcd(num2, num % num2);
    }
    
    
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
       
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
       BufferedReader br;
       StringTokenizer st;
  
       public MyScanner() {
          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;
       }
 
    }
    //--------------------------------------------------------
 }