#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main ()
{
	int t = {0}, n = {0};
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d", &n);
		int arr[n] = {0}, max = {0}, indexArr[n] = {0}, ansArr[n] = {0}, gcd = {0}, index = {0};
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &arr[i]);
			if (max < arr[i])
			{
				max = arr[i];
			}
		}
		for (int i = 0; i < n; ++i)
		{
			if (max == arr[i])
				indexArr[i] = 1;
		}
		ansArr[0] = max;
		for (int i = 0; i < n - 1; ++i)
		{
			gcd = 0;
			for (int j = 0; j < n; ++j)
			{
				// cout << arr[j] << " = " << indexArr[j] << " ";
				if (indexArr[j] == 0 && __gcd(max, arr[j]) > gcd)
				{
					gcd = __gcd(max, arr[j]);
					index = j;
				}
			}
			ansArr[i + 1] = arr[index]; 
			max = gcd;
			indexArr[index] = 1;
		}
		for (int i = 0; i < n; ++i)
		{
			printf("%d ", ansArr[i]);
		}
		printf("\n");
	}

	return 0;
}