#include <iostream>
#include <vector>

#define ASSERT( condition, message ){ \
   if ( !(condition) ){ \
     cout << "Assertion failed: " << message << endl; \
     exit( EXIT_FAILURE );			      \
   } \
}

using namespace std;

class Editor{
  
private:
  
  // parameters
  int cursor;
  string text;
  string buffer;
  
public:
  
  // initialize parameters
  Editor(string initialText){
    cursor = 0;
    text = initialText;
    buffer = "";
  }
  
  // return text
  string getText(){
    return text;
  }
  
  // functions of commands
  
  void command_a(){
    
    while(1){
      
      if( cursor == 0 ) return;
      
      cursor--;
      
      if( text[cursor] == '\n' ) break;
      
    }
    
    cursor++;
    
  }
  
  void command_e(){
    
    while(1){
      
      if( text[cursor] == '\n' ) break;
      
      cursor++;
      
    }
    
  }
  
  void command_p(){
    
    command_a();
    
    if( cursor == 0 ) return;
    
    cursor--;
    
    command_a();
    
  }
  
  void command_n(){
    
    command_e();
    
    if( cursor != (int)text.size() - 1 ) cursor++;
    else command_a();
    
  }
  
  void command_f(){
    
    if( cursor != (int)text.size() - 1 ) cursor++;
    
  }
  
  void command_b(){
    
    if( cursor != 0 ) cursor--;
    
  }
  
  void command_d(){
    
    if( (int)text.size() == 0 ) return;
    if( cursor == (int)text.size() - 1 ) return;
    
    string newText;
    
    newText += text.substr( 0, cursor );
    newText += text.substr( cursor + 1 );
    
    text = newText;
    
  }
  
  void command_k(){
    
    if( cursor == (int)text.size() - 1 ) return;
    
    if( text[cursor] == '\n' ){
      command_d();
      buffer = '\n';
      return;
    }
    
    int beginIdx = cursor;
    int endIdx = cursor;
      
    while(1){
	
      endIdx++;
      
      if( text[endIdx] == '\n' ) break;
      
    }
    
    endIdx--;
    
    buffer = text.substr( beginIdx, endIdx - beginIdx + 1 );
    
    string newText;
    
    newText += text.substr( 0, beginIdx );
    newText += text.substr( endIdx + 1 );
    
    text = newText;
      
  }

  void command_y(){
    
    string newText;
    
    newText += text.substr( 0, cursor );
    newText += buffer;
    newText += text.substr( cursor );
    
    text = newText;
    
    cursor += (int)buffer.size();
    
  }
  
};

// input text
// modify : initialText
void inputText(string &initialText){
  
  int line_cnt = 0;
  
  while(1){
    
    string oneline;
    getline( cin, oneline );
    
    if( oneline == "END_OF_TEXT" ) break;
    
    ASSERT( (int)oneline.size() <= 20, "The number of characters in a line given as input > 20" );
    
    initialText += oneline;
    initialText += '\n';
    
    line_cnt++;
    
  }
  
  ASSERT( 0 < (int)initialText.size() , "text should be consist of several lines." );
  
  ASSERT( line_cnt <= 10, "The number of lines in the text given as input > 10" );
  
}

// input commands
// modify : commands
void inputCommands(vector<char> &commands){
  
  int command_cnt = 0;
  
  while(1){
    
    char command;
    cin >> command;
    
    if( command == '-' ) break;
    
    commands.push_back( command );
    
    command_cnt++;
    
  }
  
  ASSERT( command_cnt <= 300, "The number of commands > 300" );
  
}

void checkText(Editor &editor){
  
  string text = editor.getText();
  int line_cnt = 0;
  int char_cnt = 0;
  int max_char_cnt = 0;
  
  for(char c : text ){
    
    if( c == '\n' ){
      line_cnt++;
      max_char_cnt = max( max_char_cnt, char_cnt );
      char_cnt = 0;
    }
    else char_cnt++;
    
  }
  
  ASSERT( line_cnt <= 100, "The maximum possible number of lines in the text during operations <= 100" );
  
  ASSERT( max_char_cnt <= 1000, "The maximum possible number of characters in a line during operations <= 1000" );
  
}

// execute commands
// input : commands, modify : editor
void executeCommands(const vector<char> commands, Editor &editor){
  
  for( char command : commands ){
    
    if( command == 'a' ) editor.command_a();
    else if( command == 'e' ) editor.command_e();
    else if( command == 'p' ) editor.command_p();
    else if( command == 'n' ) editor.command_n();
    else if( command == 'f' ) editor.command_f();
    else if( command == 'b' ) editor.command_b();
    else if( command == 'd' ) editor.command_d();
    else if( command == 'k' ) editor.command_k();
    else if( command == 'y' ) editor.command_y();
    else{ // error
      string error_message = "command " + string(1,command) + " is not exist.";
      ASSERT( 0, error_message );
    }
    
    checkText( editor );
    
  }
  
}

// output text
// input : editor
void outputText(Editor editor){
  
  string text = editor.getText();
  
  cout << text;
  
}

int main(){
  
  string initialText; // text of input
  inputText( initialText );
  
  vector<char> commands; // commands of input
  inputCommands( commands );
  
  Editor editor( initialText ); // editor
  executeCommands( commands, editor );
  
  outputText( editor );
  
  return 0;
}



Measured memory_bytes: 3068