from math import gcd

t = int(input())

for i in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    for i in range(len(a)):
        a[i] = [a[i], a[i]]
    for i in range(n):
        maxi = [0,0]
        for i in a:
            if maxi[0] < i[0]:
                maxi = i
        print(maxi[1],end= ' ')
        for i in range(len(a)):
            if a[i][0] != 0:
                a[i][0] = gcd(maxi[0],a[i][0])
        for i in range(len(a)):
            if a[i][0] == maxi[0]:
                a[i][0] = 0
                break
    print()