import java.io.*;
import java.util.*;
public class Pb{
	public static void main(String[] args) throws IOException{
		FastReader sc = new FastReader();
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out),"ASCII"),512);
		int t = sc.nextInt();
		while(t-- > 0){
			int n = sc.nextInt();
			int[] a = new int[n];
			int max = 0;
			int index = -1;
			StringBuilder op = new StringBuilder();
			for(int i=0;i<n;i++){
				a[i] = sc.nextInt();
				if(a[i] > max){
					max = a[i];
					index = i;
				}
			}
			op.append(max + " ");
			a[index] = 0;
			int temp = max;
			int d = 1;
			while(d != n){
				int g = 0;
				index = -1;
				for(int i=0;i<n;i++){
					if(a[i] == 0) continue;
					int x = gcd(temp, a[i]);
					if(x > g){
						g = x;
						index = i;
					}
				}
				if(g == 1){
					op.append(a[index] + " ");
					a[index] = 0;
					for(int i=0;i<n;i++){
						if(a[i] != 0) op.append(a[i] + " ");
					}
					break;
				}
				temp = g;
				op.append(a[index] + " ");
				a[index] = 0;
				d++;
			}
			out.write(op + "");
			out.write('\n');
		}
		out.flush();
	}
	static int gcd(int a, int b){ 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    }
}
class FastReader{
	BufferedReader br;
	StringTokenizer st;
	public FastReader(){
		br = new BufferedReader(new InputStreamReader(System.in));
	}
	String next(){
		while(st == null || !st.hasMoreElements()){
			try{ 
				st = new StringTokenizer(br.readLine()); 
			} 
			catch (IOException  e){ 
				e.printStackTrace(); 
			} 
		} 
		return st.nextToken();
	}
	int nextInt(){ 
        return Integer.parseInt(next()); 
	}
    long nextLong(){ 
        return Long.parseLong(next()); 
    }
    double nextDouble(){ 
        return Double.parseDouble(next()); 
    }
    String nextLine(){ 
        String str = ""; 
        try{ 
            str = br.readLine(); 
        } 
		catch (IOException e){ 
            e.printStackTrace(); 
        } 
        return str; 
    }
}