#include <bits/stdc++.h>

using namespace std;

#define sz(c) ((int)c.size())
#define	pb push_back

typedef pair<int,int> ii;
typedef pair<int,ii> iii;

typedef long long ll;
typedef pair<ll,ll> pll;

typedef vector<int> vi;
typedef vector<ii> vii;

constexpr int MAX = 1010;
constexpr int MAX_N = 100'010;
constexpr int SIGMA = 30;
constexpr ll MAX_LL = 9223372036854775807;
constexpr ll INF = 1'000'000'000;
constexpr ll P = 1'000'000'007;
constexpr double PI = 3.14159265359;
constexpr double TO_RAD = PI/180.;
constexpr long double EPS = 1e-6;

int gcd(int a, int b){
  if(b == 0){
    return a;
  }
  return gcd(b, a % b);
}

int A[1010];
bool gone[1010];
int main(){
	// ios_base::sync_with_stdio(0);
	// cin.tie(0);

	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		
		int mxi = 0;
		for(int i = 0; i < n; i++){
			cin >> A[i];			
			if(A[i] > A[mxi]){
				mxi = i;
			}
		}
		memset(gone,false,sizeof(gone));
		gone[A[mxi]] = true;

		vi ans;
		ans.pb(A[mxi]);
		while(sz(ans) < n){
			int b = 0, bi = -1;
			for(int i = 0; i < n; i++){
				if(gone[A[i]]) continue;
				int g = gcd(ans.back(), A[i]);
				if(g > b || (g == b && A[i] > A[bi])){					
					b = g;
					bi = i;
				}
			}
			
			gone[A[bi]] = true;
			ans.pb(A[bi]);
		}

		for(auto i : ans){
			cout << i << " ";
		}
		cout << "\n";
	}
	return 0;
}