#Codeforces Round #669
#Problem B
import sys

#
#BEGIN TEMPLATE
#
def input(): return sys.stdin.readline()[:-1]
def getInt(): return int(input())
def getIntIter(): return map(int, input().split())
def getIntList(): return list(getIntIter())
def flush(): sys.stdout.flush()
#
#END TEMPLATE
#

def gcd(a,b):
    if b > a:
        a,b = b,a
    while b != 0:
        a,b = b,a%b
    return a

for _ in range(getInt()):
    n = getInt()
    nums = getIntList()
    nums.sort()
    out = [nums[-1]]
    currGcd = nums[-1]
    del nums[-1]
    while len(nums) > 0:
        gcdMap = list(map(lambda x: gcd(currGcd,x), nums))
        if max(gcdMap) == 1:
            out.extend(nums)
            break
        i = gcdMap.index(max(gcdMap))
        out.append(nums[i])
        currGcd = gcd(currGcd, nums[i])
        del nums[i]
    print(" ".join(map(str,out)))
