#include <bits/stdc++.h>
using namespace std;
int mmm(long long a, long long x, long long n) {
  if (a - n >= x)
    return a - n;
  else
    return x;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void solve1() {
  int n;
  cin >> n;
  int s = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> b(n, 0);
  b[0] = a[a.size() - 1];
  vector<bool> used(n, 0);
  used[n - 1] = 1;
  int g = b[0];
  int ind = 0;
  for (int i = 1; i < n; i++) {
    int G = 1;
    int maxk = 1;
    for (int j = 0; j < n; j++) {
      if (!used[j]) {
        int k = gcd(g, a[j]);
        if (k >= G) {
          G = k;
          maxk = k;
          ind = j;
        }
      }
    }
    g = maxk;
    b[i] = a[ind];
    used[ind] = true;
  }
  for (int i = 0; i < n; i++) cout << b[i] << ' ';
  cout << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) solve1();
  return 0;
}
