#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;

void inputText(vector<string> &text){ //input text
  string str; //1-line string
  while(getline(cin,str)){
    if(str=="END_OF_TEXT")break;
    str += '\n';
    text.push_back(str);
  }
  
  assert(text.size()>0);
}

void outputText(const vector<string> text){ //output text
  for(int i=0;i<(int)text.size();i++){
    cout<<text[i];
  }
}

void cmdA(int &pr, int &pc, string &buffer, vector<string> &text){
  pc = 0;
}

void cmdE(int &pr, int &pc, string &buffer, vector<string> &text){
  pc = (int)text[pr].size() - 1;
}

void cmdP(int &pr, int &pc, string &buffer, vector<string> &text){
  if(pr > 0) pr--, pc = 0;
  else pc = 0;
}

void cmdN(int &pr, int &pc, string &buffer, vector<string> &text){
  if(pr < (int)text.size() - 1) pr++, pc = 0;
  else pc = 0;
}

void cmdF(int &pr, int &pc, string &buffer, vector<string> &text){
  if(pc < (int)text[pr].size() - 1) pc++;
  else if(pr < (int)text.size() - 1) pr++, pc=0;
}

void cmdB(int &pr, int &pc, string &buffer, vector<string> &text){
  if(pc > 0) pc--;
  else if(pr > 0) pr--, pc = (int)text[pr].size() - 1;
}

void cmdD(int &pr, int &pc, string &buffer, vector<string> &text){
  if(text[pr][pc] != '\n'){
    string nstr; // next string
    for(int i=0;i<(int)text[pr].size();i++)
      if(i != pc)nstr += text[pr][i];

    text[pr]=nstr;
  }
  else if(pr < (int)text.size() - 1){
    text[pr].pop_back();
    text[pr] += text[pr+1];
    text.erase(text.begin() + pr + 1);
  }
}

void cmdK(int &pr, int &pc, string &buffer, vector<string> &text){
  if(pc == (int)text[pr].size() - 1){
    if(pr < (int)text.size() - 1){
      cmdD(pr, pc, buffer, text);
      buffer = "\n";
    }
  }
  else {
    buffer = "";
    for(int i=pc;i<(int)text[pr].size()-1;i++)
      buffer += text[pr][i];

    string nstr; //next string
    for(int i=0;i<pc;i++)nstr += text[pr][i];
    nstr += '\n';
    text[pr]=nstr;
  }
}

void cmdY(int &pr, int &pc, string &buffer, vector<string> &text){
  if((int)buffer.size()==0)return;

  if(buffer == "\n"){
    string nstr = text[pr].substr(pc); // next string
    text[pr].erase(text[pr].begin() + pc, text[pr].end());
    text[pr] += '\n';
    text.insert(text.begin()+ pr + 1 , nstr);
    pr++; pc = 0;
  }
  else{
    text[pr].insert(pc, buffer);
    pc += buffer.size();
  }
}



void doCmd(char cmd, int &pr, int &pc, string &buffer, vector<string> &text){
  if(cmd=='a')cmdA(pr, pc, buffer, text);
  if(cmd=='e')cmdE(pr, pc, buffer, text);
  if(cmd=='p')cmdP(pr, pc, buffer, text);
  if(cmd=='n')cmdN(pr, pc, buffer, text);
  if(cmd=='f')cmdF(pr, pc, buffer, text);
  if(cmd=='b')cmdB(pr, pc, buffer, text);
  if(cmd=='d')cmdD(pr, pc, buffer, text);
  if(cmd=='k')cmdK(pr, pc, buffer, text);
  if(cmd=='y')cmdY(pr, pc, buffer, text);  
}

void emacsCmd(vector<string> &text){ //input and execute a command.
  char cmd; // inputted emacs command
  int pr=0, pc=0; //pr is the current row and pc is the current column
  string buffer; // the curren buffer

  while(cin>>cmd){
    if(cmd=='-')break;
    doCmd(cmd, pr, pc, buffer, text);
  }
}


main(){
  vector<string> text;
  inputText(text);
  emacsCmd(text);
  outputText(text);
}



Measured memory_bytes: 3152