import java.io.PrintWriter

fun main(args: Array<String>) = with(PrintWriter(System.out)) {
    var root: Node? = null

    val n = readLine()!!.toInt()
    for (i in 1..n) {
        val cmds = readLine()!!.split(' ')
        when (cmds[0]) {
            "insert" -> {
                root = Treep.insert(root, cmds[1].toInt(), cmds[2].toInt())
            }
            "print" -> {
                Treep.printInorder(root, this); println()
                Treep.printPreorder(root, this); println()
            }
            "find" -> {
                println(if (Treep.find(root, cmds[1].toInt())) "yes" else "no" )
            }
            "delete" -> {
                root = Treep.delete(root, cmds[1].toInt())
            }
        }
    }


    flush()
}

object Treep {
    private fun rightRotate(t: Node): Node {
        val s = t.left!!
        t.left = s.right
        s.right = t
        return s
    }

    private fun leftRotate(t: Node): Node {
        val s = t.right!!
        t.right = s.left
        s.left = t
        return s
    }


    fun insert(t: Node?, key: Int, priority: Int): Node {
        t ?: return Node(key, priority)
        if (key == t.key) return t
        else if (key < t.key) {
            t.left = insert(t.left, key, priority)
            return if (t.priority < t.left!!.priority)
                rightRotate(t)
            else t
        } else {
            t.right = insert(t.right, key, priority)
            return if (t.priority < t.right!!.priority)
                leftRotate(t)
            else t
        }
    }

    fun delete(t: Node?, key: Int): Node? {
        t ?: return null
//        printDebug(t); println()
        if (key == t.key)
            return _delete(t, key)
        if (key < t.key)
            t.left  = delete(t.left,  key)
        else
            t.right = delete(t.right, key)
        return t
    }

    fun _delete(t: Node, key: Int): Node? {
        if (t.left == null && t.right == null)
            return null
        val s = if (t.left == null)
            leftRotate(t)
        else if (t.right == null)
            rightRotate(t)
        else if (t.left!!.priority > t.right!!.priority)
            rightRotate(t)
        else
            leftRotate(t)
        return delete(s, key)
    }

    fun find(t: Node?, key: Int): Boolean {
        t ?: return false
        if (t.key == key)
            return true
        return if (t.key < key)
            find(t.right, key)
        else
            find(t.left, key)
    }

    fun printInorder(t: Node?, writer: PrintWriter) {
        t ?: return
        printInorder(t.left, writer)
        writer.print(' ')
        writer.print(t.key)
        printInorder(t.right, writer)
    }

    fun printPreorder(t: Node?, writer: PrintWriter) {
        t ?: return
        writer.print(' ')
        writer.print(t.key)
        printPreorder(t.left, writer)
        printPreorder(t.right, writer)
    }

    fun printDebug(t: Node?) {
        t ?: return
        print('(')
        printDebug(t.left)
        print(t.key)
        printDebug(t.right)
        print(')')
    }
}

data class Node(
    val key: Int,
    val priority: Int,
    var left: Node? = null,
    var right: Node? = null
)
