package main

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

type Node struct {
	key      int
	priority int
	left     *Node
	right    *Node
}

func rightRotate(node *Node) *Node {
	newRoot := node.left
	node.left = newRoot.right
	newRoot.right = node
	return newRoot
}

func leftRotate(node *Node) *Node {
	newRoot := node.right
	node.right = newRoot.left
	newRoot.left = node
	return newRoot
}

func insert(key int, priority int, rootNode *Node) (*Node, error) {
	if rootNode == nil {
		return &Node{key, priority, nil, nil}, nil
	} else if rootNode.key == key {
		return nil, fmt.Errorf("Same key already exists: %d\n", key)
	} else if rootNode.key < key {
		if rootNode.right == nil {
			newNode := &Node{key, priority, nil, nil}
			rootNode.right = newNode
		} else {
			rootNode.right, _ = insert(key, priority, rootNode.right)
		}
		if rootNode.priority < rootNode.right.priority {
			rootNode = leftRotate(rootNode)
		}
	} else {
		if rootNode.left == nil {
			newNode := &Node{key, priority, nil, nil}
			rootNode.left = newNode
		} else {
			rootNode.left, _ = insert(key, priority, rootNode.left)
		}
		if rootNode.priority < rootNode.left.priority {
			rootNode = rightRotate(rootNode)
		}
	}
	return rootNode, nil
}

func delete(key int, rootNode *Node) (*Node, error) {
	if rootNode == nil {
		return nil, fmt.Errorf("not found: %d\n", key)
	} else if rootNode.key == key {
		return _delete(key, rootNode)
	} else if rootNode.key < key {
		rootNode.right, _ = delete(key, rootNode.right)
		return rootNode, nil
	} else {
		rootNode.left, _ = delete(key, rootNode.left)
		return rootNode, nil
	}
}

func _delete(key int, node *Node) (*Node, error) {
	if node.left == nil && node.right == nil {
		return nil, nil
	} else if node.left == nil {
		node = leftRotate(node)
	} else if node.right == nil {
		node = rightRotate(node)
	} else {
		if node.left.priority < node.right.priority {
			node = leftRotate(node)
		} else {
			node = rightRotate(node)
		}
	}
	return delete(key, node)
}

func find(key int, rootNode *Node) bool {
	if rootNode == nil {
		return false
	} else if rootNode.key == key {
		return true
	} else if rootNode.key < key {
		return find(key, rootNode.right)
	} else {
		return find(key, rootNode.left)
	}
}

func inorder(root *Node) []int {
	is := []int{}
	return _inorder(root, is)
}

func _inorder(root *Node, is []int) []int {
	if root == nil {
		return is
	}
	is = _inorder(root.left, is)
	is = append(is, root.key)
	is = _inorder(root.right, is)
	return is
}

func preorder(root *Node) []int {
	is := []int{}
	return _preorder(root, is)
}

func _preorder(root *Node, is []int) []int {
	if root == nil {
		return is
	}
	is = append(is, root.key)
	is = _preorder(root.left, is)
	is = _preorder(root.right, is)
	return is
}

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

func CmdInsert(key int, priority int, rootNode *Node) *Node {
	x, _ := insert(key, priority, rootNode)
	return x
}

func CmdDelete(key int, rootNode *Node) *Node {
	x, _ := delete(key, rootNode)
	return x
}

func CmdPrint(node *Node) {
	fmt.Println(strings.Join(isToAs(inorder(node)), ""))
	fmt.Println(strings.Join(isToAs(preorder(node)), ""))
}

func CmdFind(key int, rootNode *Node) {
	if find(key, rootNode) {
		fmt.Println("yes")
	} else {
		fmt.Println("no")
	}
}

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

	n, _ := nextInt(sc)
	var node *Node
	for i := 0; i < n; i++ {
		command := nextText(sc)
		switch command {
		case "insert":
			key, _ := nextInt(sc)
			priority, _ := nextInt(sc)
			node = CmdInsert(key, priority, node)
		case "delete":
			key, _ := nextInt(sc)
			node = CmdDelete(key, node)
		case "print":
			CmdPrint(node)
		case "find":
			key, _ := nextInt(sc)
			CmdFind(key, node)
		default:
			panic(fmt.Sprintf("unknown command: %s\n", command))
		}
	}
}

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

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



Measured memory_bytes: 36168