from math import gcd
T = int(input())
for t in range(T):
    n = int(input())
    A = [int(i) for i in input().split()]
    cur_grade = dict()
    visited = [False]*n
    max_A = max(A)
    B = []
    for i in range(n):
        if A[i] == max_A:
            visited[i] = True
            B.append(A[i])
    prev_best_grad = B[-1]
    while len(B) < n:
        best_grade = 0
        for i in range(n):
            if not visited[i]:
                cur_grade[i] = gcd(prev_best_grad, A[i])
                best_grade = max(best_grade,cur_grade[i])
        for i in range(n):
            if  not visited[i] and cur_grade[i] == best_grade:
                visited[i] = True
                B.append(A[i])
        prev_best_grad = best_grade
    print(*B)



