X = 1
Y = 0

class Editor
  def initialize
    @text = []
    @buffer = ''
    @cursor = [0, 0]
  end

  def line_start?
    @cursor[Y] == 0
  end

  def line_end?
    @cursor[Y] == @text.size - 1
  end

  def str_start?
    @cursor[X] == 0
  end

  def str_end?
    y = @cursor[Y]
    @cursor[X] == @text[y].size
  end

  def character?
    (not str_end?)
  end

  def command_a
    @cursor[X] = 0
  end

  def command_e
    y = @cursor[Y]
    @cursor[X] = @text[y].size
  end

  def command_p
    @cursor[Y] -= 1 unless line_start?
    @cursor[X] = 0
  end

  def command_n
    @cursor[Y] += 1 unless line_end?
    @cursor[X] = 0
  end

  def command_f
    if not str_end?
      @cursor[X] += 1
    elsif not line_end?
      @cursor[Y] += 1
      @cursor[X] = 0
    end
  end

  def command_b
    if not str_start?
      @cursor[X] -= 1
    elsif not line_start?
      @cursor[Y] -= 1
      y = @cursor[Y]
      @cursor[X] = @text[y].size
    end
  end

  def command_d
    x = @cursor[X]
    y = @cursor[Y]

    if character?
      @text[y].slice!(x)
    elsif str_end? and not line_end?
      @text[y] << @text[y+1]
      @text.slice!(y+1)
    end
  end

  def command_k
    x = @cursor[X]
    y = @cursor[Y]

    if character?
      @buffer = @text[y].slice!(x..-1)
    elsif str_end? and not line_end?
      @buffer = '\n'
      @text[y] << @text[y+1]
      @text.slice!(y+1)
    end
  end

  def command_y
    unless @buffer.empty?
      x = @cursor[X]
      y = @cursor[Y]

      if @buffer == '\n'
        str = @text[y].slice!(x..-1)
        @text.insert(y+1, str)
        @cursor[X] = 0
        @cursor[Y] += 1
      else
        @text[y].insert(x, @buffer)
        @cursor[X] += @buffer.size
      end
    end
  end

  def write line
    @text << line
  end

  def print
    @text.each {|line| puts line}
  end
end

def main
  editor = Editor.new
  loop do
    line = gets.chomp
    break if line == 'END_OF_TEXT'
    editor.write(line)
  end

  loop do
    command = gets.chomp
    break if command == '-'
    eval("editor.command_#{command}")
  end

  editor.print
end

main