t = int(input())
import heapq, math
for _ in range(t):
    n = int(input())
    A = list(map(int, input().split()))

    if n == 1:
        print(A[0])
        continue

    H = []
    for i in range(n):
        h = []
        heapq.heapify(h)
        H.append(h)
    #print(H)
    for i in range(n-1):
        for j in range(i+1, n):
            g = math.gcd(A[i], A[j])
            heapq.heappush(H[i], (-g, j))
            heapq.heappush(H[j], (-g, i))

    B = []
    for i, a in enumerate(A):
        B.append((a, i))
    B.sort(reverse=True)
    ans = []
    ans.append(B[0][0])
    i = B[0][1]
    used = set()
    used.add(i)
    for j in range(n-1):
        while H[i]:
            _, k = heapq.heappop(H[i])
            if k not in used:
                break
        used.add(k)
        i = k
        ans.append(A[i])
    print(*ans)
