t = int(input())


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

    return gcd(b, a % b)


for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    first = max(a)
    answer = [first]
    done = set()
    max_index = 0
    for i in range(0, len(a)):
        if a[i] == first:
            max_index = i
            break
    done.add(max_index)
    i = 1
    gcd_value = first
    check = gcd_value
    while i < n:

        maximum = 0
        max_val = 0

        for k in range(0, len(a)):
            if k in done:
                continue
            if gcd(check, a[k]) > maximum:
                maximum = gcd(check, a[k])
                max_val = a[k]
                max_index = k

        check = gcd(check, max_val)

        answer.append(max_val)
        done.add(max_index)
        i += 1
    print(*answer)
