#include <bits/stdc++.h>
using namespace std;
int a[1000];
int b[1000 / 2];
int d[1000];
int findmaxyue(int a, int b) {
  int c = a;
  if (a < b) {
    a = b;
    b = c;
  }
  while (1) {
    c = a % b;
    if (c == 0)
      return b;
    else {
      a = b;
      b = c;
    }
  }
}
void swap(int f[], int i, int j) {
  int ans = f[i];
  f[i] = f[j];
  f[j] = ans;
}
int main() {
  int n, r, max, ans, j = 0;
  cin >> r;
  while (r--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    max = 1;
    for (int i = 1; i <= n; i++) {
      if (a[i] > a[max]) {
        max = i;
      }
    }
    swap(a, 1, max);
    j = 1;
    d[1] = a[1];
    while (j <= n) {
      for (int i = j + 1; i <= n; i++) {
        d[i] = findmaxyue(d[j], a[i]);
      }
      max = j + 1;
      for (int i = j + 1; i <= n; i++) {
        if (d[i] > d[max]) max = i;
      }
      swap(a, j + 1, max);
      j++;
    }
    for (int i = 1; i <= n; i++) {
      cout << a[i] << " ";
    }
    cout << endl;
  }
}
