import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;

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

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }


    public static void main(String[] args) {
        FastReader In = new FastReader();
        int t    = In.nextInt();
        for (int i = 0; i < t; i++) {
            int n = In.nextInt();
            HashMap<Integer,Integer> hash = new HashMap<>();
            for (int j = 0; j < n; j++) {
                int number = In.nextInt();
                hash.put(number,hash.getOrDefault(number,0)+1);
            }
            Set<Integer> set = hash.keySet();
            int gcd = -1;
            for(Integer j: set){
                gcd = Math.max(gcd, j);
            }
            int[] arr = new int[n];
            arr[0] = gcd;
            int index = 1;
            hash.replace(gcd, hash.get(gcd)-1);
            if(hash.get(gcd) == 0){
                set.remove(gcd);
                hash.remove(gcd);
            }
            HashMap<Integer, Integer> common = new HashMap<>();
            for(int j: set){
                int value = gcd(gcd,j);
                if(value != 1){
                    common.put(j, hash.get(j));
                }
            }
            for(int j: common.keySet()){
                set.remove(j);
            }
            while(!common.isEmpty()){
                int max = -1;
                int aux = -1;
                for(int j:common.keySet()){
                    if(gcd(gcd,j) >= max){
                        max = gcd(gcd,j);
                        aux = j;
                    }
                }
                arr[index] = aux;
                index++;
                common.replace(aux,common.get(aux)-1);
                if(common.get(aux) == 0){
                    common.remove(aux);
                }
                gcd = max;
            }
            for(int j: set){
                while(hash.get(j) != 0) {
                    arr[index] = j;
                    index++;
                    hash.replace(j,hash.get(j)-1);
                }
            }
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < n; j++) {
                str.append(arr[j]);
                if(j != n-1)
                    str.append(" ");
            }
            System.out.println(str);
        }
    }

    public static int gcd(int n1, int n2) {
        if (n2 != 0)
            return gcd(n2, n1 % n2);
        else
            return n1;
    }
}
