def gcd(a, b):
    while a != 0 and b != 0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b
n = int(input())

for i in range(n):
    n1 = int(input())
    a = list(map(int, input().split()))
    b = [0 for i in a]
    d = [False for i in a]
    h = 0
    for i in range(len(a)):
        g = 0
        index = 0
        for j in range(len(a)):
            gcd1 = gcd(h, a[j])
            if gcd1 > g and d[j] == False:
                g = gcd1
                index = j
        h = g
        b[i] = a[index]
        d[index] = True
    print(*b)

