package main

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

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

type Tree struct {
	root *Node
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	tree := &Tree{}
	for i := 0; i < n; i++ {
		command := nextText(sc)
		if command == "insert" {
			value, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			priority, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			insertTree(value, priority, tree)
		} else if command == "print" {
			printTree(tree)
		} else if command == "find" {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			if findTree(x, tree) != nil {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		} else if command == "delete" {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			deleteTree(x, tree)
		} else {
			panic(fmt.Errorf("unknown command: %s\n", command))
		}
	}
}

func insertTree(value int, priority int, tree *Tree) {
	tree.root = insertNode(value, priority, tree.root)
}

func insertNode(value int, priority int, node *Node) *Node {
	if node == nil {
		return &Node{value, priority, nil, nil}
	}
	if node.value < value {
		node.right = insertNode(value, priority, node.right)
		if node.priority < node.right.priority {
			node = leftRotate(node)
		}
	} else {
		node.left = insertNode(value, priority, node.left)
		if node.priority < node.left.priority {
			node = rightRotate(node)
		}
	}
	return node
}

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

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

func printTree(tree *Tree) {
	if tree.root == nil {
		fmt.Println("")
		fmt.Println("")
	} else {
		printResult(inorder(tree.root, []int{}))
		printResult(preorder(tree.root, []int{}))
	}
}

func findTree(x int, tree *Tree) *Node {
	return findNode(x, tree.root)
}

func findNode(x int, node *Node) *Node {
	for node != nil {
		if node.value == x {
			return node
		} else if node.value < x {
			node = node.right
		} else {
			node = node.left
		}
	}
	return nil
}

func deleteTree(x int, tree *Tree) {
	tree.root = deleteNode(x, tree.root)
}

func deleteNode(x int, node *Node) *Node {
	if node == nil {
		return nil
	}
	if x < node.value {
		node.left = deleteNode(x, node.left)
	} else if x > node.value {
		node.right = deleteNode(x, node.right)
	} else {
		return _deleteNode(x, node)
	}
	return node
}

func _deleteNode(x int, node *Node) *Node {
	if node.left != nil && node.right != nil {
		if node.left.priority < node.right.priority {
			node = leftRotate(node)
		} else {
			node = rightRotate(node)
		}
	} else if node.left != nil {
		node = rightRotate(node)
	} else if node.right != nil {
		node = leftRotate(node)
	} else {
		return nil
	}
	return deleteNode(x, node)
}

func preorder(top *Node, vs []int) []int {
	vs = append(vs, top.value)
	if top.left != nil {
		vs = preorder(top.left, vs)
	}
	if top.right != nil {
		vs = preorder(top.right, vs)
	}
	return vs
}

func inorder(top *Node, vs []int) []int {
	if top.left != nil {
		vs = inorder(top.left, vs)
	}
	vs = append(vs, top.value)
	if top.right != nil {
		vs = inorder(top.right, vs)
	}
	return vs
}

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())
}

func printResult(result []int) {
	writer := bufio.NewWriter(os.Stdout)
	for i := 0; i < len(result); i++ {
		writer.Write([]byte(fmt.Sprintf(" %d", result[i])))
	}
	writer.Write([]byte("\n"))
	writer.Flush()
}



Measured memory_bytes: 23892