#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int fix[N], t, n, mx, a[N], c, cur, k, i, j;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    mx = 0;
    for (k = 1; k <= n; k++) {
      cin >> a[k];
      fix[k] = 0;
      mx = max(mx, a[k]);
    }
    for (k = 1; k <= n; k++) {
      if (mx == a[k]) fix[k] = 1;
    }
    cur = mx;
    cout << mx << " ";
    for (k = 2; k <= n; k++) {
      mx = 0;
      for (int i = 1; i <= sqrt(cur); i++) {
        if (cur % i != 0) continue;
        for (j = 1; j <= n; j++) {
          if (fix[j]) continue;
          if (a[j] % i == 0) {
            if (mx < i) {
              mx = i;
              c = j;
            }
          }
          if (a[j] % (cur / i) == 0) {
            if (mx < (cur / i)) {
              mx = (cur / i);
              c = j;
            }
          }
        }
      }
      fix[c] = 1;
      cout << a[c] << " ";
    }
    cout << endl;
  }
}
