def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
for _ in range(int(input())):
    n = int(input())
    a = [int(i) for i in input().split()]
    used = [False for i in range(n)]
    a.sort(reverse=True)
    g = a[0]
    used[0] = True
    print(a[0], end=' ')
    while g != 1:
        mx = 1
        ind = -1
        for i in range(0,n):
            if used[i] == False:
                if gcd(g,a[i]) > mx:
                    mx = gcd(g, a[i])
                    ind = i
        if ind > -1:
            used[ind] = 1
            g = gcd(g, a[ind])
            print(a[ind], end=' ')
        else:
            break
    for i in range(n):
        if used[i] == False:
            print(a[i], end=' ')
    print()
