local mfl, mce, mmi = math.floor, math.ceil, math.min
local mma = math.max

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local i = self.box[#self.box]
  table.remove(self.box)
  self.v[i], self.p[i] = val, parent
  self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  return i
end
AvlTree.create = function(self, lessthan, n)
  self.lessthan = lessthan
  self.root = 1
  self.box = {}
  for i = n + 1, 2, -1 do table.insert(self.box, i) end
  -- value, leftCount, rightCount, left, right, parent
  self.v, self.lc, self.rc, self.l, self.r, self.p = {}, {}, {}, {}, {}, {}
  for i = 1, n + 1 do
    self.v[i], self.p[i] = 0, 1
    self.lc[i], self.rc[i], self.l[i], self.r[i] = 0, 0, 1, 1
  end
end

AvlTree.recalcCount = function(self, i)
  if 1 < i then
    local kl, kr = self.l[i], self.r[i]
    if 1 < kl then self.lc[i] = 1 + mma(self.lc[kl], self.rc[kl])
    else self.lc[i] = 0
    end
    if 1 < kr then self.rc[i] = 1 + mma(self.lc[kr], self.rc[kr])
    else self.rc[i] = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, i)
  while 1 < i do
    self:recalcCount(i)
    i = self.p[i]
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = self.p[parent]
  self.r[child], self.l[parent] = parent, self.r[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.l[parent]] = parent
  if 1 < granp then
    if self.l[granp] == parent then
      self.l[granp] = child
    else
      self.r[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = self.p[parent]
  self.l[child], self.r[parent] = parent, self.l[child]
  self.p[child], self.p[parent] = granp, child
  self.p[self.r[parent]] = parent
  if 1 < granp then
    if self.r[granp] == parent then
      self.r[granp] = child
    else
      self.l[granp] = child
    end
  else
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if self.root <= 1 then self.root = self:makenode(val, 1) return end
  local pos = self.root
  while true do
    if self.lessthan(val, self.v[pos]) then
      if 1 < self.l[pos] then
        pos = self.l[pos]
      else
        self.l[pos] = self:makenode(val, pos)
        pos = self.l[pos]
        break
      end
    else
      if 1 < self.r[pos] then
        pos = self.r[pos]
      else
        self.r[pos] = self:makenode(val, pos)
        pos = self.r[pos]
        break
      end
    end
  end
  while 1 < pos do
    local child, parent = pos, self.p[pos]
    if parent <= 1 then
      break
    end
    self:recalcCount(parent)
    local lcp_m_rcp = self.lc[parent] - self.rc[parent]
    if lcp_m_rcp % 2 ~= 0 then -- 1 or -1
      pos = parent
    elseif lcp_m_rcp == 2 then
      if self.lc[child] - 1 == self.rc[child] then
        self:rotR(child, parent)
      else
        local cr = self.r[child]
        self:rotL(cr, child)
        self:rotR(cr, parent)
      end
      pos = 1
    elseif lcp_m_rcp == -2 then
      if self.rc[child] - 1 == self.lc[child] then
        self:rotL(child, parent)
      else
        local cl = self.l[child]
        self:rotR(cl, child)
        self:rotL(cl, parent)
      end
      pos = 1
    else
      pos = 1
    end
  end
end

AvlTree.rmsub = function(self, node)
  while 1 < node do
    self:recalcCount(node)
    if self.lc[node] == self.rc[node] then
      node = self.p[node]
    elseif self.lc[node] + 1 == self.rc[node] then
      self:recalcCountAll(self.p[node])
      node = 1
    else
      if self.lc[self.r[node]] == self.rc[self.r[node]] then
        self:rotL(self.r[node], node)
        node = 1
      elseif self.lc[self.r[node]] + 1 == self.rc[self.r[node]] then
        local nr = self.r[node]
        self:rotL(nr, node)
        node = nr
      else
        local nrl = self.l[self.r[node]]
        self:rotR(nrl, self.r[node])
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local node = self.root
  while 1 < self.l[node] do
    node = self.l[node]
  end
  local v = self.v[node]
  local kp = self.p[node]
  self.p[self.r[node]] = kp
  if 1 < kp then
    self.l[kp] = self.r[node]
    self:rmsub(kp)
  else
    self.root = self.r[node]
  end
  table.insert(self.box, node)
  return v
end

AvlTree.new = function(lessthan, n)
  local obj = {}
  setmetatable(obj, {__index = AvlTree})
  obj:create(lessthan, n)
  return obj
end

local n = io.read("*n")
local hn = mfl(n / 2)
local stodd = SegTree.new(hn, mmi, 1000000007)
local steven = SegTree.new(hn, mmi, 1000000007)
local mapodd, mapeven = {}, {}
for i = 1, hn do
  local a = io.read("*n")
  mapodd[a] = i
  stodd:setValue(i, a, true)
  a = io.read("*n")
  mapeven[a] = i
  steven:setValue(i, a, true)
end
stodd:updateAll()
steven:updateAll()

local t_avl = {}
local avl = AvlTree.new(function(x, y) return t_avl[x].v1 < t_avl[y].v1 end, hn)

local function makenode(isodd, left, right)
  local tmp = {}
  tmp.isodd = isodd
  tmp.left = left
  tmp.right = right
  if isodd then
    local v1 = stodd:getRange(left, right)
    local v1pos = mapodd[v1]
    local v2 = steven:getRange(v1pos, right)
    local v2pos = mapeven[v2]
    tmp.v1 = v1
    tmp.v2 = v2
    tmp.v1pos = v1pos
    tmp.v2pos = v2pos
  else
    local v1 = steven:getRange(left, right)
    local v1pos = mapeven[v1]
    local v2 = stodd:getRange(v1pos + 1, right + 1)
    local v2pos = mapodd[v2]
    tmp.v1 = v1
    tmp.v2 = v2
    tmp.v1pos = v1pos
    tmp.v2pos = v2pos - 1
  end
  table.insert(t_avl, tmp)
  avl:add(#t_avl)
end
local ret = {}
makenode(true, 1, hn)
for i = 1, hn do
  local idx = avl:pop()
  local z = t_avl[idx]
  local isodd, left, right = z.isodd, z.left, z.right
  local v1, v2, v1pos, v2pos = z.v1, z.v2, z.v1pos, z.v2pos
  table.insert(ret, v1)
  table.insert(ret, v2)
  if isodd then
    stodd:setValue(v1pos, 1000000007)
    steven:setValue(v2pos, 1000000007)
    if left < v1pos then
      makenode(true, left, v1pos - 1)
    end
    if v1pos < v2pos then
      makenode(false, v1pos, v2pos - 1)
    end
    if v2pos < right then
      makenode(true, v2pos + 1, right)
    end
  else
    steven:setValue(v1pos, 1000000007)
    stodd:setValue(v2pos + 1, 1000000007)
    if left < v1pos then
      makenode(false, left, v1pos - 1)
    end
    if v1pos < v2pos then
      makenode(true, v1pos + 1, v2pos)
    end
    if v2pos < right then
      makenode(false, v2pos + 1, right)
    end
  end
end
print(table.concat(ret, " "))


Measured memory_bytes: 69132