# for _ in range(int(input())):
# = map(int, input().split())
# = int(input())
# = input()

def gcd(a,b):
    if(b==0):
        return a
    else:
        return gcd(b,a%b)


for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    b = []
    m = max(a)
    b.append(m)
    a.remove(m)
    bgcd = m
    maxgcd = 0
    maxelement = -1
    while len(a) > 0:
        for el in a:
            tempgcd = gcd(bgcd, el)
            if tempgcd > maxgcd:
                maxgcd = tempgcd
                maxelement = el
        b.append(maxelement)
        a.remove(maxelement)
        bgcd = maxgcd
        maxgcd = 0
        maxelement = -1

    for el in b:
        print(el, end = ' ')
    print()
