tc = int(input())
import math

def get_factors(n):
    facs = set()
    for i in range(1,int(math.sqrt(n))+1):
        if n % i == 0:
            facs.add(i)
            facs.add(n//i)
    return facs


for _ in range(tc):
    n = int(input())
    a = [int(x) for x in input().strip().split()]
    big = max(a)
    b = [big]
    ongoing = get_factors(big)
    a.remove(big)
    facs = []
    
    for e in a:
        facs.append(get_factors(e))
    
    while len(a) > 0:
        biggest = 0
        biggesti = -1
        for i in range(len(a)):
            current = max(ongoing & facs[i])
          
            if current > biggest:
               
                biggest = current
                biggesti = i
            
        b.append(a[biggesti])
        ongoing = facs[biggesti] & ongoing
    
        del a[biggesti]
        del facs[biggesti]
    print(b)























    
    
