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

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    flag = [False]*n
    ans = [0]
    now = 0
    
    for i in range(n):
        M = -1
        mark = -1
        
        for j in range(n):
            if flag[j]:
                continue
            
            g = gcd(now, a[j])
            
            if g>M:
                M = g
                mark = j
        
        ans.append(a[mark])
        flag[mark] = True
        now = gcd(now, a[mark])

    print(*ans[1:])