import math
t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    gc=a[0]
    for i in range(1,n):
        gc=math.gcd(gc,a[i])
    ans=[]
    ind=a.index(max(a))
    ans.append(a.pop(ind))
    pos=ans[0]
    y=pos
    while a:
        while y>=gc:
            tf=False
            for i in range(len(a)):
                if math.gcd(a[i],pos)==y:
                    pos=y
                    ans.append(a[i])
                    a.pop(i)
                    tf=True
                    break
            if tf==False:
                y-=1
            if len(a)==0:
                break
    print(*ans)