from collections import defaultdict
from math import gcd
t = int(input())
for test in range(t):
    n = int(input())
    a = list(map(int,input().split()))

    maxi = max(a)
    ans = []
    cur = 0
    while a:
        mx = 0 
        idx = -1
        for i in range(len(a)):
            x = gcd(a[i],cur)
            if x > mx:
                mx = x
                idx = i
        cur = mx
        ans.append(a[idx])
        a.pop(idx)
    print(*ans)