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

public class Main {
	static final FastReader in = new FastReader();
	static final PrintWriter out = new PrintWriter(System.out);
	
	static int gcd(int a, int b) {
		if(b == 0) return a;
		return gcd(b, a%b);
	}
	
	public static void main(String[] args) {
		int t = in.nextInt();
		for(int tt=0; tt<t; tt++) {
			int n = in.nextInt();
			int[] a = readIntArray(n);
			
			Pair[] list = new Pair[n];
			Arrays.sort(a);
			for(int i=0; i<n; i++) {
				list[i] = new Pair(gcd(a[i],a[n-1]),a[i]);
			}
			
			Arrays.sort(list);
			
			for(int i=n-1; i>=0; i--) out.print(list[i].y+" ");
			out.println();
		}
		out.close();
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

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

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	static int power(int a, int b, int mod) {
		if (b == 0)
			return 1;
		long res = power(a, b / 2, mod);
		res = res * res % mod;
		if (b % 2 == 1)
			res = res * a % mod;
		return (int) res;
	}

	static class Pair implements Comparable<Pair> {
		public int x;
		public int y;

		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int compareTo(Pair o) {
			if (x == o.x)
				return Integer.compare(o.y, y);

			return Integer.compare(x, o.x);
		}
	}

	static int[] readIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();
		return a;
	}

	static long[] readLongArray(int n) {
		long a[] = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextLong();
		return a;
	}

	static int[][] readIntMat(int n, int m) {
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				a[i][j] = in.nextInt();

		return a;
	}
}
