#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define in auto&it
#define out auto it
#define allr(x) (x).rbegin(),(x).rend()
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
const double pi = acos(-1);
#define err(x) cerr<<#x<<" = "<<x<<'\n';
int main() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
		freopen("error.txt", "w", stderr);
	#endif
	fastio
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		vector<int>a(n),b(n,0),ans(n);
		for(int i=0;i<n;++i) cin>>a[i];
		int cnt=0;
		for(int i=0;i<n;++i){
			int mx=0,index=0;
			for(int j=0;j<n;++j){
				//if(j==0) cerr<<cnt<<" "<<b[j];
				if(!b[j] && __gcd(cnt,a[j])>mx){
					mx=__gcd(cnt,a[j]);
					index=j;
				}
			}
			ans[i]=a[index];
			cnt=a[index];	
			b[index]=1;
		}
		for(int i=0;i<n;++i) cout<<ans[i]<<" ";
		cout<<'\n';
	} 
	return 0;
}