#include<bits/stdc++.h>
// #define top 503
#define top 100000
#define as "\n"
#define S second
#define F first
#define ep emplace_back
#define pb push_back
using ll = long long;
#define fow(a,b) for(int i=a; i<b; ++i)
using namespace std;

int main()
{ 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout<<__gcd(930, 589)<<as;
    cout<<__gcd(930, 474)<<as;

    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n]{};
        for(int i=0; i<n; ++i)
            cin>>arr[i];
        int brr[n]{};
        sort(arr, arr+n, greater<int>());
        int check[n]{}; check[0] =1;
        brr[0] =arr[0];
        int huu = brr[0];
        for(int i=1; i<n; ++i)
        {
            int ind =0, hi =0;
            for(int j=0; j<n; ++j)
            {
                if(check[j]) continue;
                int x = __gcd(huu,arr[j]);
                if(x > hi) hi = x, ind =j;
            }
            check[ind]=1; brr[i] =arr[ind]; huu = hi;
        }
        for(int i=0; i<n; ++i)
            cout<<brr[i]<<" ";
        cout<<as;
    }
  
}
    // ios_base::sync_with_stdio(false); 
    // cin.tie(NULL);