import sys
input = sys.stdin.readline
from math import gcd

t = int(input())
for i in range(t):
    n = int(input())
    A = list(map(int, input().split()))

    ANS = [max(A)]
    A.pop(A.index(max(A)))
    g = ANS[0]
    
    for j in range(n-1):
        mg = 0
        mind = -1
        for k in range(len(A)):
            ng = gcd(g, A[k])
            if ng > mg:
                mg = ng
                mind = k
        ANS.append(A[mind])
        A.pop(mind)
        g = mg
    print(*ANS)
