output_description: Print the eventual sequence that will be produced, in a line.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      A_1 A_2 … A_N
attempt: def gcd(a, b):
      return gcd(b, a % b) if b else a
  
  
  def merge(a, us, vs):
      i, j, res = 0, 0, []
      while i < len(us) and j < len(vs):
          if a[us[i]] >= a[vs[j]]:
              res.append(us[i])
              i += 1
          else:
              res.append(vs[j])
              j += 1
      return res + us[i:] + vs[j:]
  
  
  def dfs(g, a, u, vis):
      vis[u] = True
      res = []
      for v in g[u]:
          if not vis[v]:
              res = merge(a, res, dfs(g, a, v, vis))
      return [u] + res
  
  
  while 1:
      try:
          n = int(input())
          a = sorted(map(int, input().split()))
      except:
          break
  
      g = [[j for j in range(n) if i != j and gcd(a[i], a[j]) != 1] for i in range(n)]
      # vis=[False]*n
      # res=[]
      # for u in range(n):
      #    if not vis[u]:
      #        res=merge(a,res,dfs(g,a,u,vis))
      # print(' '.join(str(a[u]) for u in res))
  
problem_description: Statement
  
  There are N integers written on a blackboard. The i-th integer is A_i.
  
  Takahashi and Aoki will arrange these integers in a row, as follows:
  
    * First, Takahashi will arrange the integers as he wishes.
    * Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.
  
  We will assume that Takahashi acts optimally so that the eventual sequence
  will be lexicographically as small as possible, and we will also assume that
  Aoki acts optimally so that the eventual sequence will be lexicographically as
  large as possible. Find the eventual sequence that will be produced.
samples: [{"input": "5\n    1 2 3 4 5", "output": "5 3 2 4 1\n    \n\nIf Takahashi arranges the given integers in the order (1,2,3,4,5), they will\nbecome (5,3,2,4,1) after Aoki optimally manipulates them.\n\n* * *"}, {"input": "4\n    2 3 4 6", "output": "2 4 6 3"}]