#include <bits/stdc++.h>
using namespace std;
int read() {
  int ret = 0, neg = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * neg;
}
int a[1111], ans[1111];
bool use[1111];
int gcd(int x, int y) { return (y == 0 ? x : gcd(y, x % y)); }
int main() {
  int t = read();
  while (t--) {
    int n = read();
    memset(use, false, sizeof(use));
    for (int i = 1; i <= n; i++) a[i] = read();
    int mx = 0, pos = 0;
    for (int i = 1; i <= n; i++)
      if (a[i] > mx) mx = a[i], pos = i;
    use[pos] = true;
    ans[1] = mx;
    for (int i = 2; i <= n; i++) {
      int mx = 0, pos = 0;
      for (int j = 1; j <= n; j++) {
        if (use[j]) continue;
        int gd = gcd(a[j], ans[i - 1]);
        if (gd > mx) mx = gd, pos = j;
      }
      use[pos] = 1;
      ans[i] = a[pos];
    }
    for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
    puts("");
  }
}
