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
# coding: utf-8

class Node:
    def __init__(self, me, left, right):
        self.me = me
        self.left = left
        self.right = right
        self.parent = None

class NodeTree:
    def __init__(self, root=None):
        self.root = root
        
    def insert(self,z):
        y = None
        x = self.root
        while x != None:
            y = x
            if z.me < x.me:
                x = x.left
            else:
                x = x.right
        z.parent = y
        
        if y == None:
            self.root = z
        elif z.me < y.me:
            y.left = z
        else:
            y.right = z

    def find(self, key):
        y = None
        x = self.root
        while True:
            if x.me == key:
                return x
            elif x.me > key:
                if x.left != None:
                    x = x.left
                else:
                    return None
            else:
                if x.right != None:
                    x = x.right
                else:
                    return None
    
    def delete(self, key):
        z = self.find(key)
        if z.left == None and z.right == None:
            if z.parent.left == z:
                z.parent.left = None
            else:
                z.parent.right = None
        elif z.left != None and z.right == None:
            if z.parent.left == z:
                z.parent.left = z.left
                z.left.parent = z.parent
            if z.parent.right == z:
                z.parent.right = z.left
                z.left.parent = z.parent
        elif z.left == None and z.right != None:
            if z.parent.left == z:
                z.parent.left = z.right
                z.right.parent = z.parent
            if z.parent.right == z:
                z.parent.right = z.right
                z.right.parent = z.parent
        else:
            #y = nextPos(z) # z.right.left.left....の最下層
            y = z.right
            while y.left != None:
                y = y.left
            p = y.me
            self.delete(y.me)
            z.me = p
            
    def getPreorder(self, node, ans):
        ans.append(node)
        if node.left != None:
            self.getPreorder(node.left, ans)
        if node.right != None:
            self.getPreorder(node.right, ans)
            
    def getInorder(self, node, ans):
        if node.left != None:
            self.getInorder(node.left, ans)
        ans.append(node)
        if node.right != None:
            self.getInorder(node.right, ans)

        
def printAns(ans):
    for a in ans:
        print(" " + str(a.me),end="")
    print()
    
m = int(input().rstrip())
nt = NodeTree()
for i in range(m):
    line = input().rstrip().split()
    if line[0] == "insert":
        nt.insert(Node(int(line[1]),None,None))
    elif line[0] == "find":
        if nt.find(int(line[1])) != None:
            print("yes")
        else:
            print("no")
    elif line[0] == "delete":
        nt.delete(int(line[1]))
    else:
        ans = []
        nt.getInorder(nt.root,ans)
        printAns(ans)
        ans = []
        nt.getPreorder(nt.root,ans)
        printAns(ans)
