#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long
#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i, n) for(int i=n-1;i>=0;i--)
long long mod=1000000007;


//to find nth fibonacci number.
//This code returns Fn and Fn+1 as a pair.
pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}




ll fact(ll n)
{
    ll res=1;
    for(ll i=n;i>=1;i--)
        (res*=i)%=mod;
    return (res%mod);
 
}

long long binpow(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res%mod) *( a%mod);
        a = (ll)(a%mod) *( a%mod);
        b >>= 1;
    }
    return res%mod;
}







bool isPrime(long long n)
{

    if (n <= 1)
        return false;


    for (long long i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;

    return true;
}

//THIS IS THE CODE TO FIND ALL PRIME NUMBERS IN THE RANGE "L" to "R";

vector<long long > segmentedSieve(long long L, long long R) {
    // generate all primes up to sqrt(R)
    long long lim = sqrt(R);
    vector<bool> mark(lim + 1, false);
    vector<long long> primes;
    for (long long i = 2; i <= lim; ++i) {
        if (!mark[i]) {
            primes.emplace_back(i);
            for (long long j = i * i; j <= lim; j += i)
                mark[j] = true;
        }
    }

    vector<bool> isPrime(R - L + 1, true);
    for (long long i : primes)
        for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
            isPrime[j - L] = false;
    if (L == 1)
        isPrime[0] = false;
    vector<long long >realprime;
   for(int i=0;i<=R-L;i++){
      if(isPrime[i]){
         realprime.emplace_back(i+L);
         
      }
   }
    return realprime;
    
}





bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;


    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}







int ternarySearch(int l, int r, int key, int ar[])

{
    while (r >= l) {

        // Find the mid1 and mid2
        int mid1 = l + (r - l) / 3;
        int mid2 = r - (r - l) / 3;

        // Check if key is present at any mid
        if (ar[mid1] == key) {
            return mid1;
        }
        if (ar[mid2] == key) {
            return mid2;
        }

        // Since key is not present at mid,
        // check in which region it is present
        // then repeat the Search operation
        // in that region

        if (key < ar[mid1]) {

            // The key lies in between l and mid1
            r = mid1 - 1;
        }
        else if (key > ar[mid2]) {

            // The key lies in between mid2 and r
            l = mid2 + 1;
        }
        else {

            // The key lies in between mid1 and mid2
            l = mid1 + 1;
            r = mid2 - 1;
        }
    }

    // Key not found
    return -1;
}






  int binarySearch(int arr[], int l, int r, int x)
{
    while (l <= r) {
        int m = l + (r - l) / 2;

        // Check if x is present at mid
        if (arr[m] == x)
            return m;

        // If x greater, ignore left half
        if (arr[m] < x)
            l = m + 1;

        // If x is smaller, ignore right half
        else
            r = m - 1;
    }

    // if we reach here, then element was
    // not present
    return -1;
}



//"KADANE'S ALGORITHM"
//iski time complexity o(n) hn i.e the smallest possible 
long long maxsubarraysum(long long *array,long long n){
   long long best = 0, sum = 0;
for (long long  k = 0; k < n; k++) {
sum = max(array[k],sum+array[k]);
best = max(best,sum);
}
return  best ;

}
//to add two very large numbers 




string findSum(string str1, string str2) 
{ 
	if (str1.length() > str2.length()) 
		swap(str1, str2); 
	string str = ""; 
	int n1 = str1.length(), n2 = str2.length(); 
 
	reverse(str1.begin(), str1.end()); 
	reverse(str2.begin(), str2.end()); 
 
	int carry = 0; 
	for (int i=0; i<n1; i++) 
	{ 
		
		int sum = ((str1[i]-'0')+(str2[i]-'0')+carry); 
		str.push_back(sum%10 + '0'); 
 
		carry = sum/10; 
	}  
	for (int i=n1; i<n2; i++) 
	{ 
		int sum = ((str2[i]-'0')+carry); 
		str.push_back(sum%10 + '0'); 
		carry = sum/10; 
	} 
	if (carry) 
		str.push_back(carry+'0'); 
	reverse(str.begin(), str.end()); 
	return str; 
} 

string smin(string a,string b)
{
 
if(a.size()<b.size())return a;
if(a.size()>b.size())return b;
if(a<=b)return a;
else return b;
}





  bool so(const pair<ll,ll> &a, 
              const pair<ll,ll> &b) 
{ 
    return (a.second < b.second); 
}  


int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
ll n;
cin>>n;
ll a[n];
ll mx=LONG_LONG_MAX;
for(ll i=0;i<n;i++){
   cin>>a[i];
   if(a[i]>mx){
      mx=a[i];
   }
}


vector<pair<ll,ll>>v;
for(ll i=1;i<n;i++){
   v.emplace_back(make_pair(a[i],__gcd(mx,a[i])));
   
}
sort(v.begin(),v.end(),so);
cout<<mx<<" ";
for(ll i=v.size()-1;i>=0;i--){
   cout<<v[i].first<<" ";
}
cout<<endl;
}
return 0;   
}