t = int(input())
for i in range(t):
    n = int(input())
    flg = [0 for _ in range(n)]
    a = list(map(int,input().split()))
    b = [max(a)]
    check = []
    for s in range(1,b[0]+1):
        if b[0]%s == 0:
            check.append(s)
    check.reverse()
    flg[a.index(b[0])]=1
    count = 0
    flg_2 = 0
    while count <= len(check)-1:
        for t in range(len(b)):
            if b[t]%check[count]!=0:
                flg_2 = 1
        for j in range(n):
            if a[j]%check[count] == 0 and flg[j]==0 and flg_2==0:
                b.append(a[j])
                flg[j]=1
        count+=1
        flg_2=0
    for k in range(n):
        if flg[k]==0:
            b.append(a[k])
    for l in range(n):
        print(b[l],end=" ")
    print()