#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (!a || !b) return a + b;
  if (a > b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int c;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int& i : a) {
      cin >> i;
    }
    int ma = 0;
    for (int i = 1; i < n; i++) {
      if (a[ma] < a[i]) ma = i;
    }
    int g = a[ma];
    swap(a[0], a[ma]);
    for (int i = 1; i < n; i++) {
      int idx = i;
      for (int j = i; j < n; j++) {
        if (gcd(g, a[idx]) < gcd(g, a[j])) idx = j;
      }
      swap(a[i], a[idx]);
      g = gcd(g, a[i]);
    }
    for (int& i : a) {
      cout << i << ' ';
    }
    cout << '\n';
  }
}
