
import java.util.*;
import java.awt.Image;
import java.io.*;

public class B69B{
	static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        private 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());
        }

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

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

        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;
        }
    }
	static ArrayList<Integer> primes =  sieve((int)(1e3 +1));
	public static void main(String[] args) {
		FastReader s =new FastReader();
		int t = s.nextInt();
		StringBuilder str = new StringBuilder();
		while(t-- >0) {
			int n = s.nextInt();
			int arr[] = new int[n];
			
			int max = Integer.MIN_VALUE;
			for(int i = 0; i < n; i++) {
				arr[i] = s.nextInt();
				max = Math.max(max, arr[i]);
			}
			int original[] = arr.clone();
			ArrayList<Integer> primeFactors = getPrimeFactors(max);
			int count[] = new int[n];
			Arrays.fill(count, 1);
			for(int p : primeFactors) {
				
				for(int i = 0; i< n; i++) {
					
					if(arr[i] % p == 0) {
						arr[i] /= p;
						count[i] *= p;
					}
				}
				
			}
			
			
			ArrayList<Pair> ansList = new ArrayList<>();
			
			for(int i = 0; i <n; i++) {
				Pair np =  new Pair(count[i],original[i]);
				ansList.add(np);
			}
			Collections.sort(ansList);
			StringBuilder sub = new StringBuilder();
			
			for(Pair pair : ansList) {
				sub.append(pair.value+" ");
			}
			
			
			
			
			str.append(sub+"\n");
		}
		System.out.println(str);
	}
	
	static class Pair implements Comparable<Pair>{
		int count,value;
		
		public Pair(int count,int value) {
			this.count= count;
			this.value = value;
		}
		
		@Override
		public int compareTo(Pair o) {
			Integer ths = this.count;
			Integer other = o.count;
			
			
			if(ths == other) {
				return Integer.compare(o.value, this.value);
			}
			
			return other.compareTo(ths);
		}
	}

	private static ArrayList<Integer> getPrimeFactors(int max) {
		ArrayList<Integer> primeFactors = new ArrayList<>(500);
		
		for(int prime : primes) {
			if(max <= 1) break;
			while(max > 1 && max % prime == 0 ) {
				primeFactors.add(prime);
				max /= prime;
			}
			
		}
			
		
		return primeFactors;
	}

		
	private static ArrayList<Integer> sieve(int n) {
		ArrayList<Integer> list = new ArrayList<>();
		boolean primes[] = new boolean[n+1]; 
		
		
		primes[2] = true;
		
		for(int i = 3; i<=n;i+=2 ) {
			primes[i] = true;
		}
		
		for(int i = 3; i*i<=n; i++) {
			if(primes[i]) {
				
				for(int j = i*i; j<=n; j+=i) {
					primes[j] = false;
				}
				
			}
		}
		for(int i=2;i<=n;i++) {
			if(primes[i]) list.add(i);
		}
		
		
		return list;
	}
	

}

