/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
public class GFG {
    public static int gcdivisor(int a, int b){
        if(b == 0)return a;
        return gcdivisor(b, a%b);
    }
	public static void main (String[] args) {
		//System.out.println("GfG!");
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		int n, arr[];
		while(t-- != 0){
		    n = sc.nextInt();
		    arr = new int[n];
		    for(int i = 0; i < n; i++)arr[i] = sc.nextInt();
		    int max = Integer.MIN_VALUE;
		    int res[] = new int[n];
		    int in = 0;
		    for(int i = 0; i < n; i++) {
		        if(max < arr[i]){
		            max = arr[i];
		            in = i;
		        }
		    }
		    res[0] = max;
		    int gcd = max;
		    boolean visited[] = new boolean[n];
		    visited[in] = true;
		    for(int i = 1; i < n; i++){
		        boolean same = false;
		        int index = 0, cur, prev = 0;
		        for(int j = 0; j < n; j++){
		            if(!visited[j]){
		                cur = gcdivisor(arr[j], gcd);
		                if(cur > prev){
		                    index = j;
		                    prev = cur;
		                }
		            }
		        }
		        gcd = prev;
		        res[i] = arr[index];
		        visited[index] = true;
		    }
		    for(int i = 0; i < n; i++)System.out.print(res[i] + " ");
		    System.out.println();
		}
	}
}