#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pie (3.141592653589)
using namespace std;

const int N = 2e3 + 10;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout << fixed;
	cout.precision(10);
	int T;
	cin >> T;
	while(T--)
	{
		int n;
		cin >> n;
		int a[n];
		vector<int> used(N);
		for(int i = 0; i < n; i++)
		{
			cin >> a[i];
			used[a[i]]++;
		}
		sort(a, a + n, greater<int>());
		vector<int> sol(n);
		sol[0] = a[0];
		int prv = a[0];
		for(int i = 1; i < n; i++)
		{
			prv = __gcd(prv, sol[i - 1]);
			int gcd = 0;
			int no = 0;
			for(int j = 1; j < n; j++)
			{
				int new_gcd = __gcd(prv, a[j]);
				if(used[a[j]] and new_gcd > gcd)
				{
					gcd = new_gcd;
					no = a[j];
				}
			}
			sol[i] = no;
			used[no]--;
		}
		for(auto i : sol)
		{
			cout << i << " ";
		}
		cout << "\n";
	}
	return 0;
}
