#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;

const int N = 1e4 + 5;

int n, m, _, k, x, y;

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        VI a(n), b(n), v(n, 0);
        for (auto &i : a) cin >> i;
        sort(all(a), greater<int>());
        int cur = b[0] = a[0]; v[0] = 1;
        rep (i, 1, n - 1) {
            int res = 0, w;
            rep (j, 1, n - 1)
                if (!v[j]) {
                    int c = __gcd(cur, a[j]);
                    if (res <= c) res = c, w = j;
                }
            cur = a[w], b[i] = w, v[w] = 1;
        }
        for (auto &i : b) cout << i << ' ';
        cout << endl;
    }
    return 0;
}