
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class R669{

	public static int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a%b);
	}
	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int t = sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt();
			int a[]=new int[n];
			for(int i=0;i<n;i++)
				a[i]=sc.nextInt();
			Arrays.parallelSort(a);
			pair newa[]=new pair[n];
			for(int i=n-1;i>=0;i--)
			{
				newa[i]=new pair(a[i],gcd(a[i],a[n-1]));
			}
			Arrays.sort(newa);
			for(pair p:newa)
				
				pw.print(p.i+" ");
			pw.println();
		}pw.flush();
	

	


	}
static class pair implements Comparable<pair>
{
	int i;int res;
public pair(int i,int res) {
	// TODO Auto-generated constructor stub
	this.i=i;
	this.res=res;
}
	@Override
	public int compareTo(pair o) {
		// TODO Auto-generated method stub
		return o.res-res;
	}
	
}

	static class Scanner {

		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}

}
