#include <bits/stdc++.h>
using namespace std;

class Emacs {
private:
  string buffer;
  // all elements has LF at the end.
  vector<string> text; // text[row][column]
  int row;
  int column;

  int findFirstColumn() { return 0; } int findLastColumn() { return text[row].size(); }

  int findFirstRow(){return 0;}
  int findLastRow(){return text.size()-1;}
  void shiftRow(){
    text[row] = text[row] + text[row+1];
    text.erase(row+1 + text.begin());
  }

  bool isLastRow(){
    auto end_row_idx = findLastRow();
    return row == end_row_idx;
  }
  bool isFirstRow(){
    return row == findFirstRow();
  }
  bool isLastColumn() {
    auto end_column_idx = findLastColumn();
    return column == end_column_idx;
  }
  bool isFirstColumn() {
    return column == findFirstColumn();
  }

public:
#ifdef DEBUG
  void print(){
    cout << "(" << row << ", " << column << ")" << endl;
    cout << "===" << buffer << endl;
    for(int i = 0; i < (int)text.size(); i++){
      for(int j = 0; j < (int)text[i].size(); j++){
        if(i == row && j == column){
          cout << "\x1b[7m";
        }
        cout << text[i][j];
        cout << "\x1b[0m";
      }
      if(i == row && (int)text[i].size() == column){
        cout << "\x1b[7m";
      }
      cout << " ";
      cout << "\x1b[0m";
      cout << endl;
    }
    cout << "---" << endl << endl;
  }
#endif
  Emacs(vector<string> text){
    buffer = "";
    this->text = text;
    row = 0;
    column = 0;
  }
  vector<string> getText(){return this->text;}
  void distinguishCommand(char command) {
    switch (command) {
    case 'a': {
      column = findFirstColumn();
      break;
    }
    case 'e': {
      column = findLastColumn();
      break;
    }
    case 'p': {
      column = findFirstColumn();
      auto next_row = row - 1;
      if (next_row >= findFirstRow()) {
        row = next_row;
      }
      break;
    }
    case 'n': {
      column = findFirstColumn();
      auto next_row = row + 1;
      if (next_row <= findLastRow()) {
        row = next_row;
      }
      break;
    }
    case 'f': {
      if (!isLastColumn()) {
        column++;
      } else if (!isLastRow()) {
        column = findFirstColumn();
        row++;
      }
      break;
    }
    case 'b': {
      if (!isFirstColumn()) {
        column--;
      } else if (!isFirstRow()) {
        row--;
        column = findLastColumn();
      }
      break;
    }
    case 'd': {
      if(!isLastColumn()){
        text[row].erase(column + text[row].begin());
      }else if(!isLastRow()){
        shiftRow();
      }
      break;
    }
    case 'k': {
      if(isLastColumn() && !isLastRow()){
        shiftRow();
        buffer = "\n";
      }else if(!isLastColumn()){
        buffer = string(text[row].begin() + column, text[row].end());
        text[row].erase(text[row].begin() + column, text[row].end());
        column = findLastColumn();
      }
      break;
    }
    case 'y': {
      if(buffer == "\n"){
        string left = string(text[row].begin(), text[row].begin()+column);
        string right = string(text[row].begin() + column, text[row].end());

        text[row] = left;
        text.insert(text.begin() + row+1, right);
        row++;
        column = findFirstColumn();
      }else if(!buffer.empty()){
         // text[row].insert(column, buffer);
         text[row].insert(column, buffer);
         column += buffer.length();
      }
      break;
    }
    default: {
      cerr << "Invalid command: " << command << endl;
      exit(1);
    }
    }
  }
};

int main(void) {
  vector<string> text;
  while(1){
    string str;
    getline(cin, str);
    if(str == "END_OF_TEXT") break;
    text.push_back(str);
  }

  Emacs emacs(text);

  while(1){
    char c; cin >> c;
    if(c == '-') break;
    emacs.distinguishCommand(c);
#ifdef DEBUG
    cout << "==" << c << "==" << endl;
    emacs.print();
#endif
  }

  vector<string> res = emacs.getText();
  for(int i = 0; i < (int)res.size(); i++){
    for(int j = 0; j < (int)res[i].size(); j++){
      cout << res[i][j];
    }
    cout << endl;
  }
  return 0;
}


Measured memory_bytes: 3172