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

public class Main{
	static class Point{
		int x,y,z;
		Point(int nx, int ny, int nz){
			x = nx; y = ny; z = nz;
		}
		@Override
		public boolean equals(Object o) {
			Point p = (Point)o;
			return p.x == x && p.y == y;
		}
	}
	public static void main(String[] args) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int qr = pint(in);
		for(; qr > 0; qr--) {
			int n = pint(in);
			int[] a = new int[n];
			StringTokenizer st = new StringTokenizer(in.readLine());
			for(int i = 0; i < n; i++) {
				a[i] = pint(st);
			}
			int[] out = new int[n];
			boolean[] u = new boolean[n];
			int mk = 0;
			for(int i = 1; i < n; i++) {
				if(a[i] > a[mk]) {
					mk = i;
				}
			}
			u[mk] = true;
			out[0] = a[mk];
			int g = out[0];
			for(int i = 1; i < n; i++) {
				mk = -1;
				int mx = 0;
				for(int j = 0; j < n; j++) {
					if(u[j]) {continue;}
					int gd = gcd(g,a[j]);
					if(gd > mx) {
						mk = j;
						mx = gd;
					}
				}
				u[mk] = true;
				out[i] = a[mk];
				g = mx;
			}
			for(int i = 0; i < n; i++) {
				System.out.print(out[i] + " ");
			}
			System.out.println();
		}
	}
	static int gcd(int a, int b) {
		if(b == 0) {return a;}
		return gcd(b, a % b);
	}
	static int[] resize(int[] a) {
		int[] r = new int[a.length * 2];
		for(int i = 0; i < a.length; i++) {
			r[i] = a[i];
		}
		return r;
	}
    static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());}
    static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());}
}