def bigVova(arr):
    res = [None for _ in arr]
    visited = [False for _ in arr]
    d = 0
    for i in range(len(arr)):
        maxItem = 0
        index = 0
        for j in range(len(arr)):
            currGcd = gcd(d, arr[j])
            if visited[j] == False and currGcd > maxItem:
                maxItem = currGcd
                index = j
        d = maxItem
        res[i] = arr[index]
        visited[index] = True
    return res


def gcd(a, b):

    while b > 0:
        rem = a % b
        a = b
        b = rem
    return a


for i in range(int(input())):
    N = int(input())
    arr = [int(val) for val in input().split(" ")]
    print(" ".join(str(x) for x in bigVova(arr)))
