
T = int(input())

#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}

def prime(n):
  for i in range(2,2+int(n**0.5)):
    if n%i == 0:
      return 0 
  return 1

ps = set()

for i in range(2,500):
  if prime(i):
    ps.add(i)

def gcd(a,b):
  while b:
    a, b = b, a%b
  return a

def pfs(n):
  fs = set()
  for i in ps:
    if i>n:
      break
    if n%i == 0:
      fs.add(i)
      if n//i in ps:
        fs.add(i)
  return fs


for t in range(T):
  n = int(input())
  #n,k = map(int,input().split())
  a = list(map(int,input().split()))
  #a = input()
  d = False
  a.sort()
  mg = pfs(max(a))
  g = [0]*(n)
  g[-1] = a[-1]
  for i in range(n-1):
    g[i] = gcd(a[-1],a[i])
  k = sorted(range(n), key = lambda i: g[i])
  for i in range(n-1,-1,-1):
    print(a[k[i]],end=' ')
  print()
    