import sys
input = sys.stdin.buffer.readline

def divisor(n):
    ass = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            if i**2 == n:
                continue
            ass.append(n//i)
    return ass

T = int(input())
for testcase in range(T):
    n = int(input())
    a = list(map(int,input().split()))
    if n == 1:
        print(a[0])
        continue
    ma = max(a)

    d = divisor(ma)
    d.sort(reverse = True)

    used = [True]*n
    b = []

    r = d[0]

    flag = True

    for e in d:
        for i in range(n):
            if used[i] and a[i]%e == 0 and r%e == 0:
                used[i] = False
                b.append(a[i])
                r = e
    
    print(*b)


