def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))

    ans = [max(arr)]
    d = {max(arr): 1}
    G = ans[0]
    for _ in range(n - 1):
        t = {}
        maxim, now = 0, 0
        for i in arr:
            if i in d and (i not in t or t[i] < d[i]):
                if i not in t:
                    t[i] = 0
                t[i] += 1
                continue
            g = gcd(G, i)
            if g > maxim:
                maxim = g
                now = i
        ans.append(now)
        if now not in d:
            d[now] = 0
        d[now] += 1
        G = gcd(G, now)

    print(*ans)
