package main

/*
Treap
*/

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

// Node ノード
type Node struct {
	key         int
	pri         int
	left, right *Node
}

func (n *Node) String() string {
	//デバッグ用
	var left, right string
	if n.left == nil {
		left = "nil"
	} else {
		left = fmt.Sprintf("%d", n.left.key)
	}
	if n.right == nil {
		right = "nil"
	} else {
		right = fmt.Sprintf("%d", n.right.key)
	}
	return fmt.Sprintf("[key:%d pri:%d left:%s right:%s]", n.key, n.pri, left, right)
}

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

func printNode(t *Node) {
	if t != nil {
		fmt.Println(t)
		printNode(t.left)
		printNode(t.right)
	}
}

func insert(t *Node, key, pri int) *Node {
	if t == nil {
		return &Node{key: key, pri: pri, left: nil, right: nil}
	}
	if key == t.key {
		// 無視する
		return t
	}
	if key < t.key {
		// 左に移動
		t.left = insert(t.left, key, pri)
		if t.pri < t.left.pri {
			t = rightRotate(t)
		}
	} else {
		// 右に移動
		t.right = insert(t.right, key, pri)
		if t.pri < t.right.pri {
			t = leftRotate(t)
		}
	}
	return t
}
func delete(t *Node, key int) *Node {
	if t == nil {
		return nil
	}
	if key < t.key {
		// 左に移動
		t.left = delete(t.left, key)
		return t
	}
	if key > t.key {
		// 右に移動
		t.right = delete(t.right, key)
		return t
	}
	//  key == t.key
	if t.left == nil && t.right == nil {
		return nil
	}
	if t.right == nil {
		t = rightRotate(t)
	} else if t.left == nil {
		t = leftRotate(t)
	} else { // どちらも子がいる．
		if t.left.pri > t.right.pri {
			t = rightRotate(t)
		} else {
			t = leftRotate(t)
		}
	}
	return delete(t, key)
}
func findKey(t *Node, key int) bool {
	if t == nil {
		return false
	}
	if key < t.key {
		// 左に移動
		return findKey(t.left, key)
	}
	if key > t.key {
		// 右に移動
		return findKey(t.right, key)
	}
	return true
}

func inOrder(t *Node, result *bytes.Buffer) {
	if t == nil {
		return
	}
	inOrder(t.left, result)
	result.WriteString(fmt.Sprint(" ", t.key))
	inOrder(t.right, result)
	return
}
func preOrder(t *Node, result *bytes.Buffer) {
	if t == nil {
		return
	}
	result.WriteString(fmt.Sprint(" ", t.key))
	preOrder(t.left, result)
	preOrder(t.right, result)
	return
}
func printInOrder(t *Node) {
	var buf bytes.Buffer
	inOrder(t, &buf)

	fmt.Println(buf.String())
}
func printPreOrder(t *Node) {
	var buf bytes.Buffer
	preOrder(t, &buf)

	fmt.Println(buf.String())
}

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	var root *Node
	for i := 0; i < n; i++ {
		info := eGetFields(scanner)
		cmd := info[0]
		switch cmd {
		case "insert":
			k, p := eAtoi(info[1]), eAtoi(info[2])
			root = insert(root, k, p)
		case "print":
			printInOrder(root)
			printPreOrder(root)
		case "delete":
			k := eAtoi(info[1])
			root = delete(root, k)
		case "find":
			k := eAtoi(info[1])
			if findKey(root, k) {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}



Measured memory_bytes: 20444