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

public class B1407 {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			int[] arr = sc.nextIntArr(n);
			int prev = 0;
			boolean[] taken = new boolean[n];
			int[] ans = new int[n];
			for (int i = 0; i < n; i++) {
				int max = -1;
				int idxmax = -1;
				for (int j = 0; j < n; j++) {
					if (!taken[j]) {
						int gcd = gcd(prev, arr[j]);
						if (gcd > max) {
							max = gcd;
							idxmax = j;
						}
					}
				}
				ans[i] = arr[idxmax];
				taken[idxmax] = true;
				prev = max;
			}
			for (int x : ans) {
				pw.print(x + " ");
			}
			pw.println();
		}
		pw.close();
	}

	public static int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

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

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

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

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

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}
