#include<iostream>
#include<vector>
#include<string>
using namespace std;

class TextEditor{
private:
  string buf;
  vector<string> text;
  int row,pos;

  void InputText();
  void MoveCursor(char com);
  void DeleteCharacter();
  void CutLine();
  void PasteBuffer();
  
public:
  TextEditor();

  void ProcessCommand(char com);
  void PrintText();
};




int main(){

  TextEditor editor;

  char command;

  while(cin >> command, command != '-'){
    editor.ProcessCommand(command);
  }
  
  editor.PrintText();
  
  return 0;
}




TextEditor::TextEditor(){
  buf = "";
  pos = row = 0;
  InputText();
}


void TextEditor::InputText(){
  string input;

  while(getline(cin,input), input!="END_OF_TEXT"){
    text.push_back( input );
  }
}


void TextEditor::MoveCursor(char com){
  switch(com){
  case 'a': //move cursor to the first character of line
    pos = 0;
    break;
    
  case 'e': // move the cursor to the end-of-line
    pos = text[row].size();
    break;
    
  case 'p': //move upper line
    if(row > 0)row--;
    pos = 0;
    break;

  case 'n': //move lower line
    if(row < text.size()-1 )row++;
    pos = 0;
    break;

  case 'f': //move cursor right
    if( pos != text[row].size() ) pos++;
    else if( row < text.size()-1 ){ pos = 0; row++; }
    break;

  case 'b': //move cursor left
    if( pos != 0 ) pos--;
    else if( row > 0 ){ pos = text[--row].size(); }
    break;
  }
}


void TextEditor::DeleteCharacter(){
  if(pos != text[row].size() ){
    text[row].erase( text[row].begin() + pos );
  }
  else if( row < text.size()-1 ){
    text[row] += text[row+1];
    text.erase( text.begin() + row + 1 );
  }
}


void TextEditor::CutLine(){
  if(pos == text[row].size()){
    DeleteCharacter();
    buf = "\n";
  }
  else{
    buf = text[row].substr(pos);
    text[row].erase( text[row].begin()+pos , text[row].end() );
    pos = text[row].size();
  }
}


void TextEditor::PasteBuffer(){
  if( buf == "\n" ){
    string newline = text[row].substr(pos);
    text[row].erase( text[row].begin()+pos , text[row].end() );
    text.insert( text.begin()+row+1 , newline );
    row++; pos = 0;
  }

  else{
    text[row].insert( pos, buf );
    pos += buf.size();
  }

}


void TextEditor::ProcessCommand(char com){


  switch(com){
  case 'd':
    DeleteCharacter();
    break;
    
  case 'k':
    CutLine();
    break;

  case 'y':
    PasteBuffer();
    break;

  default:
    MoveCursor(com);
  }
  
}


void TextEditor::PrintText(){
  for(auto line : text){
    cout << line << endl;
  }
}

