#include <bits/stdc++.h>
using namespace std;
int t, n, a[1010], gcd[1010][1010];
vector<pair<int, int>> vt;
int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
int main() {
  for (int i = 1; i <= 1000; i++) {
    for (int j = 1; j <= 1000; j++) {
      gcd[i][j] = GCD(i, j);
    }
  }
  scanf("%d", &t);
  while (t--) {
    vt.clear();
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", a + i);
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
      vt.push_back({gcd[a[n - 1]][a[i]], a[i]});
    }
    sort(vt.begin(), vt.end());
    for (int i = n - 1; i >= 0; i--) {
      printf("%d ", vt[i].second);
    }
    puts("");
  }
}
