from math import gcd
def isprime1(n):
    i=2
    while i**2<=n:
        if n%i==0:
            return False
        else:
            i+=1
    return True
def big_vova(arr):
    maxi=max(arr)
    temp=[]
    arr=sorted(arr,reverse=True)
    while arr:
        t=max(arr,key=lambda s:gcd(maxi,s))
        temp.append(t)
        maxi=t
        arr.remove(t)
    print(*temp)
    return ""



for i in range(int(input())):
    a=input()
    lst=list(map(int,input().strip().split()))
    print(big_vova(lst))