import math
for _ in range(int(input())):
    n=int(input())
    l=list(map(int, input().split()))
    l.sort()
    pre = l[0]
    for i in range(0, n-2):
      pos = i +1
      cc = 1
      for j in range(i+1, n):
        x=math.gcd(pre, l[j])
        if x > cc:
          cc = x
          pos = j
      pre = x
      l[i +1], l[pos] = l[pos], l[i+1]
    for i in l:
      print(i, end= ' ')
    print()
        
            
        
