from math import gcd
def helper(arr,n):
    #print(arr)
    t1=[]
    t2=[]
    for i in arr:
        g=gcd(i,n)
        if g==1:
            t2.append(i)
        else:
            t1.append([g,i])
    return [t1,t2]
for _ in range(int(input())):

    n=int(input())
    arr=list(map(int,input().split()))
    if n<=1:
        print(*arr)
        continue
    t=max(arr)
    t1=[]
    t2=[]
    flag=True
    for i in arr:
        g=gcd(i,t)
        if i==t and True:
            flag=False
            continue
        elif g==1:
            t2.append(i)
        else:
            t1.append([g,i])
    t1.sort(reverse=True)
    ans=[t]
    for i in t1:
        ans.append(i[1])
    ans+=t2
    #print(t1,t2)
    print(*ans)
