import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class bigVova {
	static int gcd(int n, int m) {
		if (n == 0)
			return m;
		if (m == 0)
			return n;
		return gcd(Integer.max(n, m) % Integer.min(n, m), Integer.min(n, m));
	}

	public static void main(String[] args) throws IOException {
		BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(sc.readLine());
		while (--t >= 0) {
			int n = Integer.parseInt(sc.readLine());
			ArrayList<Integer> a = new ArrayList<Integer>(), b = new ArrayList<Integer>();
			StringTokenizer st = new StringTokenizer(sc.readLine());
			for (int i = 0; i < n; i++)
				a.add(Integer.parseInt(st.nextToken()));
			b.add(0);
			for (int i = 0; i < n; i++) {
				int ind = 0;
				for (int j = 0; j < a.size(); j++)
					if (gcd(b.get(i), a.get(j)) > gcd(a.get(ind), b.get(i)))
						ind = j;
				b.add(a.get(ind));
				a.remove(ind);
			}
			for (int i = 1; i < b.size(); i++)
				System.out.print(b.get(i) + " ");
			System.out.println();
		}
	}
}
