#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int inputs[1005];
int vis[1005];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int maxpos = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> inputs[i];
      vis[i] = 0;
      if (inputs[maxpos] < inputs[i]) maxpos = i;
    }
    vis[maxpos]++;
    cout << inputs[maxpos] << " ";
    int nowgcd = inputs[maxpos];
    for (int i = 1; i < n; i++) {
      int pos = -1;
      for (int j = 0; j < n; j++) {
        if (vis[j]) continue;
        if (pos == -1)
          pos = j;
        else {
          if (gcd(nowgcd, inputs[pos]) < gcd(nowgcd, inputs[j])) pos = j;
        }
      }
      vis[pos]++;
      cout << inputs[pos] << " ";
    }
    cout << endl;
  }
}
