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

using namespace std;

vector<string> buf;
int row;
int column;
string rem;

void printbuf(){
  cout << row << ' ' << column << " \"" << rem << "\"" << endl;
  for(int i=0;i<buf.size();i++){
    cout << buf[i];
  }
}

int main(){
  string in;
  while(getline(cin, in)){
    if(in == "END_OF_TEXT") break;
    //if(in == "END_OF_TEXT" || in == "1") break;
    in += '\n';
    buf.push_back(in);
  }
  char c;
  row = 0;
  column = 0;
  while(cin >> c){
    if(c == '-') break;
    switch(c){
      case 'a':
        column = 0;
        break;
      case 'e':
        column = (int)buf[row].size()-1;
        break;
      case 'p':
        column = 0;
        if(row > 0) --row;
        break;
      case 'n':
        column = 0;
        if(row+1 < buf.size()) ++row;
        break;
      case 'f':
        if(column+1 >= buf[row].size()){
          if(row+1 < buf.size()){
            column = 0;
            ++row;
          }
        }else{
          ++column;
        }
        break;
      case 'b':
        if(column == 0){
          if(row > 0){
            --row;
            column = (int)buf[row].size()-1;
          }
        }else{
          --column;
        }
        break;
      case 'd':
        if(column+1 == buf[row].size()){
          if(row+1 != buf.size()){
            buf[row].erase((int)buf[row].size()-1);
            buf[row] += buf[row+1];
            vector<string>::iterator ite = buf.begin();
            for(int i=0;i<row+1;i++) ++ite;
            buf.erase(ite);
          }
        }else{
          buf[row].erase(column, 1);
        }
        break;
      case 'k':
        rem = "";
        if(column+1 == buf[row].size()){
          if(row+1 != buf.size()){
            buf[row].erase((int)buf[row].size()-1);
            buf[row] += buf[row+1];
            vector<string>::iterator ite = buf.begin();
            for(int i=0;i<row+1;i++) ++ite;
            buf.erase(ite);
            rem += '\n';
          }
        }else{
          for(int i=column;i+1<buf[row].size();++i){
            rem += buf[row][i];
          }
          string::iterator ite = buf[row].begin();
          for(int i=0;i<column;i++) ++ite;
          buf[row].erase(ite, buf[row].end());
          buf[row] += '\n';
        }
        break;
      case 'y':
        if(rem.size() != 0){
          int irem;
          string back = buf[row].substr(column);
          buf[row] = buf[row].substr(0, column);
          for(irem = 0;irem<rem.size();irem++){
            buf[row] += rem[irem];
            ++column;
            if(rem[irem] == '\n'){
              vector<string>::iterator ite = buf.begin();
              for(int i=0;i<row+1;i++) ++ite;
              buf.insert(ite, "");
              column = 0;
              ++row;
            }
          }
          buf[row] += back;
        }
        break;
    }
    //printbuf();
  }
  for(int i=0;i<buf.size();i++){
    cout << buf[i];
  }
  //cout << endl;

  return 0;
}