#!/usr/bin/python3
" Codeforces Round #669 (Div. 2) "
"A. Ahahahahahahahaha"
#t =int(input())
#for i in range(t):
#    n =int(input())
#    a =list(map(int,input().split()))
#    q0=sum(a[::2])
#    q1=sum(a[1::2])
#    if q1==q0:
#        print(n)
#        for j in range(n):
#            print(a[j],end=" ")
#        print()
#        continue
#    if q1+q0<=int(n/2):
#       print(int(n/2)) 
#       for j in range(int(n/2)):
#           print(0,end=" ")
#       print()
#    elif int(n/2)%2==0:
#         print(int(n/2))
#         for j in range(int(n/2)):
#           print(1,end=" ")
#         print()
#    else:
#         print(int(n/2)+1)
#         for j in range(int(n/2)+1):
#           print(1,end=" ")
#         print()
"B. Big Vova"
def gcd(x,y):
    if y==0:
        return x
    return gcd(y,x%y)

t =int(input())
for i in range(t):
    n =int(input())
    a =list(map(int,input().split()))
    c=[]
    g=m=ij=f=te=0
    ind=[j for j in range(n)]
    for j in range(n):
        m=0
        for k in ind:
            maxi,mini=max(a[k],g),min(a[k],g)
            te=gcd(maxi,mini)
            if te>m:
                m=te
                ij=k
        g=m
        ind.remove(ij)
        c.append(a[ij])
        if g==1:
            break
    for j in ind:
        c.append(a[j])
    for j in c:
        print(j,end=" ")
    print()
