import java.util.*;
import java.io.*;
import java.math.*;
public class Euler {
	
	
	
    public static void main(String[] args){

        FastReader in = new FastReader();
        PrintWriter o = new PrintWriter(System.out);
        
        int t = in.nextInt();
        
        while(t-- > 0) {
			
			int n = in.nextInt();
			int[] a = new int[n];
			int max = Integer.MIN_VALUE;
			int pos = 0;
			
			for (int i = 0;i < n;i++) {
				a[i] = in.nextInt();
				if (a[i] > max) {
					max = a[i];
					pos = i;
				}
			}
			
			int[] c = new int[n];
			int[] b = new int[n];
			a[pos] = -1;
			
			
			c[0] = max;
			b[0] = max;
			
			for (int i = 1; i < n; i++) {
				
				int val = Integer.MIN_VALUE;
				int p = -1;
				
				for (int j = 0; j < n; j++) {
					
					if (a[j] != -1) {
						int x = gcd(c[i - 1], a[j]);
						if (x > val) {
							p = j;
							val = x;
						}
					}
					
				}
				
				c[i] = val;
				b[i] = a[p];
				a[p] = -1;
				
				
			}
			
			for (int i = 0; i < n;i++) {
				o.print(b[i] + " ");
			}
			o.println();
			
		}
        
        
        o.close();
        o.flush();
        return;


    }
    
    public static int gcd(int a, int b) {
		if (b == 0) return a;
		else return gcd(b, a % b);
	}
    
    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;
        }
    }


}
