import java.io.*;
import java.util.*;
 
public class Main {
    static final long MOD = 1000000007L;
    static final int INF = 50000000;
    static final int NINF = -50000000;
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        PrintWriter pw = new PrintWriter(System.out);
        
        int Q = sc.ni();
        for (int q = 0; q < Q; q++) {
            int N = sc.ni();
            long[] nums = new long[N];
            for (int i = 0; i < N; i++)
            	nums[i] = sc.nl();
            boolean[] taken = new boolean[N];
            int t = 0;
            for (int i = 1; i < N; i++) {
            	if (nums[i] > nums[t])
            		t = i;
            }
            long[] ans = new long[N];
            taken[t] = true;
            ans[0] = nums[t];
            
            long gcd = ans[0];
            for (int i = 1; i < N; i++) {
            	long g = 0;
            	int take = -1;
            	for (int j = 0; j < N; j++) {
            		if (taken[j])continue;
            		if (gcd(gcd,nums[j]) > g) {
            			g = gcd(gcd,nums[j]);
            			take = j;
            		}
            	}
            	ans[i] = nums[take];
            	gcd = gcd(gcd,nums[take]);
            	taken[take] = true;
            }
            
            for (long a: ans) {
            	pw.print(a + " ");
            }
            pw.println();
        }

        pw.close();
    }
    
    //Find the GCD of two numbers
    public static long gcd(long a, long b) {
      if (a < b)
        return gcd(b,a);
      if (b == 0)
        return a;
      else
        return gcd(b,a%b);
    }
 
    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
 
        public FastScanner() {
            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 ni() {
            return Integer.parseInt(next());
        }
 
        long nl() {
            return Long.parseLong(next());
        }
 
        double nd() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}