from sys import stdin
from math import gcd
input = stdin.readline

t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    used = [False]*n
    last = 0
    b = []
    for j in range(n):
        best = 0
        for i in range(n):
            if used[i]:
                continue
            g = gcd(last, a[i])
            if g > best:
                best = g
                best_idx = i
        used[best_idx] = True
        last = a[best_idx]
        b.append(a[best_idx])
    print(*b)