import math


def solve(arr,n):
    maxm = max(arr)
    arr.remove(maxm)
    arrm=[maxm]
    while len(arr):
        g=0
        number = 0
        
        for i in arr:
            if math.gcd(arrm[-1] , i)>g:
                g=math.gcd(arrm[-1] , i)
                number = i
            elif math.gcd(arrm[-1] , i)==g:
                number = max(i, number)
        if len(arr):
            arrm.append(number)
            arr.remove(number)
    print(*arrm)
    


for _ in range(int(input())):
    n=map(int,input().split())
    arr=list(map(int,input().split()))
    solve(arr,n)


