from functools import lru_cache


@lru_cache(maxsize=None)
def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)


t = int(input())

for _ in range(t):
    n = int(input())

    a = [int(x) for x in input().split()]
    b = []

    b.append(max(a))

    a.remove(max(a))

    curr = b[0]
    for _ in range(n - 1):
        bestGcd = -1
        bestIndex = -1
        for i, ai in enumerate(a):
            if gcd(curr, ai) > bestGcd:
                bestGcd = gcd(curr, ai)
                bestIndex = i
        curr = bestGcd
        b.append(a[bestIndex])
        del a[bestIndex]

    print(*b)
