import java.util.*;
import java.io.*;
public class b {
	static final FS sc = new FS();
	static final PrintWriter pw = new PrintWriter(System.out);
	
	static int n;
	static int[] arr;
	static boolean[] used;
	public static void main(String[] args) {
		int t = sc.nextInt();
		for(int tt = 1; tt <= t; ++tt) {
			n = sc.nextInt();
			arr = new int[n];
			for(int i = 0; i < n; ++i) arr[i] = sc.nextInt();
			
			used = new boolean[n];
			int gcd = 0;
			for(int i = 0; i < n; ++i) {
				int max = -1, maxI = -1;
				for(int j = 0; j < n; ++j) {
					if(!used[j]) {
						int currGcd = gcd(gcd, arr[j]);
						if(currGcd > max) {
							max = currGcd;
							maxI = j;
						}
					}
				}
				
				pw.print(arr[maxI] + " ");
				used[maxI] = true;
				gcd = gcd(gcd, arr[maxI]);
			}
			pw.println();
		}
		pw.flush();
	}
	static int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}
	static class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while(!st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(Exception e) {}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}