#include <bits/stdc++.h>
using namespace std;

vector<string> text;
string buffer;

int main()
{
	string input;
	
	while (true){
		getline(cin, input);
		if (input == "END_OF_TEXT") break;
		text.push_back(input);
	}
	
	int p1 = 0;
	int p2 = 0;
	
	while (true){
		cin >> input;
		if (input == "-") break;
		
		if (input == "a") p2 = 0;
		if (input == "e") p2 = text[p1].size();
		if (input == "p"){
			if (p1 > 0) p1--;
			p2 = 0;
		}
		if (input == "n"){
			if (p1 + 1 < text.size()) p1++;
			p2 = 0;
		}
		if (input == "f"){
			if (p2 < text[p1].size()) p2++;
			else if (p2 == text[p1].size() && p1 + 1 < text.size()){
				p1++;
				p2 = 0;
			}
		}
		if (input == "b"){
			if (p2 > 0) p2--;
			else if (p1 > 0){
				p1--;
				p2 = text[p1].size();
			}
		}
		if (input == "d"){
			if (p2 < text[p1].size()) text[p1].erase(p2, 1);
			else if (p1 + 1 < text.size()){
				text[p1] += text[p1 + 1];
				text.erase(text.begin() + p1 + 1);
			}
		}
		if (input == "k"){
			if (p2 == text[p1].size()){
				if (p1 + 1 < text.size()){
					if (p2 < text[p1].size()) text[p1].erase(p2, 1);
					else if (p1 + 1 < text.size()){
						text[p1] += text[p1 + 1];
						text.erase(text.begin() + p1 + 1);
					}
					buffer = "\n";
				}
			}
			else {
				buffer = string(text[p1], p2, text[p1].size() - p2);
				text[p1].erase(p2);
				p2 = text[p1].size();
			}
		}
		if (input == "y"){
			if (buffer == "") continue;
			if (buffer == "\n"){
				string tmp(text[p1], p2, text[p1].size() - p2);
				text[p1].erase(p2);
				text.insert(text.begin() + p1 + 1, tmp);
				p1++;
				p2 = 0;
			}
			else {
				text[p1].insert(p2, buffer);
				p2 += buffer.size();
			}
		}
	}
	
	for (int i = 0; i < text.size(); i++){
		cout << text[i] << endl;
	}
}