def GCD(n1, n2):
    if n1 < n2:
        n1, n2 = n2, n1
    while n2:
        n1, n2 = n2, n1%n2
    return n1

def highestGCD(li,taken, num):
    hgcd = 0
    pos = -1
    for i in range(len(li)):
        if not taken[i]:
            cgcd = GCD(num,li[i])
            if cgcd > hgcd:
                hgcd = cgcd
                pos = i
    if pos != -1:
        taken[pos] = True
        print(li[pos],end=" ")
        return hgcd
    else:
        return 1

for t in range(int(input())):
    n = int(input())
    li = list(map(int,input().split()))
    taken = [False]*n
    m = 0
    for i in range(1,n):
        if li[m] < li[i]:
            m = i
    print(li[m],end=" ")
    taken[m] = True
    hgcd = li[m]
    counter = 0
    while hgcd != 1 and counter < n:
        hgcd = highestGCD(li,taken,hgcd)
        counter += 1
    
    if counter != n:
        for i in range(n):
            if not taken[i]:
                print(li[i], end=" ")
    print()