# -*- encoding: utf-8 -*-
class Node
  attr_accessor :key, :left, :right
  def initialize key
    @key = key
  end

  def inorder
    left.inorder if left
    print " ", key
    right.inorder if right
  end

  def preorder
    print " ", key
    left.preorder if left
    right.preorder if right
  end
end

def insert
  z = Node.new $key.to_i
  x = $root
  while x
    y = x  #???????¨????
    x = z.key < x.key ? x.left : x.right  #?????????????§????/?????????????§????
  end

  unless y  #y???nil?????´???
    $root  = z
  else
    z.key < y.key ? y.left = z : y.right = z  #z???y?????????????????????/z???y?????????????????????
  end
end

n = gets.to_i
n.times do
  command, $key = gets.split
  if command == "insert"
    insert
  else
    $root.inorder
    puts 
    $root.preorder
    puts
  end
end