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()))
   mat = []
   for i in range(n):
      tmp = []
      for j in range(n):
         tmp.append((gcd(a[i], a[j]), a[j]))
      tmp = sorted(tmp, key=lambda x: x[0], reverse=True)
      mat.append(tmp)

   
   ans = max(mat)
   for ind in ans:
      print(ind[1], end=' ')
   print('')
