t = int(input())
i = 0
while i < t:

    n = int(input())
    a = list(map(int, input().split()))
    b = []
    krat = []
    MAX = max(a)
    z = MAX
    while z > 0:
        if MAX % z == 0:
            krat.append(z)
        z -=1
    
    indexK = 0
    while indexK < len(krat):
        K = krat[indexK]
        index = 0
        while index < n and krat[indexK] != -1:
            if a[index] % K == 0 and a[index] != 0:
                b.append(a[index])
                indK = indexK
                while indK < (len(krat)):
                    if a[index] % krat[indK] != 0:
                        del krat[indK]
                        krat.insert(indK, -1)
                    indK += 1
                        
                del a[index]
                a.insert(index, 0)
                
            index += 1
        
        
        indexK += 1
    B = ""
    for j in b:
        B += str(j) + " "
    print(B)
    
    i += 1
