
import java.io.*;
import java.util.*;
public class Solution {
		static int MAX = (int)1e5+1;
		public static void main(String[] args)throws Exception{
			InputReader input = new InputReader(System.in);
			OutputStreamWriter out = new OutputStreamWriter(System.out);
			int test = input.readInt();
			while(test-->0) {
				int n = input.readInt();
				int arr[] = new int[n];
				for(int i =0;i<n;i++) {
					arr[i] = input.readInt();
				}
				Arrays.sort(arr);
				int res[] = new int[n];
				int z = 0;
				for(int i =n-1;i>=0;i--) {
					res[z++] = arr[i];
				}
				ArrayList<Integer>list = new ArrayList<>();
				list.add(res[0]);
				int gcd = res[0];
				while(true) {
					int mx = -1;
					int index = -1;
					for(int i =1;i<n;i++) {
						int ans = gcd(gcd,res[i]);
						if(ans>mx) {
							index = i;
							mx = ans;
						}
					}
					
						if(mx == 1 || index == -1)break;
						gcd = mx;
						list.add(res[index]);
						res[index] = -1;
						
					
				}
				for(int it:list)System.out.print(it+" ");
				for(int it:res) {
					if(!list.contains(it) && it != -1)System.out.print(it+" ");
				}
				System.out.println();
			}
		}
		public static int gcd(int a,int b) {
			return b == 0?a:gcd(b,a%b);
		}
		public static void swap(int a,int b,int arr[]) {
			int t = arr[a];
			arr[a] = arr[b];
			arr[b] = t;
		}
}

class InputReader {
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	
	public int read() {
		if (numChars == -1) {
			throw new InputMismatchException();
		}
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0) {
				return -1;
			}
		}
		return buf[curChar++];
	}
	
	public int readInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9') {
				throw new InputMismatchException();
			}
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	
	public String readString() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	
	public double readDouble() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		double res = 0;
		while (!isSpaceChar(c) && c != '.') {
			if (c == 'e' || c == 'E') {
				return res * Math.pow(10, readInt());
			}
			if (c < '0' || c > '9') {
				throw new InputMismatchException();
			}
			res *= 10;
			res += c - '0';
			c = read();
		}
		if (c == '.') {
			c = read();
			double m = 1;
			while (!isSpaceChar(c)) {
				if (c == 'e' || c == 'E') {
					return res * Math.pow(10, readInt());
				}
				if (c < '0' || c > '9') {
					throw new InputMismatchException();
				}
				m /= 10;
				res += (c - '0') * m;
				c = read();
			}
		}
		return res * sgn;
	}
	
	public long readLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9') {
				throw new InputMismatchException();
			}
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	
	public boolean isSpaceChar(int c) {
		if (filter != null) {
			return filter.isSpaceChar(c);
		}
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	
	public String next() {
		return readString();
	}
	
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
