#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  int maxEle = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > maxEle) maxEle = a[i];
  }
  int gcdTillNow = maxEle;
  for (int i = 0; i < n; i++) {
    int currMaxGcd = -1, maxGcdPos = -1;
    for (int j = i; j < n; j++) {
      if (gcd(a[j], gcdTillNow) > currMaxGcd) {
        currMaxGcd = gcd(a[j], gcdTillNow);
        maxGcdPos = j;
      }
    }
    swap(a[i], a[maxGcdPos]);
    cout << a[i] << " ";
    gcdTillNow = currMaxGcd;
  }
  cout << "\n";
}
