import math
for i in range(int(input())):
    n=int(input())
    ls=[int(a) for a in input().split()]
    ls1=[]
    ls1.append(max(ls))
    ls.remove(max(ls))
    an=0
    for j in range(n-1):
        if math.gcd(ls1[0],ls[j])>an:
            an=math.gcd(ls1[0],ls[j])
            an1=ls[j]
            #print(an1,an)
    if n>1:
        ls1.append(an1)
        ls.remove(an1)
        cgcd=an
        #print(cgcd)
    for j in range(n-2):
        an=0
        for k in range(len(ls)):
            if math.gcd(cgcd,ls[k])>an:
                #print(an,ls[k])
                an=math.gcd(cgcd,ls[k])
                an1=ls[k]
                #print(an)
        ls.remove(an1)
        ls1.append(an1)
        cgcd=an
    print(''.join(str(a)+' ' for a in ls1))