#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e);
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
void solve(){
	int n;
	cin >> n;
	int arr[n];
	for(int i = 0; i < n; i++)cin >> arr[i];
	int b[n];
	int m = INT_MIN, p = 99;
	for(int i = 0; i < n; i++){
		if(m < arr[i]){
			m = arr[i];
			p = i;
		}
	}
	b[0] = arr[p];
	arr[p] = -1;
	for(int i = 1; i < n-1; i++){
		int gcdmax =INT_MIN;
		int pos = 0;
		for(int j = 0; j < n; j++){
			if(arr[j]==-1)continue;
			if(gcdmax < __gcd(b[i-1],arr[j])){
				gcdmax =  __gcd(b[i-1],arr[j]);
				pos = j;
			}
		}
		b[i] = arr[pos];
		arr[pos] = -1;
	}
	for(int i = 0; i < n;i ++){
		if(arr[i]!=-1)b[n-1] = arr[i];
	}
	for(int i = 0; i < n; i++)cout << b[i] << " ";
	cout << "\n";
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	int t;
	cin >> t;
	while(t--)
	solve();
	
	return 0;
}
