# Fast IO (be careful about bytestring, not on interactive)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

from math import gcd

for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    aWork = []
    b = []
    for elem in a:
        aWork.append([1,elem,elem])
    for i in range(n):
        curElem = max(aWork)
        b.append(curElem[2])
        curElem[0] = 0
        for i in range(n):
            if aWork[i][0] == 1:
                aWork[i][1] = gcd(aWork[i][1],curElem[1])
    print(" ".join(map(str,b)))
