from sys import stdin, stdout


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


t = int(stdin.readline())
for case in range(t):
    n = int(stdin.readline())
    arr = list(map(int, stdin.readline().split()))
    avail = [0] * (10**3 + 1)
    for item in arr:
        avail[item] += 1
    res = [max(arr)]
    avail[res[0]] -= 1
    ggcd = res[0]
    for turn in range(n - 1):
        elem, cgcd = -1, 1
        for item in arr:
            if avail[item] > 0:
                if gcd(item, ggcd) >= cgcd:
                    cgcd = gcd(item, ggcd)
                    elem = item
        avail[elem] -= 1
        res.append(elem)
        ggcd = cgcd
    stdout.write(" ".join(map(str, res)))
    stdout.write("\n")