#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e15 + 3;
int t;
int n;
int a[20020];
bool used[20020];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<int> ans;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) used[i] = false;
    ans.clear();
    int cur = 0;
    for (int i = 1; i <= n; ++i) {
      int maxinum = -1;
      int maxi = -1;
      for (int j = 1; j <= n; ++j) {
        if (used[j]) continue;
        int cand = gcd(cur, a[j]);
        if (cand > maxi) {
          maxi = cand;
          maxinum = j;
        }
      }
      used[maxinum] = true;
      ans.push_back(a[maxinum]);
      cur = maxi;
    }
    for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " ";
    cout << endl;
  }
  return 0;
}
