#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  arr.clear();
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr.push_back(x);
  }
  vector<int> index(n, 0);
  sort(arr.rbegin(), arr.rend());
  int g = 0;
  for (int i = 0; i < n; i++) {
    int m = -1;
    for (int j = 0; j < n; j++) {
      if (!index[j] && (gcd(arr[j], g) >= gcd(arr[m], g) || m == -1)) {
        m = j;
      }
      g = gcd(arr[m], g);
      index[m] = 1;
      cout << arr[m] << " ";
    }
    cout << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    solve();
  }
  return 0;
}
