package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

// NewTreapNode is a constructor of TreapNode
func NewTreapNode(key int, priority int) *TreapNode {
	tp := TreapNode{}
	tp.key = key
	tp.priority = priority
	return &tp
}

// TreapNode contains integer
type TreapNode struct {
	key      int
	priority int
	left     *TreapNode
	right    *TreapNode
}

func (node *TreapNode) setLeftChild(key int, priority int) {
	child := NewTreapNode(key, priority)
	node.left = child
}

func (node *TreapNode) setRightChild(key int, priority int) {
	child := NewTreapNode(key, priority)
	node.right = child
}

// NewTreap is a constructor of Treap
func NewTreap() *Treap {
	treap := Treap{}
	return &treap
}

// Treap described at
//  https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_8_D
type Treap struct {
	root *TreapNode
}

func (t *Treap) insert(key int, priority int) {
	if t.root == nil {
		node := NewTreapNode(key, priority)
		t.root = node
	} else {
		t.root = t.insertLoop(t.root, key, priority)
	}
}

func (t *Treap) insertLoop(current *TreapNode, key int, priority int) *TreapNode {
	if current == nil {
		newNode := NewTreapNode(key, priority)
		return newNode
	}

	if key == current.key {
		return current
	}

	if key < current.key {
		current.left = t.insertLoop(current.left, key, priority)
		if current.priority < current.left.priority {
			current = t.rightRotate(current)
		}
	} else {
		current.right = t.insertLoop(current.right, key, priority)
		if current.priority < current.right.priority {
			current = t.leftRotate(current)
		}
	}

	return current
}

func (t *Treap) leftRotate(x *TreapNode) *TreapNode {
	y := x.right
	x.right = y.left
	y.left = x
	return y
}

func (t *Treap) rightRotate(y *TreapNode) *TreapNode {
	x := y.left
	y.left = x.right
	x.right = y
	return x
}

func (t *Treap) find(key int) *TreapNode {
	return t.findLoop(t.root, key)
}

func (t *Treap) findLoop(current *TreapNode, key int) *TreapNode {
	if current == nil {
		return nil
	}

	if key < current.key {
		return t.findLoop(current.left, key)
	} else if current.key == key {
		return current
	}
	return t.findLoop(current.right, key)
}

func (t *Treap) delete(key int) {
	t.root = t.deleteLoop(t.root, key)
}

func (t *Treap) deleteLoop(current *TreapNode, key int) *TreapNode {
	if current == nil {
		return nil
	}
	if key < current.key {
		current.left = t.deleteLoop(current.left, key)
	} else if key > current.key {
		current.right = t.deleteLoop(current.right, key)
	} else {
		return t.deleteNode(current, key)
	}
	return current
}

func (t *Treap) deleteNode(node *TreapNode, key int) *TreapNode {
	if node.left == nil && node.right == nil {
		return nil
	} else if node.left == nil {
		node = t.leftRotate(node)
	} else if node.right == nil {
		node = t.rightRotate(node)
	} else {
		if node.left.priority > node.right.priority {
			node = t.rightRotate(node)
		} else {
			node = t.leftRotate(node)
		}
	}
	return t.deleteLoop(node, key)
}

func (t *Treap) printInorderWalk() {
	fmt.Println(t.printInorderWalkRoop(t.root))
}

func (t *Treap) printInorderWalkRoop(mid *TreapNode) string {
	if mid == nil {
		return ""
	}

	return t.printInorderWalkRoop(mid.left) + " " + strconv.Itoa(mid.key) + t.printInorderWalkRoop(mid.right)
}

func (t *Treap) printPreorderWalk() {
	fmt.Println(t.printPreorderWalkRoop(t.root))
}

func (t *Treap) printPreorderWalkRoop(mid *TreapNode) string {
	if mid == nil {
		return ""
	}

	return " " + strconv.Itoa(mid.key) + t.printPreorderWalkRoop(mid.left) + t.printPreorderWalkRoop(mid.right)
}

func main() {
	scanner.Split(bufio.ScanWords)

	treap := NewTreap()

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		queryType := nextString()
		if queryType == "insert" {
			key := nextInt()
			priority := nextInt()
			treap.insert(key, priority)
		} else if queryType == "find" {
			key := nextInt()
			if treap.find(key) != nil {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		} else if queryType == "delete" {
			key := nextInt()
			treap.delete(key)
		} else if queryType == "print" {
			treap.printInorderWalk()
			treap.printPreorderWalk()
		}
	}
}



Measured memory_bytes: 27008