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

public class B {
	public static int gcd(int a,int b) {
		if(a==0) return b;
		else return gcd(b%a,a);
	}
	public static void main(String[] args) {
		FastReader in = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		int T = in.nextInt();
		for(int tt=0;tt<T;tt++) {
			int n = in.nextInt();
			int[] arr = new int[n];
			int max = 0;
			for(int i=0;i<n;i++) {
				arr[i] = in.nextInt();
				max = Math.max(arr[i], max);
			}
			ArrayList<Pair> ans = new ArrayList<Pair>();
			for(int i=0;i<n;i++) {
				ans.add(new Pair(gcd(max,arr[i]),arr[i]));
			}
			Collections.sort(ans,Collections.reverseOrder());
			for(int i=0;i<ans.size();i++) {
				out.print(ans.get(i).b+" ");
			}
//			for(int i=0;i<n;i++) {
//				if(!used[i]) out.print(arr[i]+" ");
//			}
			out.println();
		}
		out.close();
	}

	static final Random random=new Random();
	//	static void ruffleSort(Pair[] a) {
	//		int n=a.length;//shuffle, then sort 
	//		for (int i=0; i<n; i++) {
	//			int oi=random.nextInt(n);
	//			Pair temp=a[oi];
	//			a[oi]=a[i]; a[i]=temp;
	//		}
	//		Arrays.sort(a);
	//	}
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	static void ruffleSort(char[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n);
			char temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	static 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; 
		} 

		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
	}
}

class Pair implements Comparable<Pair>{
    int a;
    int b;
    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int compareTo(Pair o) {
        if(this.a==o.a)
            return this.b - o.b;
        return this.a - o.a;
    }   
}