def gcd(x, y): 
   while(y): 
       x, y = y, x % y   
   return x
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    m=x=0
    while(l):
        m,x=max((gcd(m,i),i)for i in l)
        print(x)
        l.remove(x)