#include <bits/stdc++.h>
using namespace std;

int findMin(vector<int> data, int n) {
	int temp = 0;
	int idx = 0;
	for(int i = 0; i < data.size(); i++) {
		int res = __gcd(n, data[i]);
		if(res > temp) {
			temp = res;
			idx = i;
		}
	}
	
	return idx;
}

void print(vector<int> data) {
	for(int i = 0; i < data.size(); i++) {
		if(i) cout << " ";
		cout << data[i];
	}
	cout << endl;
}

int main() {
	// your code goes here
	
	int t;
	cin >> t;
	while(t--) {
		vector<int> data, ans;
		int n, nilai;
		cin >> n;
		for(int i = 0; i < n; i++) {
			cin >> nilai;
			data.push_back(nilai);
		}
		
		if(n == 1) {
			cout << data[0] << endl;
			continue;
		}
		int aa = data[0];
		sort(data.begin(), data.end(), greater<int>());
		
		if(data[0] == aa){
			ans.push_back(data[1]);
			data.erase (data.begin() + 1);
		} else {
			ans.push_back(data[0]);
			data.erase (data.begin());
		}
		
		while(data.size() > 0) {
			int temp = ans[ans.size() - 1];
			int res = findMin(data, temp);
			ans.push_back(data[res]);
			data.erase (data.begin() + res);
		}
		print(ans);
	}
	
	return 0;
}