import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

RS = raw_input
RI = lambda x=int: map(x,RS().split())
RN = lambda x=int: x(RS())
''' ...................................................................... '''
MX = 10**3+1

def gcd(a,b):
    while b:
        a,b = b,a%b
    return a

def factor(n,p = 0):
    fac1,fac2 = [],[]
    
    i = 1
    while i*i<=n:
        if n%i==0:
            j = n/i

            fac1.append(i)
            if i != j: fac2.append(j)
            
        i+=1

    fac = fac1+fac2[::-1]
    return fac[::-1]    # reverse it


for _ in xrange(RN()):
    n = RN()
    arr = RI()

    cnt = [0]*MX
    
    high = arr[0]
    for i in arr:
        cnt[i]+=1
        if i>high: high=i

    ans = [high]
    cnt[high]-=1

    last = high

    while len(ans)<n:
        
        found = False
        for f in factor(last):
            for num in xrange(f,MX,f):
                if cnt[num]>0:
                    tmp = [num]*cnt[num]
                    ans.extend(tmp)
                    cnt[num]=0
                    last = gcd(last,num)

                    found = True
                    break

            if found: break

    print ' '.join(map(str,ans))
            

    
