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

public class B {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);
	

	public static void main(String[] args) {
		pre();
		int t = f.nextInt();
		while(t-- > 0)
			solve();
		out.close();
	}

	static ArrayList<Integer> prime = new ArrayList<>();
	static void pre() {
		boolean[] bol = new boolean[1000];
		for(int i=2;i<1000;i++) {
			if(!bol[i])
				for(int j=i+i;j<1000;j+=i) {
					bol[j] = true;
				}
		}

		for(int i=2;i<1000;i++) {
			if(!bol[i])
				prime.add(i);
		}
	}

	static ArrayList<Integer>[] primes;
	static void solve() {
		int n = f.nextInt();
		int[] arr = new int[n];
		int pos = 0;
		for(int i=0;i<n;i++) {
			arr[i] = f.nextInt();
			if(arr[i] > arr[pos]) {
				pos = i;
			}
		}

		out.print(arr[pos]+" ");
		boolean[] use = new boolean[n];
		use[pos] = true;

		primes = new ArrayList[n];
		for(int i=0;i<n;i++) {
			primes[i] = pri(arr[i]);
		}
		int prev = arr[pos];
		for(int k=1;k<n;k++) {
			int maxDiv = 1;
			pos = 0;
			ArrayList<Integer> list = new ArrayList<>();
			for(int i=0;i<n;i++) {
				if(!use[i]) {
					int loc = calc(i, prev);
					if(loc >= maxDiv) {
						pos = i;
						maxDiv = loc;
					}
				}
			}
			prev = maxDiv;
			use[pos] = true;
			out.print(arr[pos]+" ");
		}
		out.println();
	}

	static ArrayList<Integer> pri(int num) {
		ArrayList<Integer> ans = new ArrayList<>();
		for(int i : prime) {
			while(num % i == 0) {
				num /= i;
				ans.add(i);
			}
		}
		return ans;
	}

	static int calc(int a, int b) {
		ArrayList<Integer> first = primes[a];
		ArrayList<Integer> second = pri(b);
		int ans = 1;
		int bPos = 0;
		for(int i : first) {
			while(bPos < second.size() && second.get(bPos) < i) {
				bPos++;
			}

			if(bPos >= second.size()) {
				break;
			} else if(second.get(bPos) == i) {
				ans *= i;
				bPos++;
			} 
		}
		return ans;
	}	

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

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

		String nextLine() {
			String s = "";
			try {
				s = br.readLine();
			} catch(IOException ioe) {
				ioe.printStackTrace();
			}

			return s;
		}

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

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