# a = 1 8 2 3

# b = 8 2 1 3
# c = 8 2 1 1

# b = 8 3 2 1
# c = 8 1 1 1

# b = 3 2 8 1
# c = 3 1 1 1

# a = 2 4 8 16 17

# b = 16 8 4 2 17
# c = 16 8 4 2 1

# b = 17 2 4 8 16
# c = 17 1 1 1 1

# b1 = largest number
# b2 = find a number with gcd(b1,b2) highest -> keep gcd as d
# b3 = find a number with gcd(b1,b2,b3) highest ... d = gcd(d,b3)

def gcd(x,y):
    while y > 0:
        x, y = y, x%y
    return x

for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    b = []
    biggest_value = 0
    for i in a:
        if i > biggest_value:
            biggest_value = i
    a.remove(biggest_value)
    b.append(biggest_value)
    current_gcd = biggest_value

    for _ in range(n-1):
        biggest_gcd = 0
        number_for_biggest_gcd = 0
        for j in a:
            if gcd(current_gcd,j) > biggest_gcd:
                biggest_gcd = gcd(current_gcd,j)
                number_for_biggest_gcd = j
        a.remove(number_for_biggest_gcd) # takes n time
        b.append(number_for_biggest_gcd)
        current_gcd = biggest_gcd

    print(*b)
