import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;

public class tr0 {
	static PrintWriter out;
	static StringBuilder sb;
	static int mod = 1000000007;
	static long inf = (long) 1e14;
	static int n, a, b, da, db;
	static ArrayList<Integer>[] ad;
	static int[][] memo;
	static boolean f;
	static boolean vis[];
	static int[] sub;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			int[] a = new int[n];
			int max = 0;
			int id = -1;
			for (int i = 0; i < n; i++) {
				a[i] = sc.nextInt();
				if (a[i] > max) {
					max = a[i];
					id = i;
				}
			}
			int ix = 0;
			a[id] = -1;
			int[] pr = new int[n];
			pr[ix++] = max;
			int all = max;
			while (ix < n) {
				max = 0;
				id = -1;
				for (int j = 0; j < n; j++) {
					if (a[j] == -1)
						continue;
					if (gcd(all, a[j]) > max) {
						max = gcd(all, a[j]);
						id = j;
					}
				}
				pr[ix] = a[id];
				a[id] = -1;
				all = gcd(all, max);
				ix++;
			}
			for(int i=0;i<n;i++)
				out.print(pr[i]+" ");
			out.println();
		}
		out.close();
	}

	static int gcd(int a, int b) {

		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public int[] nextArrInt(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextArrLong(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}