Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

* insert  $k$: Insert a node containing $k$ as key into $T$.
* find $k$: Report whether $T$ has a node containing $k$.
* delete $k$: Delete a node containing $k$.
* print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.



The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

1. If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).
2. If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.
3. If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Constraints

* The number of operations $\leq 500,000$
* The number of print operations $\leq 10$.
* $-2,000,000,000 \leq key \leq 2,000,000,000$
* The height of the binary tree does not exceed 100 if you employ the above pseudo code.
* The keys in the binary search tree are all different.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Example

Input

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print


Output

yes
yes
yes
no
no
no
yes
yes
 1 2 3 7 8 22
 8 2 1 3 7 22
 1 2 8 22
 8 2 1 22
class BinarySearchTree:
    def __init__(self):
        self.lcs = {}
        self.rcs = {}
        self.preorder = []
        self.inorder = []
        self.root = None

    def insert(self, key):
        if self.root is None:
            self.root = key
            self.lcs[key] = None
            self.rcs[key] = None
            return
        parent = self.root
        while True:
            if key < parent:
                if self.lcs[parent] is None:
                    self.lcs[parent] = key
                    self.lcs[key] = None
                    self.rcs[key] = None
                    break
                else:
                    parent = self.lcs[parent]
            else:
                if self.rcs[parent] is None:
                    self.rcs[parent] = key
                    self.lcs[key] = None
                    self.rcs[key] = None
                    break
                else:
                    parent = self.rcs[parent]

    def _find(self, key, cur):
        if key == cur:
            return True
        elif key < cur:
            if self.lcs[cur] is None:
                return False
            else:
                return self._find(key, self.lcs[cur])
        else:
            if self.rcs[cur] is None:
                return False
            else:
                return self._find(key, self.rcs[cur])

    def find(self, key):
        return self._find(key, self.root)

    def _search(self, cur):
        self.preorder.append(cur)
        if self.lcs[cur]:
            self._search(self.lcs[cur])
        self.inorder.append(cur)
        if self.rcs[cur]:
            self._search(self.rcs[cur])

    def print(self):
        self.preorder = []
        self.inorder = []
        self._search(self.root)
        print(' ' + ' '.join(map(str, self.inorder)))
        print(' ' + ' '.join(map(str, self.preorder)))

    def _delete(self, key, cur, parent):
        if key == cur:
            if self.lcs[cur] is None:
                # print('R', key)
                if self.lcs[parent] == cur:
                    self.lcs[parent] = self.rcs[cur]
                else:
                    self.rcs[parent] = self.rcs[cur]
            elif self.rcs[cur] is None:
                # print('L', key)
                if self.lcs[parent] == cur:
                    self.lcs[parent] = self.lcs[cur]
                else:
                    self.rcs[parent] = self.lcs[cur]
            else:
                # print('B', key)
                n = self.inorder[self.inorder.index(key) + 1]
                self._delete(n, cur, parent)
                self.lcs[n], self.rcs[n] = self.lcs[cur], self.rcs[cur]
                self.lcs[cur], self.rcs[cur] = None, None
                if key == self.root:
                    self.root = n
                elif self.lcs[parent] == cur:
                    self.lcs[parent] = n
                else:
                    self.rcs[parent] = n
            return True
        elif key < cur:
            if self.lcs[cur] is None:
                return False
            else:
                return self._delete(key, self.lcs[cur], cur)
        else:
            if self.rcs[cur] is None:
                return False
            else:
                return self._delete(key, self.rcs[cur], cur)

    def delete(self, key):
        self.preorder = []
        self.inorder = []
        self._search(self.root)
        self._delete(key, self.root, None)


T = BinarySearchTree()
N = int(input())
for _ in range(N):
    s = input().split()
    if s[0] == 'insert':
        T.insert(int(s[1]))
    elif s[0] == 'find':
        print('yes' if T.find(int(s[1])) else 'no')
    elif s[0] == 'delete':
        T.delete(int(s[1]))
    else:
        T.print()

