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

public class Main {

	public static void main(String[] args) {
		InputReader in = new InputReader(System.in);
		int t = in.nextInt();
		while(t-- > 0) {
			int n = in.nextInt();
			int[] a = new int[n];
			a[0] = in.nextInt();
			int max = 0;
			for(int i = 1; i < n; i++) {
				a[i] = in.nextInt();
				if(a[max] < a[i]) {
					max = i;
				}
			}
			swapArrayElements(a, 0, max);
			int k, l, current = a[0];
						
			for(int i = 0; i < n-1; i++) {
				max = i+1;
				k = gcd(current, a[max]);
				for(int j = i+1; j < n; j++) {
					l = gcd(current, a[j]);
					if(l > k) {
						swapArrayElements(a, max, j);
						max = j;
						k = l;
					}
				}
				if(current == 1) {
					break;
				}
			}
			for(int val : a) {
				System.out.print(val + " ");
			}
			System.out.println();
			
		}
	}
	
	
	static int[] reverseArray(int[] a) {
		int[] temp = new int[a.length];
		for(int i = 0; i < a.length; i++) {
			temp[i] = a[a.length - 1- i];
		}
		return temp;
	}
	
	static void swapArrayElements(int[] a, int i, int j){
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	
	static int gcd(int a, int b) {
		if(b > a) {
			if(b%a == 0) {
				return a;
			}else {
				return gcd(b%a, a);
			}
		}else {
			if(a%b == 0) {
				return b;
			}else {
				return gcd(a%b, b);
			}
		}
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

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

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

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

	String nextLine() {
		String str = "";
		try {
			str = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

}