#include <string>
#include <iostream>
#include <set>
#include <cstdlib>
#include <cctype>
#include <vector>
#include <cmath>
#include <climits>
#include <algorithm>
#include <set>
using namespace std;

vector<int> sort(vector<int>); // sorts the vector given as argument and returns it

typedef long long int lli;


bool contains(string s, char c) {
	for(size_t i(0); i < s.size(); i++) {
		if(s[i] == c) {
			return true;
		}
	}
	return false;
}


int maxElem(vector<int> vec) {
	int max(-1);
	for(size_t i(0); i < vec.size(); i++) {
		if(max < vec[i]) {
			max = vec[i];
		}
	}
	return max;
}

int index_of(int elem, vector<int> vec) {
	for(size_t i(0); i < vec.size(); i++) {
		if(vec[i] == elem) {
			return i;
		}
	}
	return -1;
}

int count(int elem, vector<int> vec) {
	int count(0);
	for(size_t i(0); i < vec.size(); i++) {
		if(vec[i] == elem) {
			count++;
		}
	}
	return count;
}

vector<int> cds(int a, int b) {
	vector<int> res;
	if(a < b) {
		for(int i(1); i <= a; i++) {
			if(a % i == 0 && b % i == 0) {
				res.push_back(i);
			}
		}
	}
	else {
		for(int i(1); i <= b; i++) {
			if(b % i == 0 && a % i == 0) {
				res.push_back(i);
			}
		}
	}
	return res;
}

int factorial(size_t n) {
	if(n == 0 || n == 1) {
		return 1;
	}
	else {
		return n * factorial(n-1);
	}
}

void print_vector_int(vector<int> vec) {
	for(size_t i(0); i < vec.size(); i++) {
		cout << vec[i] << " ";
	}
	cout << endl;
}

int gcdElems(vector<int> vec) {
	vector<int> allgcds;
	for(size_t i(0); i < vec.size()-1; i++) {
		for(size_t j(i+1); j < vec.size(); j++) {
			vector<int> cdivs(cds(vec[i], vec[j]));
			for(size_t i(0); i < cdivs.size(); i++) {
				allgcds.push_back(cdivs[i]);
			}
		}
	}

	vector<int> occurences(maxElem(allgcds)+1);
	for(size_t i(0); i < allgcds.size(); i++) {
		occurences[allgcds[i]]++;
	}
	int maxIndex(0);
	for(size_t i(0); i < occurences.size(); i++) {
		if(occurences[i] == (vec.size()*(vec.size()-1))/2) {
			maxIndex = i;
		}
	}
	return maxIndex;

}

int gcdVecElem(vector<int> vec, int elem) {
	if(vec.empty()) {
		return elem;
	}
	vector<int> added(vec);
	added.push_back(elem);
	return gcdElems(added);
}

vector<int> removeElem(vector<int> vec, int elem) {
	vector<int> out;
	bool removed(false);
	for(size_t i(0); i < vec.size(); i++) {
		if((vec[i] == elem && removed) || vec[i] != elem) {
			out.push_back(vec[i]);
		}
		else {
			removed = true;
		}
	}

	return out;
}


int main(void) {
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		vector<int> input(n);
		for(size_t i(0); i < n; i++) {
			cin >> input[i];
		}
		vector<int> output({maxElem(input)});
		input[index_of(maxElem(input), input)] = -1;
		input.pop_back();
		while(output.size() != n) {
			int maxGcd(0);
			int maxElem(0);
			int indexMax(0);
			for(size_t i(0); i < input.size(); i++) {
				if((__gcd(output[output.size()-1], input[i]) > maxGcd)) {
					maxGcd = __gcd(output[output.size()-1], input[i]);
					maxElem = input[i];
					indexMax = i;
				}
			}
			output.push_back(maxElem);
			input[indexMax] = -1;
		}
		for(size_t i(0); i < output.size(); i++) {
			cout << output[i] << " ";
		}
		cout << endl;


	}

	return 0;

}












vector<int> sort(vector<int> vec) {
	bool swapped(true);
	while(swapped) {
		swapped = false;
		for(size_t i(0); i < vec.size()-1; i++) {
			if(vec[i] < vec[i+1]) {
				int temp(vec[i]);
				vec[i] = vec[i+1];
				vec[i+1] = temp;
				swapped = true;}}}
	return vec;
}
