import math
gcd = math.gcd
t = int(input())
for _ in range(t):
    n = int(input())
    l1 = [int(x) for x in input().split()]
    old = max(l1)
    ans=[]
    for i in range(n):
        l2 = [gcd(int(x),old) for x in l1]
        new = l1[l2.index(max(l2))]
        ans.append(new)
        l1.remove(new)
        old = gcd(old,new)
        #print(ans,l1,old,new,primo,l2)
    print(*ans)