#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<map>
#include<vector>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
string cash;

void A(P& cursor)
{
  cursor.S = 0;
}

void E(P& cursor,vector<string>& buffer)
{
  cursor.S = buffer[cursor.F].size()-1;
}

void PP(P& cursor,vector<string>& buffer)
{
  if(cursor.F-1 >= 0)
    cursor.F--,cursor.S = 0;
  else 
    cursor.S = 0;
}

void N(P& cursor,vector<string>& buffer)
{
  if(cursor.F+1 < buffer.size())
    cursor.F++,cursor.S = 0;
  else
    cursor.S = 0;
}

void F(P& cursor,vector<string>& buffer)
{
  if(cursor.S+1 < buffer[cursor.F].size())
    cursor.S++;
  else if(buffer[cursor.F].size()-1 == cursor.S && cursor.F+1 < buffer.size())
    cursor.F++,cursor.S = 0;
}

void B(P& cursor,vector<string>& buffer)
{
  if(cursor.S != 0)
    cursor.S--;
  else if(cursor.S == 0 && cursor.F-1 >= 0)
    cursor.F--,cursor.S = buffer[cursor.F].size()-1;
}

void D(P& cursor,vector<string>& buffer)
{
  if(cursor.S != buffer[cursor.F].size()-1)
    {
      string pre,bef;
    for(int i=0;i<buffer[cursor.F].size();i++)
      if(i == cursor.S)
	continue;
      else if(i < cursor.S)      
	pre += buffer[cursor.F][i];
      else 
	bef += buffer[cursor.F][i];

    buffer[cursor.F] = pre;
    buffer[cursor.F] += bef;
    }
    else if(cursor.F+1 < buffer.size())
    {
      int Size = buffer[cursor.F].size();
      for(int i=0;i<buffer[cursor.F+1].size();i++)
	{
	  if(cursor.S+i < Size)
	    buffer[cursor.F][cursor.S+i] = buffer[cursor.F+1][i];
	  else 
	    buffer[cursor.F] += buffer[cursor.F+1][i];
	}

      for(int i=cursor.F+1;i<buffer.size()-1;i++)
	buffer[i] = buffer[i+1];
      buffer.erase(buffer.end());
    }
}

void K(P& cursor,vector<string>& buffer)
{
  if(cursor.S == buffer[cursor.F].size()-1)
    {
      D(cursor,buffer);
      cash = " ";   
    }
  else
    {
      cash.clear();
      string pre;
      for(int i=0;i<buffer[cursor.F].size();i++)
	if(cursor.S <= i)
	  cash += buffer[cursor.F][i];
	else 
	  pre += buffer[cursor.F][i];
      buffer[cursor.F] = pre + " ";
      cursor.S = buffer[cursor.F].size()-1;
    }
}

void Y(P& cursor,vector<string>& buffer)
{
  if(cash.size() == 0)
    return;

  if(cash.size() == 1)
    {   
      buffer.push_back("");
      for(int i=buffer.size()-1;i>cursor.F;i--)
	buffer[i] = buffer[i-1];
     
      string pre,bef;
      for(int i=0;i<buffer[cursor.F].size();i++)
	{
	  if(i < cursor.S)
	    pre += buffer[cursor.F][i];
	  else 
	    bef += buffer[cursor.F][i];
	}	
      buffer[cursor.F].clear();
      buffer[cursor.F] = pre+" ";
      buffer[cursor.F+1] = bef;
      cursor.F++,cursor.S = 0;
      return;
    }

  string pre,bef;
      for(int i=0;i<buffer[cursor.F].size();i++)
	{
	  if(i < cursor.S)
	    pre += buffer[cursor.F][i];
	  else 
	    bef += buffer[cursor.F][i];
	}	
     
      buffer[cursor.F] = pre;
      buffer[cursor.F] += cash.substr(0,cash.size()-1);
      cursor.S = buffer[cursor.F].size();
      buffer[cursor.F] += bef;
      
}

int main()
{
  
 
      vector<string> buffer;
      string line;
      P cursor = P(0,0); //P(y、x)
      cash.clear();

      while(getline(cin,line),line != "END_OF_TEXT")
	{
	  buffer.push_back(line);  
	  buffer.back() += " ";
	}

      if(buffer.size() == 0)
	buffer.push_back(" ");

      while(cin >> line,line != "-")
	{
	  if(buffer.size() == 0 || (buffer.size() == 1 && buffer[0].size() == 1))
	    continue;
	  if(line[0] == 'a')
	    A(cursor);
	  else if(line[0] == 'e')
	    E(cursor,buffer);
	  else if(line[0] == 'p')
	    PP(cursor,buffer);
	  else if(line[0] == 'n')
	    N(cursor,buffer);
	  else if(line[0] == 'f')
	    F(cursor,buffer);
	  else if(line[0] == 'b')
	    B(cursor,buffer);
	  else if(line[0] == 'd')
	    D(cursor,buffer);
	  else if(line[0] == 'k')
	    K(cursor,buffer);
	  else if(line[0] == 'y')
	    Y(cursor,buffer);
	  //cout <<line <<  " ----------" << endl;
	  //cout << "cursor(" << cursor.F << " "  << cursor.S << ")" << endl;
	  //cout << "cash = " << cash << endl;
	  //for(int i=0;i<buffer.size();i++)
	  //cout << buffer[i].substr(0,buffer[i].size()-1) << "||| " << buffer[i].size() << endl;	  

	}

      for(int i=0;i<buffer.size();i++)
	cout << buffer[i].substr(0,buffer[i].size()-1) << endl;
	

    
 
  return 0;
}

Measured memory_bytes: 1208