class Node
  attr_accessor :key, :left, :right

  def initialize(key)
    @key = key
  end

  def inorder
    left.inorder unless left.nil?
    print " ", key
    right.inorder unless right.nil?
  end

  def preorder
    print " ", key
    left.preorder unless left.nil?
    right.preorder unless right.nil?
  end
end

def insert
  z = Node.new $key.to_i
  x = $root
  while x do
    y = x
    if z.key < x.key
      x = x.left
    else 
      x = x.right
    end
  end
  
  if y.nil?
    $root = z
  elsif z.key < y.key
    y.left = z
  else
    y.right = z
  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