import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class B {

	static final FastReader in = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) {
		int t = in.nextInt();
		while(t-->0) {
			solve();
		}
		out.close();
	}
	
	static void solve() {
		
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i=0; i<n; i++) {
			a[i] = in.nextInt();
		}
		int[] gcd = new int[n];
		for(int i=1; i<n; i++) {
			if(a[0]<a[i]) {
				int temp = a[0];
				a[0] = a[i];
				a[i] = temp;
			}
		}
		gcd[0] = a[0];
		
		for(int i=1; i<n; i++) {
			for(int j=i; j<n; j++) {
				int temp = gcd(gcd[i-1],a[j]);
				if(gcd[i]<temp) {
					gcd[i] = temp;
					int temp2 = a[i];
					a[i] = a[j];
					a[j] = temp2;
				}
			}
		}
		for(int i=0; i<n; i++) {
			out.print(a[i]+" ");
		}
		
		out.println();
		
	}
	
	static int gcd(int a,int b) {
		if(a%b==0) {
			return b;
		}
		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;
		}
	}

}
