from math import gcd
def big_vova(arr):
    g=max(arr)
    ans=[]
    while arr:
        nex=max((gcd(g,arr[i]),i) for i in range(0,len(arr)))
        ans.append(arr.pop(nex[1]))
        g=nex[0]
    print(*ans)
    return ""

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