package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

const (
	MAX_KEY = 2000000000
	MAX_PRIORITY = 2000000000
)

type node struct {
	key int
	priority int
	parent *node
	left *node
	right *node
}

type treap struct {
	root *node
}

func newTreap() *treap {
	treap := treap{}
	dummy := node{key: MAX_KEY + 1, priority: MAX_PRIORITY + 1}
	treap.root = &dummy
	return &treap
}

// return new root after rotation
func (nd *node) rightRotate() *node {
	newRoot := nd.left
	if newRoot == nil {
		panic(fmt.Errorf("expect left of node %d is not null", nd.key))
	}

	nd.left = newRoot.right
	if nd.left != nil {
		nd.left.parent = nd
	}

	newRoot.parent = nd.parent
	if newRoot.parent != nil {
		if newRoot.parent.left == nd {
			newRoot.parent.left = newRoot
		} else {
			newRoot.parent.right = newRoot
		}
	}
	nd.parent = newRoot

	newRoot.right = nd
	return newRoot
}

// return new root after rotation
func (nd *node) leftRotate() *node {
	newRoot := nd.right
	if newRoot == nil {
		panic(fmt.Errorf("expect right of node %d is not null", nd.key))
	}

	nd.right = newRoot.left
	if nd.right != nil {
		nd.right.parent = nd
	}

	newRoot.parent = nd.parent
	if newRoot.parent != nil {
		if newRoot.parent.left == nd {
			newRoot.parent.left = newRoot
		} else {
			newRoot.parent.right = newRoot
		}
	}
	nd.parent = newRoot

	newRoot.left = nd
	return newRoot
}

func (currentNode *node) insert(key int, priority int) *node {
	var prevNode *node
	for currentNode != nil {
		prevNode = currentNode
		if currentNode.key < key {
			currentNode = currentNode.right
		} else {
			currentNode = currentNode.left
		}
	}

	newNode := &node{key: key, priority: priority}
	newNode.parent = prevNode
	if prevNode.key < key {
		prevNode.right = newNode
	} else {
		prevNode.left = newNode
	}
	return newNode
}

func (nd *node) delete() {
	currentNode := nd
	prevNode := currentNode.parent
	if currentNode.left == nil && currentNode.right == nil {
		// no child exist
		if prevNode.left == currentNode {
			prevNode.left = nil
		} else {
			prevNode.right = nil
		}
	} else if currentNode.left != nil && currentNode.right != nil {
		// both child exist
		var child *node
		child = currentNode.right
		for child.left != nil {
			child = child.left
		}
		currentNode.key = child.key
		child.delete()
	} else {
		// the either of left or right child exist
		var child *node
		if currentNode.left != nil {
			child = currentNode.left
		} else {
			child = currentNode.right
		}
		if prevNode.left == currentNode {
			prevNode.left = child
			child.parent = prevNode
		} else {
			prevNode.right = child
			child.parent = prevNode
		}
	}
}

func visitPreorder(node *node, acc []int) []int {
	if node == nil {
		return acc
	}
	acc = append(acc, int(node.key))
	if node.left != nil {
		acc = visitPreorder(node.left, acc)
	}
	if node.right != nil {
		acc = visitPreorder(node.right, acc)
	}
	return acc
}

func visitInorder(node *node, acc []int) []int {
	if node == nil {
		return acc
	}
	if node.left != nil {
		acc = visitInorder(node.left, acc)
	}
	acc = append(acc, int(node.key))
	if node.right != nil {
		acc = visitInorder(node.right, acc)
	}
	return acc
}

// ------
// treap
// ------

func (tree *treap) insert(key int, priority int) {
	if tree.root == nil {
		tree.root = &node{key: key, priority: priority}
		return
	}

	inserted := tree.root.insert(key, priority)
	parent := inserted.parent
	for parent != nil && parent.priority < inserted.priority {
		if parent.left == inserted {
			inserted = parent.rightRotate()
		} else {
			inserted = parent.leftRotate()
		}
		parent = inserted.parent
	}
	// tree.print()
}

func (tree *treap) find(key int) bool {
	node := tree.root
	for node != nil {
		if node.key == key {
			return true
		} else if node.key < key {
			node = node.right
		} else {
			node = node.left
		}
	}
	return false
}

func (tree *treap) delete(key int) {
	var currentNode *node
	currentNode = tree.root
	for currentNode != nil && currentNode.key != key {
		if currentNode.key < key {
			currentNode = currentNode.right
		} else {
			currentNode = currentNode.left
		}
	}
	if currentNode == nil {
		return
	}

	for {
		if currentNode.left == nil && currentNode.right == nil {
			// no child
			break

		} else if currentNode.left != nil && currentNode.right != nil {
			// both left and right child exist
			if currentNode.left.priority < currentNode.right.priority {
				_ = currentNode.leftRotate()
			} else {
				_ = currentNode.rightRotate()
			}

		} else {
			// either of left or child exist
			if currentNode.left != nil {
				_ = currentNode.rightRotate()
			} else {
				_ = currentNode.leftRotate()
			}
			break
		}
	}

	currentNode.delete()
}

func (tree *treap) print() {
	var acc []int
	if tree.root.left == nil {
		fmt.Println("")
		fmt.Println("")
	} else {
		fmt.Println(" " + strings.Join(isToAs(visitInorder(tree.root.left, acc)), " "))
		fmt.Println(" " + strings.Join(isToAs(visitPreorder(tree.root.left, acc)), " "))
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	tree := newTreap()
	for i := 0; i < n; i++ {
		command := nextText(sc)
		switch command {
		case "insert":
			key, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			priority, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			tree.insert(key, priority)
		case "find":
			key, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			if tree.find(key) {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		case "delete":
			key, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			tree.delete(key)
		case "print":
			tree.print()
		default:
			panic("unknown command: " + command)
		}
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

func isToAs(is []int) []string {
	n := len(is)
	as := make([]string, n)
	for i := 0; i < n; i++ {
		as[i] = strconv.Itoa(is[i])
	}
	return as
}


Measured memory_bytes: 37024