class Cursor
  attr_accessor :line, :index
  def initialize
    @line = 0
    @index = 0
  end
  def move(n)
    @index = n
  end
  def up
    @line -= 1
  end
  def down
    @line += 1
  end
end

text = Array.new
while true do
  text.push(gets.chomp)
  break if text.last == "END_OF_TEXT"
end
text.pop

cursor = Cursor.new
buffer = ""
while true do
  case gets.chomp
  when "-"
    break
  when "a"
    cursor.move(0)
  when "e"
    cursor.move(text[cursor.line].size)
  when "p"
    cursor.up if cursor.line > 0
    cursor.move(0)
  when "n"
    cursor.down if cursor.line < text.size - 1
    cursor.move(0)
  when "f"
    if cursor.index < text[cursor.line].size
      cursor.move(cursor.index + 1)
    elsif cursor.line < text.size - 1
      cursor.down
      cursor.move(0)
    end
  when "b"
    if cursor.index > 0
      cursor.move(cursor.index - 1)
    elsif cursor.line > 0
      cursor.up
      cursor.move(text[cursor.line].size - 1)
    end
  when "d"
    if cursor.index != text[cursor.line].size
      text[cursor.line].slice!(cursor.index)
    elsif cursor.line < text.size - 1
      text[cursor.line] += text[cursor.line + 1]
      text.delete_at(cursor.line + 1)
    end
  when "k"
    if cursor.index != text[cursor.line].size
      buffer = text[cursor.line].slice!(cursor.index...text[cursor.line].size)
    elsif cursor.line < text.size - 1
      text[cursor.line] += text[cursor.line + 1]
      text.delete_at(cursor.line + 1)
      buffer = "\n"
    end
  when "y"
    case buffer
    when ""
    when "\n"
      text.insert(cursor.line + 1, text[cursor.line].slice!(cursor.index...text[cursor.line].size))
      cursor.down
      cursor.move(0)
    else
      text[cursor.line].insert(cursor.index, buffer)
      cursor.move(cursor.index + buffer.size)
    end
  end
end

puts text