import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader reader = new Reader(System.in);
		PrintWriter print = new PrintWriter(System.out);
		Solver solver = new Solver();
		solver.solve(reader, print);

		print.close();
	}

	static class Solver {
		public void solve(Reader reader, PrintWriter print) throws IOException {
			int __; __ = reader.nextInt();
			while (__-- > 0) {
				int n; n = reader.nextInt();
				boolean[] f = new boolean [n];
				Integer[] a = new Integer [n];
				for (int i = 0; i < n; ++i) a[i] = reader.nextInt();
				int tmp = (int) Collections.max( Arrays.asList(a) );
				for (int i = 0; i < n;	 ++i) {
					int pos = 0, max = 0;
					for (int j = 0; j < n; ++j) {
						if (! f[j]) {
							int tt = gcd(tmp, a[j]);
							if (tt > max) {
								pos = j;
								max = tt;
							}
						}
					}
					tmp = max;
					f[pos] = true;
					print.print(a[pos] + " ");
				}
				print.println();
			}
		}
		public int gcd (int a, int b) {
			return b != 0 ? gcd(b, a % b) : a;
		}
	}

	static class Reader {
		BufferedReader reader;
		StringTokenizer tokenizer;

		public Reader(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
			tokenizer = null;
		}

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

		public boolean hasNext() throws IOException {
			String str;
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				str = reader.readLine();
				if (str == null)
					return false;
				tokenizer = new StringTokenizer(str);
			}
			return true;
		}

		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());
		}
	}
}
  	          	      		    			 	