from math import gcd
t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int,input().split()))
    sol = []
    boro_nb = max(l)
    while len(l) != 0:
        Max = 0
        for i in l:
            temp = gcd(boro_nb,i)
            if temp > Max:
                Max = temp
                element = i
        boro_nb = Max
        sol.append(element)
        l.remove(element)
    print(*sol)