import math
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    ans, used = [], []
    for i in range(n):
        used.append(False)
    cur = 0
    for i in range(n):
        mx, idx = 0, -1
        for j in range(n):
            if(math.gcd(cur, a[j]) >= mx and used[j] == False):
                mx = math.gcd(cur, a[j])
                idx = j
        ans.append(a[idx])
        used[idx] = True
        cur = mx
    for x in ans:
        print(x, end=" ")
    print()
