def Sort(sub_li,y):
    sub_li.sort(key=lambda x: x[y])
    return sub_li
def gcd(a,b):
    if(b==0):
        return a
    return gcd(b, a%b)
a=int(input())
for j in range(a):
    b=int(input())
    c=[int(x) for x in input().split()]
    x=max(c)
    for j in range(b):
        d=[c[j]]
        c[j]=d
    for j in range(b):
        e=gcd(x,c[j][0])
        c[j].append(e)
    f=Sort(c,0)
    f=Sort(c,1)
    f=f[::-1]
    for j in range(b):
        print(f[j][0],end=" ")
    print()
