#include <bits/stdc++.h>
using namespace std;
int arr[1010];
int brr[1010];
bool vis[1010];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) {
      vis[i] = false;
      brr[i] = 0;
    }
    int maxx = -1, op = -1;
    for (int i = 0; i < n; i++) {
      scanf("%d", &arr[i]);
      if (arr[i] > maxx) {
        maxx = arr[i];
        op = i;
      }
    }
    brr[0] = maxx;
    vis[op] = true;
    int cur = 1;
    int curgcd = brr[0];
    while (cur < n) {
      maxx = -1, op = -1;
      for (int i = 0; i < n; i++) {
        if (!vis[i]) {
          int gcdnum = gcd(curgcd, arr[i]);
          if (gcdnum > maxx) {
            maxx = gcdnum;
            op = i;
          }
        }
      }
      brr[cur++] = arr[op];
      vis[op] = true;
      curgcd = maxx;
    }
    for (int i = 0; i < cur; i++) {
      printf("%d", brr[i]);
      if (i != cur - 1)
        printf(" ");
      else
        printf("\n");
    }
  }
  return 0;
}
