from math import gcd
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    maxi = max(l)
    ans = []
    while l:
        temp = max( (gcd(maxi,l[i]), i) for i in range(len(l)))
        ans.append( l.pop(temp[1]) )
        maxi = temp[0]
    print(' '.join(map(str,ans)))
        