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

void solve(){
	ll n, k;
	cin >> n;
	vector<ll> a(n);
	ll c = 0;
	for(auto &x: a) {
		cin >> x;
	}
	sort(a.begin(), a.end(), greater<ll>());
	
	for(int i = 0; i < n-2; i++){
		ll gg = 0, x = 0;
		for(int j = i+1; j < n; j++){
			if(__gcd(a[i], a[j]) > gg){
				gg = __gcd(a[i], a[j]);
				x = j;
			}
		}
		swap(a[i+1], a[x]);
	}
	for(int i = 0; i < n; i++){
		cout << a[i] << " ";
	}
	return;
	
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int tt = 1;
	cin >> tt;
	while(tt--){
		solve();
		cout << '\n';
	}
}
