
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

public class Main {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int T;
	int N;
	int[] items;
	String[] answers;
	
	void init() {
		try {
			T = Integer.parseInt(br.readLine());
			answers = new String[T+1];
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void input() {
		try {
			N = Integer.parseInt(br.readLine());
			items = new int[N+1];
			String[] split = br.readLine().split("\\s");
			for (int i=1; i<=N; i++) {
				items[i] = Integer.parseInt(split[i-1]);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void solve(int index) {
		int maxNum = -1;
		List<Integer> list = new LinkedList<>();
		for (int i=1; i<=N; i++) {
			if (items[i] > maxNum) {
				list.add(0, items[i]);
				maxNum = items[i];
			} else {
				list.add(items[i]);
			}
		}
		
		List<Integer> ansList = new LinkedList<>();
		ansList.add(maxNum);
		
		int gcdValue = maxNum;
		while (ansList.size() < N) {
			int idx = -1;	int maxGcd = -1; 
			for (int i=1; i<list.size(); i++) {
				int value = gcd(gcdValue, list.get(i));
				
				if (value > maxGcd) {
					idx = i;
					maxGcd = value;
				}
			}
			gcdValue = maxGcd;
			ansList.add(list.get(idx));
			list.remove(idx);
		}
		
		StringBuilder builder = new StringBuilder();
		for (int i=0; i<ansList.size(); i++) {
			builder.append(ansList.get(i)).append(" "); 
		}
		answers[index] = builder.toString();
	}
	
	public int gcd(int a,int b){
	      if(a % b == 0)
	          return b;
	      return gcd(b,a%b);
	}
	
	void print() {
		for (int i=1; i<=T; i++) {
			System.out.println(answers[i]);
		}
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Main m = new Main();
		m.init();
		for (int i=1; i<=m.T; i++) {
			m.input();
			m.solve(i);
		}
		m.print();

	}

}
