/*
======================
[     ___T_          ]
[    | 6=6 | =>HI :-)] 
[    |__o__|         ]
[ >===]__o[===<      ]
[     [o__]          ]
[      .".           ]
[      |_|           ]
[                    ]
======================
 */

#include<bits/stdc++.h>
using namespace std;
using lli = int64_t;
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }

int main() {
	 ios::sync_with_stdio(false);
	 cin.tie(nullptr);
	 cout.tie(nullptr);
	 int T;
	 cin >> T;
	 for(int t = 1; t <= T; t++) {
		 int n;
		 cin >> n;
		 int input[n + 1];
		 for(int i = 1; i <= n; i++){
			 cin >> input[i];
		 }
		 bool visited[n + 1] = {0};
		 for(int i = 1; i <= n; i++){
			 int idx = -1,res = 0,_max = 0;
			 for(int j = 1; j <= n; j++){
				 if(visited[j]){
					 continue;
				 }
				 res = gcd(res,input[j]);
				 if(res > _max){
					 _max = res;
					 idx = j;
				 }
			 }
			 visited[idx] = true;
			 cout << input[idx] << " ";
		 }
		 cout << "\n";
	 }
}
