import math
t = int(input())
for case in range(t):
    n = int(input())
    a = [int(s) for s in input().split(' ')]
    x = max(a)
    r = [x]
    a.remove(x)
    gd = x
    loop_bool = True
    while loop_bool:
        loop_bool = False
        max_elem = 1
        for item in a:
            if math.gcd(gd, item) > max_elem:
                max_elem = math.gcd(gd, item)
                x = item
                loop_bool = True
        if loop_bool:
            gd = max_elem
            r.append(x)
            a.remove(x)
    print(' '.join([str(s) for s in r + a]))

