t = int(input())

def gcd2(a,b):
    if b > a:
        a,b = b,a
    r = a%b
    while r > 0:
        a,b = b,r
        r = a%b
    return b

for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    gcd = max(a)
    a.remove(gcd)
    b = [gcd]
    while len(b) < n:
        new_gcd = 0
        new_value = 0
        for c in a:
            g = gcd2(gcd, c)
            if g > new_gcd:
                new_gcd = g
                new_value = c
        if new_gcd == 1:
            b.extend(a)
        else:
            b.append(new_value)
            gcd = new_gcd
            a.remove(new_value)
    printable = ' '.join([str(i) for i in b])
    print(printable)