import java.util.*;
import java.io.*;

public class Main {
//        File file = new File("input.txt");
//        Scanner in = new Scanner(file);
//        PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
        FastReader in = new FastReader();
        
        int t = in.nextInt();
        while(t-- > 0) {
        	int n = in.nextInt();
        	int[] a = new int[n], b = new int[n], vis = new int[n];
        	for(int i = 0; i<n; i++) {
        		a[i] = in.nextInt();
        	}
        	int d = 0;
        	for(int i = 0; i<n; i++) {
        		int ind = 0;
        		int max = 0;
        		for(int j = 0; j<n; j++) {
        			if(vis[j]==0 && gcd(d,a[j])>max) {
        				ind = j;
        				max = gcd(d,a[j]);
        			}
        		}
    			b[i] = a[ind];
    			d = max;
    			vis[ind] = 1;
        	}
        	for(int e: b)
        		System.out.print(e+" ");
        	System.out.println();
        }     
        
    }
    
    private static int gcd(int a, int b) {
    	if(b==0) return a;
    	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;
        }
    }
}