
from math import gcd

for _ in range(int(input())):
    
    n = int(input())
    A = list(map(int, input().split()))
    note = [[0] * 1001 for i in range(n)]
    for i in range(n):
        for j in range(1001):
            note[i][j] = gcd(j, A[i])
    x = 0
    ans = [0] * n
    for i in range(n):
        m = 0
        k = 0
        for j in range(n):
            if A[j] and note[j][x] > m:
                m = note[j][x]
                k = j
        ans[i] = A[k]
        A[k] = 0
        x = m
    print(*ans)
    