#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

void convert(string& text, int& cursor, string& buffer, char command);

int main()
{	
	string text, buffer;
	int cursor = 0;
	
	for(string line; getline(cin, line) && line != "END_OF_TEXT"; text += line + '\n');
	for(char command; cin >> command && command != '-'; convert(text, cursor, buffer, command));
	
	cout << text;
}

void convert(string& text, int& cursor, string& buffer, char command)
{	
	switch(command)
	{
			/*
			 a
			 J[\ð»ÝÌsÌæª¶ÉÚ®·éB
			 */
		case 'a':
		{
			if(cursor != 0 && text[cursor - 1] != '\n')
			while(--cursor) if(text[cursor] == '\n')
			{
				cursor++;
				break;
			}
			
			break;
		}
			/*
			 e
			 J[\ð»ÝÌsÌsÉÚ®·éB
			 */
		case 'e':
		{
			while(text[cursor] != '\n') cursor++;
			
			break;
		}
			/*
			 p
			 ãÉsª êÎAJ[\ðãÌsÌæª¶ÉÚ®·éB
			 ãÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
			 */
		case 'p':
		{
			if(cursor != 0) while(--cursor) if(text[cursor] == '\n')
			{
				break;
			}

			if(cursor != 0) while(--cursor) if(text[cursor] == '\n')
			{
				cursor++;
				break;
			}
			
			

			break;
		}
			/*
			 n
			 ºÉsª êÎAJ[\ðºÌsÌæª¶ÉÚ®·éB
			 ºÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
			 */
		case 'n':
		{
			int cur = cursor;
			while(text[cur] != '\n') cur++;
			
			if(cur + 1 == text.size())
			{
				if(cursor != 0) while(--cursor) if(text[cursor] == '\n')
				{
					cursor++;
					break;
				}
			}
			else
			{
				cursor = cur + 1;
			}
			
			break;
		}
			/*
			 f
			 J[\ªsÉÈ¢êAJ[\ðPÂEÉÚ®·éB
			 J[\ªsÉ è©ÂºÉsª éêÍAJ[\ðºÌsÌæª¶ÖÚ®·éB
			 »êÈOÌêÍ½àµÈ¢B
			 */
		case 'f':
		{
			cursor += cursor + 1 != text.size();
			
			break;
		}
			/*
			 b
			 J[\ªæª¶ÅÈ¢ÈçÎAJ[\ðPÂ¶ÉÚ®·éB
			 J[\ªæª¶Å è©ÂãÉsª éêÍAJ[\ðãÌsÌsÖÚ®·éB
			 »êÈOÌêÍ½àµÈ¢
			 */
		case 'b':
		{
			cursor -= 0 < cursor;
			
			break;
		}
			/*
			 d
			 J[\ª¶ðw·êÍ»Ì¶ðí·éBí³ê½¶ÌE¤Ì¶ñÍ¶ÉVtg³êéB
			 J[\ªsðwµºÉsª éêÍAºÌsð»ÌÜÜJ[\ÌÊuÉq°éBÈºÌsÍãÉVtg³êéB
			 »êÈOÌêÍ½àµÈ¢B
			 */
		case 'd':
		{
			if(cursor + 1 != text.size()) text.erase(text.begin() + cursor);
			break;
		}
			/*
			 k
			 J[\ªsÉ éêAºÉsª êÎAd Æ¯¶ìðµobt@ÉPÂÌüsðL^·éB
			 J[\ªsÉÈ¢êAJ[\ªw·¶ðÜß½E¤·×ÄÌ¶ðØèæè»êðobt@ÉL^·éBJ[\Í³ÌsÌsðw·æ¤ÉÈéBSÄÌL^É¨¢Äobt@Íã«³êéB
			 »êÈOÌêÍ½àµÈ¢B
			 */
		case 'k':
		{
			buffer = "";
			if(text[cursor] == '\n')
			{
				if(cursor + 1 == text.size())
					break;
				
				buffer = "\n";
				text.erase(text.begin() + cursor);
			}
			else while(text[cursor] != '\n')
			{
				buffer += text[cursor];
				text.erase(text.begin() + cursor);
			}
			
			break;
		}
			/*
			 y
			 obt@ªóÌêÍÈÉàµÈ¢B
			 obt@ÌàeªüsÌêAJ[\ªw·¶Ì¼OÅüsðs¤BJ[\ÍVµ¢sÌæª¶ÖÚ®·éB
			 obt@Ìàeª¶ñÌêAJ[\ÌÊuÉobt@Ìàeð}ü·éB J[\ÌÊuÍàÆàÆwµÄ¢½¶Ü½ÍsÌêÖÚ®·é
			 */
		case 'y':
		{
			text = text.substr(0, cursor) + buffer + text.substr(cursor);
			cursor += buffer.size();
			
			break;
		}
	}
}