#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int min_index = max_element(a.begin(), a.end()) - a.begin();
  vector<int> b(n);
  b[0] = a[min_index], a[min_index] = 0;
  int g = b[0];
  for (int i = 1; i < n; i++) {
    int in = 0, g_ans = 0;
    for (int j = 0; j < n; j++) {
      int g_tmp = gcd(g, a[j]);
      if (a[j] && g_tmp > g_ans) {
        g_ans = g_tmp;
        in = j;
      }
    }
    b[i] = a[in];
    a[in] = 0;
    g = g_ans;
  }
  for (int i : b) cout << i << " ";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
