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

public class First {

	public static void main(String[] args) throws Exception {

//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//		int t = Integer.parseInt(br.readLine());
//		while (t-- > 0) {
//			int n = Integer.parseInt(br.readLine());
//			String[] str = (br.readLine()).trim().split(" ");
//			int[] arr = new int[n];
//			for (int i = 0; i < n; i++) {
//				arr[i] = Integer.parseInt(str[i]);
//			}
//		}
//		return;

//		first();
		sec();
//		third();
//		four();
	}

	private static void first() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while (t-- > 0) {
			int n = Integer.parseInt(br.readLine());
			String[] str = (br.readLine()).trim().split(" ");
			int[] arr = new int[n];
			long zero = 0, one = 0;
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(str[i]);
				if (arr[i] == 0)
					zero += 1;
				else
					one += 1;
			}
			StringBuilder sb = new StringBuilder();
			if (zero < one) {
				if (zero % 2 == 0) {
					sb.append(n - zero);
					sb.append("\n");
					for (int i = 0; i < n - zero; i++) {
						sb.append("1 ");
					}
				} else {
					sb.append(n - zero - 1);
					sb.append("\n");
					for (int i = 0; i < n - zero - 1; i++) {
						sb.append("1 ");
					}
				}
			} else if (one < zero) {
				if (one % 2 == 0) {
					sb.append(n - one);
					sb.append("\n");
					for (int i = 0; i < n - one; i++) {
						sb.append("0 ");
					}
				} else {
					sb.append(n - one - 1);
					sb.append("\n");
					for (int i = 0; i < n - one - 1; i++) {
						sb.append("0 ");
					}
				}
			} else {
				sb.append(n / 2);
				sb.append("\n");
				for (int i = 0; i < n / 2; i++) {
					sb.append("0 ");
				}
			}
			System.out.println(sb);

		}
	}

	public static void sec() throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while (t-- > 0) {
			int n = Integer.parseInt(br.readLine());
			String[] str = (br.readLine()).trim().split(" ");
			int[] arr = new int[n];
			int max = 0, id = -1;
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(str[i]);
				if (max < arr[i]) {
					max = arr[i];
					id = i;
				}

			}
			
			StringBuilder sb = new StringBuilder();
			sb.append(max + " ");
			boolean[] vis = new boolean[n];
			int g = max;
			vis[id] = true;
			for (int i = 1; i < n; i++) {
				int idx = -1, cg = 0, ans = 0;
				for (int j = 0; j < n; j++) {
					if(vis[j]) continue;
					ans = gcd(g, arr[j]);
					if (ans > cg) {
						cg = ans;
						idx = j;
					}
				}
				sb.append(arr[idx] + " ");
				g = cg;
				vis[idx] = true;

			}
			System.out.println(sb);

		}
		return;
	}

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

	}

}