#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
  if (x < y) swap(x, y);
  if (x % y == 0) return y;
  return gcd(y, x % y);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int te;
  cin >> te;
  while (te > 0) {
    te--;
    int n;
    cin >> n;
    vector<int> a(n);
    multiset<int> s;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      s.insert(a[i]);
    }
    int m = *max_element(a.begin(), a.end());
    vector<int> ans;
    ans.push_back(m);
    int g = m;
    s.erase(s.find(m));
    while (g > 1) {
      int now = 0;
      int u = -1;
      for (auto v : s) {
        int temp = gcd(v, g);
        if (now < temp) {
          now = temp;
          u = v;
        }
      }
      if (now == 0) break;
      if (now == 1)
        g = 1;
      else {
        g = u;
        ans.push_back(u);
        s.erase(s.find(u));
      }
    }
    for (auto v : s) {
      ans.push_back(v);
    }
    for (int i = 0; i < n; i++) {
      if (i != n - 1)
        cout << ans[i] << ' ';
      else
        cout << ans[i] << endl;
    }
  }
}
