from math import gcd

for _ in range(int(input())):
    n = int(input())
    lst = [int(i) for i in input().split()]
    b = list()
    b.append(max(lst))
    lst.remove(max(lst))
    num = b[0]
    for j in range(n - 1):
        g = 0
        for k in range(n - j - 1):
            a = gcd(num, lst[k])
            if a > g:
                x, g = lst[k], a
        num = g
        lst.remove(x)
        b.append(x)
        if x == 1:
        	b += lst
        	break
    print(*b)