def hcf(a,b):
    if b==0:
        return 1
    if a%b==0:
        return b
    return hcf(b,a%b)
    

t=int(input())
for _ in range(t):
    n=int(input())
    list1=list(map(int,input().strip().split()))
    
    list2=list()
    x=max(list1)
    for val in list1:
        list2.append([hcf(val,x),val])
    list2.sort()
    
    for i in range(n):
        print(list2[n-1-i][1],end=" ")
    print()
    
    
       
