#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;
  int a[n];
  int us[n];
  for (int i = 0; i < n; i++) us[i] = false;
  for (int i = 0; i < n; i++) cin >> a[i];
  int mx = 0;
  for (int i = 1; i < n; i++)
    if (a[i] > a[mx]) mx = i;
  us[mx] = true;
  cout << a[mx] << ' ';
  int now = a[mx];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++)
      if (!us[j]) mx = j;
    for (int j = 0; j < n; j++)
      if (!us[j] && gcd(now, a[mx]) < gcd(now, a[j])) mx = j;
    cout << a[mx] << ' ';
    now = gcd(now, a[mx]);
    us[mx] = true;
  }
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  int q;
  cin >> q;
  while (q--) solve();
}
