import math
def solution():
    n = int(input())
    l = list(map(int,input().split()))
    g = max(l)
    pg = l.index(g)
    gcd = 1
    l = l[:pg]+l[pg+1:]
    print(g,end = " ")
    while(len(l) > 0):
        for i in range(len(l)):
            cgcd = math.gcd(g,l[i])
            if cgcd > gcd:
                gcd = cgcd
                pg = i
        if gcd == 1:
            break
        print(l[pg],end = " ")
        g = gcd
        gcd = 1
        l = l[:pg]+l[pg+1:]
        
    for i in range(len(l)):
        print(l[i],end=" ")

    print("")        




t = int(input())
while t>0:
    solution()
    t-=1