import math
for t in range(int(input())):
    n=int(input())
    num=list(map(int,input().split()))
    k={}
    pre=0
    for i in num:
        temp={}
        val=i
        for j in num:
            v=math.gcd(i,j)
            temp[j]=v
            val=max(v,val)
        if val>pre:
            pre=val
            k=temp
    out=sorted(k.keys(),key=lambda x: k[x],reverse=True)
    print(*out)