def remove(e, ind, arr):
   f = arr[:ind+1]
   s = arr[ind+1:]
   s.remove(e)
   return f+s

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

for _ in range(int(input())):
   n = int(input())
   a = list(map(int, input().split()))
   elem = max(a)
   a.remove(elem)
   a.insert(0,elem)
   ans = [elem]
   i = 0
   while i<n-1: 
      mx = 1
      tp = a[i+1]
      for k in range(i+1,n):
         hcf = gcd(a[i], a[k])
         if hcf>mx:
            mx = hcf
            tp = a[k]
      a = remove(tp, i, a) 
      
      ans.append(tp)
      a.insert(i+1,mx)
      
      i+=1
   print(*ans)
