import java.math.BigInteger; 
import java.util.*;

public class B669Div2Try1 {
    public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();

	while (t-- > 0) {
	    int n = sc.nextInt();
	    List<Integer> list = new ArrayList();

	    for (int i = 0; i < n; i++) {
		list.add(sc.nextInt());
	    }

	    List<Integer> seqB = new ArrayList();
	    int lastElement = 0;
	    int netGCD = 0;
	    while (!list.isEmpty()) {
		int maxGcd = Integer.MIN_VALUE;
		int maxIndex = 0;
		for (int i = 0; i < list.size(); i++) {
		    int currGCD = gcd(netGCD, list.get(i));
		    if (maxGcd < currGCD) {
			maxGcd = currGCD;
			maxIndex = i;
		    }
		}
		lastElement = list.get(maxIndex);
		netGCD = gcd(netGCD, lastElement);
		seqB.add(list.get(maxIndex));
		list.remove(maxIndex);	
	    }
	    for (int element: seqB) {
		System.out.print(element + " " );
	    }
	    System.out.println();
	}
    }

    private static int gcd(int a, int b) {
	// Might be too much time wise.
	return new BigInteger(a + "").gcd(new BigInteger(b + "")).intValue();
    }
}

