#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

#define TOP_OF_BUFFER 0
#define BEGIN_OF_LINE 0

struct Cursor{
  int x, y;
  Cursor(){
    x = 0, y = 0;
  }
};

vector<string> buffer;
string buf;
bool new_line;
Cursor cursor;
int end;

void output(){
  for(int i = 0 ; i <= end ; i++){
    cout << buffer[i] << endl;
  }
  //cout << endl;
}

void a_command(){
  cursor.x = BEGIN_OF_LINE;
}

void e_command(){
  //cout << buffer[cursor.y].size() << endl;
  cursor.x = buffer[cursor.y].size();
}

void p_command(){
  if(cursor.y == TOP_OF_BUFFER) cursor.x = BEGIN_OF_LINE;
  else{
    cursor.y--;
    cursor.x = BEGIN_OF_LINE;
  }
}

void n_command(){
  if(cursor.y == end) cursor.x = BEGIN_OF_LINE;
  else{
    cursor.y++;
    cursor.x = BEGIN_OF_LINE;
  }
}

void f_command(){
  if(cursor.x != buffer[cursor.y].size()) cursor.x++;
  else if(cursor.x == buffer[cursor.y].size() && cursor.y != end){
    cursor.x = BEGIN_OF_LINE;
    cursor.y++;
  }
}

void b_command(){
  if(cursor.x != BEGIN_OF_LINE) cursor.x--;
  else if(cursor.x == BEGIN_OF_LINE && cursor.y != TOP_OF_BUFFER){
    cursor.x = buffer[cursor.y-1].size();
    cursor.y--;
  }
}

void d_command(){
  if(cursor.x == buffer[cursor.y].size() && cursor.y != end){
    //シフト
    //if(buffer[cursor.y].size() == 0) cursor.x = 0;
    cursor.x = buffer[cursor.y].size();
    buffer[cursor.y] += buffer[cursor.y+1];
    buffer.erase(buffer.begin() + cursor.y+1);
    end--;
  }
  else if(cursor.x != buffer[cursor.y].size()){
    buffer[cursor.y].erase(buffer[cursor.y].begin() + cursor.x);
  }
}

void k_command(){
  if(cursor.x == buffer[cursor.y].size() && cursor.y != end){
    d_command();
    buf = "";
    new_line = true;
  }
  else if(cursor.x != buffer[cursor.y].size()){
    buf = "";
    new_line = false;
    for(int j = cursor.x ; j < buffer[cursor.y].size() ; j++){
      buf += buffer[cursor.y][j];
    }

    int buffer_size = buffer[cursor.y].size();
    for(int j = cursor.x ; j < buffer_size ; j++){
      buffer[cursor.y].erase(buffer[cursor.y].begin() + cursor.x);
      //cout << buffer[cursor.y] << endl;
    }
  }
}

void y_command(){
  //if(buf == "") return;
  string bf = "";
  for(int j = cursor.x ; j < buffer[cursor.y].size() ; j++){
    bf += buffer[cursor.y][j];
  }
  
  
  if(new_line){
    buffer.push_back("");
    //cout << "size = " << buffer.size() << endl;
    for(int i = buffer.size()-1 ; i > cursor.y ; i--){
      buffer[i] = buffer[i-1];
    }
    end++;
    buffer[cursor.y+1] = bf;
    int buffer_size = buffer[cursor.y].size();
    for(int j = cursor.x ; j < buffer_size ; j++){
      buffer[cursor.y].erase(buffer[cursor.y].begin() + cursor.x);
    }
    cursor.y++;
    cursor.x = 0;
  }
  
  //if(buf == "") return;
  
  else{
    if(cursor.x == buffer[cursor.y].size()){
      buffer[cursor.y] += buf;
      cursor.x += buf.size();
    }
    else{
      for(int cnt = 0 ; cnt < bf.size() ; cnt++){
	buffer[cursor.y].erase(buffer[cursor.y].begin() + cursor.x);
      }
      buffer[cursor.y] += buf;
      buffer[cursor.y] += bf;
      cursor.x += buf.size();
      // cout << "came " << buf.size() << endl;
    }
  }
} 

void init(){
  end = buffer.size()-1;
  new_line = false;  
  buf = "";
}
  
int main(){
  string s;
  while(getline(cin, s)){
    if(s == "END_OF_TEXT") break;
    buffer.push_back(s);
  }
  
  init();
  
  char op;
  int Cnt = 0;
  while(cin >> op){
    //cout << "operand = " << op << endl;
    if(op == '-') break;
    if(op == 'a') a_command();
    if(op == 'e') e_command();
    if(op == 'p') p_command();
    if(op == 'n') n_command();
    if(op == 'f') f_command();
    if(op == 'b') b_command();
    if(op == 'd') d_command();
    if(op == 'k') k_command();
    if(op == 'y') y_command();  
    //cout << Cnt++ << endl;
    //if(cursor.x == buffer[cursor.y].size() ) cursor.x = END_OF_LINE;
    //output();
    //cout << "cursor.x = " << cursor.x << ", cursor.y = " << cursor.y << endl << endl;  
    //cout << "new_line = " << new_line << endl;
  }
  
  output();
  return 0;
}

Measured memory_bytes: 1228