#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#define INPUT_LINE_MAX 10
#define INPUT_CHAR_MAX 20
#define COMMAND_MAX 300
#define RUN_LINE_MAX 100
#define RUN_CHAR_MAX 1000

using namespace std;

int main() {
  string input, buffer, result;
  vector<string> editor;
  char command = 0;
  string::size_type cursor = 0;
  vector<string>::size_type line = 0;

  for (int in = 0; in < INPUT_LINE_MAX; in++) {
    getline(cin, input);
    if (input.size() > INPUT_CHAR_MAX)
      return -1;
    if (input == "END_OF_TEXT")
      break;

    editor.push_back(input);
  }

  cursor = 0;
  line = 0;

  for (int com = 0; com < COMMAND_MAX; com++) {
    cin >> command;
    if (command == '-')
      break;

    switch (command) {
    case 'a':
      cursor = 0;
      break;

    case 'e':
      cursor = editor[line].size();
      break;

    case 'p':
      cursor = 0;
      if (line > 0)
        line--;
      break;
    case 'n':
      cursor = 0;
      if (line < editor.size() - 1)
        line++;
      break;
    case 'f':
      if (cursor < editor[line].size())
        cursor++;
      else if (cursor == editor[line].size() && line < editor.size() - 1) {
        cursor = 0;
        line++;
      }
      break;
    case 'b':
      if (cursor > 0)
        cursor--;
      else if (cursor == 0 && line > 0) {
        line--;
        cursor = editor[line].size();
      }
      break;
    case 'd':
      if (cursor < editor[line].size())
        editor[line].erase(editor[line].begin() + cursor);
      else if (cursor == editor[line].size() && line < editor.size() - 1) {
        editor[line] += editor[line + 1];
        editor.erase(editor.begin() + line + 1);
      }
      break;

    case 'k':
      if (cursor < editor[line].size()) {
        buffer = "";
        copy(editor[line].begin() + cursor, editor[line].end(), back_inserter(buffer));
        editor[line].erase(editor[line].begin() + cursor, editor[line].end());
        cursor = editor[line].size();
      }
      else if (cursor == editor[line].size() && line < editor.size() - 1) {
        editor[line] += editor[line + 1];
        buffer = "\n";
        editor.erase(editor.begin() + line + 1);
      }
      break;

    case 'y':
      if (!buffer.empty()) {
        if (buffer == "\n") {
          string tmp;
          copy(editor[line].begin() + cursor, editor[line].end(), back_inserter(tmp));
          editor[line].erase(editor[line].begin() + cursor, editor[line].end());
          editor.insert(editor.begin() + line + 1, tmp);
          cursor = 0;
          line++;
        }
        else {
          if (cursor == editor[line].size()) {
            editor[line] += buffer;
            cursor = editor[line].size();
          }
          else {
            editor[line].insert(editor[line].begin() + cursor, buffer.begin(), buffer.end());
            cursor += buffer.size();
          }
        }
      }
      break;
    }
  }

  for (vector<string>::size_type i = 0; i < editor.size(); i++) {
    result += editor[i];
    result += "\n";
  }

  cout << result;

  return 0;
}

