import math

for _ in range(int(input())):
    n = int(input())
    data = list(map(int, input().split()))
    gcd_value = 0
    for i in range(n):
        max_value = (0, 0) # (gcd_value, index)
        for j in range(n):
            if data[j] != -1:
                max_value = max(max_value, (math.gcd(gcd_value, data[j]), j))
        gcd_value = max_value[0]
        print(data[max_value[1]], end=' ')
        data[max_value[1]] = -1
    print()