#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
//#include <limits>
//#include <sstream>
//#include <functional>
using namespace std;

#define len(array)  (sizeof (array) / sizeof *(array))
#define rep(i, s, e) for(int i = s;i < e;i++)
#define rrep(i, e, s) for(int i = e;s <= i;i--)
#define vrange(v) v.begin(), v.end()
#define vrrange(v) v.rbegin(), v.rend()
#define vsort(v) sort(vrange(v))
#define vrsort(v) sort(vrrange(v))
#define arange(a) a, a + len(a)
#define asort(a) sort(arange(a))
#define arsort(a, t) sort(arange(a), greater<t>())
#define afill(a, v) fill(arange(a), v)
#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)
#define fmax(a, b) (a < b? b : a)
#define fmin(a, b) (a > b? b : a)
#define fabs(a) (a < 0? -a : a)
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-10;
//const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
//const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
//const int weight[] = {0,1,10,100,1000,10000,100000,1000000,10000000};
//priority_queue<int, vector<int>, greater<int>> q;
typedef struct _Node {
  _Node(int arg1 = 0, int arg2 = 0 , int arg3 = 0) {
	i = arg1;
	j = arg2;
	k = arg3;
  }
  int i,j,k;
  bool operator <(const struct _Node &e) const{
    return i == e.i? j < e.j : i < e.i;
  }
  bool operator >(const struct _Node &e) const{
    return i == e.i? j > e.j : i > e.i;
  }
}node;


void doIt(){
  string s = "", t, buffer = "";
  char com;
  int pos = 0;
  bool bLine, bContinue = true;
  while(1){
	//cin >> t;
	getline(cin, t);
	if(t == "END_OF_TEXT") break;
	s += t + '\n';
  }
  //cout << "start.." << endl;
  while(bContinue){
	cin >> com;

	switch(com){
	case 'a':
	  while(pos != 0 && s[pos-1] != '\n') pos--;
	  break;
	case 'e':
	  while(s[pos] != '\n') pos++;
	  break;
	case 'p':
	  bLine = false;
	  //if(pos != 0 && s[pos] == '\n') pos--;
	  while(pos != 0){
		if(s[pos-1] == '\n'){
		  if(bLine) break;
		  else bLine = true;
		}
		pos--;
	  }
	  break;
	case 'n':
	  while(s[pos] != '\n') pos++;
	  if(pos == s.length() - 1){
	  	//pos--;
	  	while(pos != 0 && s[pos-1] != '\n') pos--;
	  }
	  else pos++;
	  // bLine = false;
	  // while(pos != s.length()){
	  // 	if(s[pos+1] == '\n'){
	  // 	  if(bLine) break;
	  // 	  else bLine = true;
	  // 	}
	  // 	pos++;
	  // }
	  break;
	case 'f':
	  if(pos != s.length() - 1) pos++;
	  break;
	case 'b':
	  if(pos != 0) pos--;
	  break;
	case 'd':
	  if(pos != s.length() - 1) s = s.substr(0, pos) + s.substr(pos+1, s.length()-(pos+1));
	  break;
	case 'k':
	  if(s[pos] == '\n'){
		if(pos != s.length()-1){
		  s = s.substr(0, pos) + s.substr(pos+1, s.length()-(pos+1));
		  buffer = "\n";
		}
	  }
	  else{
		int end = pos;
		while(s[end] != '\n') end++;
		buffer = s.substr(pos, end-pos);
		s = s.substr(0, pos) + s.substr(end, s.length()-end);

	  }
	  break;
	case 'y':
	  if(buffer != ""){
		s = s.substr(0, pos) + buffer + s.substr(pos, s.length()-pos);
		pos += buffer.length();
	  }
	  break;
	case '-':
	  bContinue = false;
	  break;
	}

	{
	  // cout << "com = " << com << endl;
	  // cout << "s = [\n" << s << "]" << endl;
	  // cout << "buffer = [" << buffer << "]" << endl;
	  // cout << "pos = " << pos << endl;
	}
  }
cout << s.substr(0, s.length() - 1) << endl;
}

int main() {
  doIt();
  return 0;
}

Measured memory_bytes: 1212