#include<iostream>
#include<vector>
using namespace std;

struct Vertex{
	int key;
	int parent;
	int left;
	int right;
};

vector<Vertex> V;
int root=-1;

void Preorder(int r){
	cout <<" "<< V[r].key;
	if(V[r].left!=-1) Preorder(V[r].left);
	if(V[r].right!=-1) Preorder(V[r].right);
	return;
}

void Inorder(int r){
	if(V[r].left!=-1) Inorder(V[r].left);
	cout <<" "<< V[r].key;
	if(V[r].right!=-1) Inorder(V[r].right);
	return;
}

void Postorder(int r){
	if(V[r].left!=-1) Postorder(V[r].left);
	if(V[r].right!=-1) Postorder(V[r].right);
	cout <<" "<< V[r].key;
	return;
}

void insert(int k){
	Vertex z;
	z.key=k;
	z.left=-1;
	z.right=-1;
	
	int y=-1;
	int x=root;
	while(x!=-1){
		y=x;
		if (z.key<V[x].key){
			x=V[x].left;
		}else{
			x=V[x].right;
		}
	}
	z.parent=y;
	V.push_back(z);
	
	if(y==-1){
		root = 0;
	}else if(z.key < V[y].key){
		V[y].left=V.size()-1;
	}else{
		V[y].right=V.size()-1;
	}
}

void printVertex(int i){
	cout << V[i].key <<" " <<V[i].parent <<" " << V[i].left << " " << V[i].right <<endl;
}

void print(){
	Inorder(root);
	cout <<endl;
	Preorder(root);
	cout <<endl;
}

int find(int k){
	int v=root;
	while(1){ 
		if(k == V[v].key){
			return v;
		}else if(k < V[v].key){
			if(V[v].left==-1){
				return -1;
			}else{
				v=V[v].left;
			}
		}else{
			if(V[v].right==-1){
				return -1;
			}else{
				v=V[v].right;
			}
		}
	}
}

int next(int k){
	int u=V[k].right;
	while(V[u].left!=-1){u=V[u].left;}
	return u;
}

void del(int k){
	int vk=find(k);
	int par=V[vk].parent;
	//printVertex(vk);
	//cout << endl;
	if(V[vk].left==-1 && V[vk].right==-1){
		if(par == -1){
			root = -1;
		}else if(V[par].left==vk) {
			V[par].left=-1;
		}else{
			V[par].right=-1;
		}
	}else if(V[vk].left==-1 && V[vk].right!=-1){
		if(par == -1){
			root=V[vk].right;
		}else if(V[par].left==vk){
			V[par].left=V[vk].right;
		}else{
			V[par].right=V[vk].right;
		}
		V[V[vk].right].parent=par;
	}else if(V[vk].left!=-1 && V[vk].right==-1){
		if(par==-1){
			root=V[vk].left;
		}else if(V[par].left==vk) {
			V[par].left=V[vk].left;
		}else{
			V[par].right=V[vk].left;
		}
		V[V[vk].left].parent=par;
	}else{
		int x=V[next(vk)].key;
		del(x);
		V[vk].key=x;
	}
}

int main(){
	int n,k;
	string s;
	cin >> n;
	for (int i=0;i<n;i++){
		cin >> s;
		if(s=="insert"){
			cin >> k;
			insert(k);
		}else if(s=="find"){
			cin >> k;
			if(find(k)!=-1){
				cout <<"yes"<<endl;
			}else{
				cout << "no" <<endl;
			}
		}else if(s=="delete"){
			cin >> k;
			del(k);
		}else{
			print();
		}
	}
	/*
	for(int i=0;i<n;i++){
		printVertex(i);
		cout <<endl;
	}
	*/
	return 0;
}
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
template<typename V> typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

bool f(string s) {
  vs t;
  t.pb("the");
  t.pb("the.");
  t.pb("this");
  t.pb("this.");
  t.pb("that");
  t.pb("that.");
  stringstream ss;
  ss << s;
  string x;
  while (ss >> x) {
    if (find(all(t), x) != t.end()) return true;
  }
  return false;
}

void nextS(string &s) {
  rep(i, s.size()) {
    if ('a' <= s[i] && s[i] <= 'z') {
      s[i] = (s[i] - 'a' + 1) % 26 + 'a';
    }
  }
}

void solve() {
  string s;
  while (getline(cin, s)) {
    do {
      if (f(s)) {
        cout << s << endl;
        break;
      }
    } while (nextS(s), 1);
  }
}