def nod(a, b):
    if a == 1 or b == 1:
        return 1

    while a != b:
        if a > b:
            a = a - b
        else:
            b = b - a
    return a

n = int(input())

i = 0
while i < n:
    q = int(input())
    mas = [int(j) for j in input().split()]

    new_mas = []

    new_mas.append(max(mas))
    mas.remove(max(mas))

    j = 0
    max_nod = new_mas[0]
    while len(mas) > 0:
        nd = 1
        num = 0
        for j in range(len(mas)):
            nd1 = nod(max_nod, mas[j])
            if nd1 > nd:
                nd = nd1
                num = j
        if nd != 1:
            new_mas.append(mas[num])
            mas.pop(num)
            max_nod = nd
        else:
            for j in mas:
                new_mas.append(j)
            mas.clear()

    for j in new_mas:
        print(j, end=' ')
    print()
    i += 1