import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class _669B {
    static BufferedReader br;
    static int gcd(int a, int b){
        if(b==0){
            return a;
        }
        return gcd(b, a%b);
    }
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int t = readInt();
        while(t-->0){
            int n = readInt();
            int []arr = readIntarray();
            ArrayList<Integer> ans = new ArrayList<>();
            int max = 0;
            for(int i = 0; i < arr.length; i++){
                if(arr[max]<arr[i]){
                    max = i;
                }
            }
            ans.add(arr[max]);
            arr[max] = -1;
            int till_gcd = arr[max];
            while(true){
                int to_add = -1;
                int max_gcd = -1;
                for(int j = 0; j < n; j++){
                    if(arr[j]!=-1){
                        int temp_gcd = gcd(till_gcd, arr[j]);
                        if(max_gcd<temp_gcd) {
                            max_gcd = temp_gcd;
                            to_add = j;
                        }
                        if(to_add==-1){
                            to_add=j;
                        }
                    }
                }
                if(to_add!=-1){
                    ans.add(arr[to_add]);
                    till_gcd = gcd(till_gcd, arr[to_add]);
                    arr[to_add] = -1;
                }else{
                    break;
                }
            }
            for(int i = 0; i < n; i++){
                System.out.print(ans.get(i)+" ");
            }
            System.out.println();
        }
    }

    static int readInt() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    static long readLong() throws IOException {
        return Long.parseLong(br.readLine());
    }

    static long[] readLongarray() throws IOException {
        String[] _arr_ = br.readLine().split(" ");
        long[] a = new long[_arr_.length];
        for (int i = 0; i < _arr_.length; i++) {
            a[i] = Long.parseLong(_arr_[i]);
        }
        return a;
    }

    static int[] readIntarray() throws IOException {
        String[] _arr_ = br.readLine().split(" ");
        int[] a = new int[_arr_.length];
        for (int i = 0; i < _arr_.length; i++) {
            a[i] = Integer.parseInt(_arr_[i]);
        }
        return a;
    }
}
