#include <bits/stdc++.h>
using namespace std;
bool b(pair<pair<long long, long long>, long long> p1,
       pair<pair<long long, long long>, long long> p2) {
  if (p1.first.first < p2.first.first) return true;
  if (p1.first.first == p2.first.first)
    if (p1.second < p2.second) return true;
  return false;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    pair<pair<long long, long long>, long long> c[n];
    long long ma = 0, ind;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] > ma) {
        ma = a[i];
        ind = i;
      }
    }
    for (long long i = 0; i < n; i++) {
      c[i].first.first = a[i];
      c[i].first.second = i;
      c[i].second = a[i];
    }
    swap(c[ind], c[0]);
    for (long long i = 1; i < n; i++) {
      ma = 0;
      for (long long j = i; j < n; j++) {
        c[j].first.first = gcd(c[i - 1].first.first, c[j].first.first);
        if (c[j].first.first > ma) {
          ma = c[j].first.first;
          ind = j;
        }
      }
      swap(c[ind], c[i]);
    }
    for (long long i = 0; i < n; i++) cout << c[i].second << " ";
    cout << endl;
  }
}
