#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	std::ios::sync_with_stdio(false);
	int T,n;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		vector<int> v(n);
		for (int i=0; i<n; i++){
			scanf("%d",&v[i]);
		}
		if (n==1) printf("%d\n",v[0]);
		else{
			sort(v.begin(), v.end());
			int k = n-2;
			int gcd = 1;
			int pos = 0;
			for (int i=0; i<n-1; i++){
				if (__gcd(v[i],v[n-1])>gcd){
					gcd = __gcd(v[i],v[n-1]);
					pos = i;
				}
			}
			int temp = v[n-2];
			v[n-2] = v[pos];
			v[pos] = temp;
			while (k>=1){
				gcd = __gcd(gcd,v[0]);
				for (int i=0; i<k; i++){
					if (__gcd(v[i],gcd)>gcd){
						pos = i;
						gcd = __gcd(v[i],gcd);
					}
				}
				int temp = v[k-1];
				v[k-1] = v[pos];
				v[pos] = temp;
				k--;
			}
			reverse(v.begin(), v.end());
			for (int i=0; i<n; i++){
				printf("%d ",v[i]);
			}
			printf("\n");
		}
	}
	return 0;
}