from math import gcd
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    acc = 0
    res = []
    while len(arr) > 0:
        mx = arr[0]
        for e in arr[1:]:
            if gcd(e, acc) > gcd(mx, acc):
                mx = e
        acc = gcd(acc, mx)
        arr.remove(mx)
        res.append(mx)
    print(*res)
