using System;

namespace ConsoleApp1
{
    public class Node {
        public int Parent { get; set; } = -1;
        public Node(int value, int priority) { this.Value = value; this.Priority = priority; }
        public int Value { get; set; } = 0;
        public int Priority { get; set; } = 0;
        public Node Left { get; set; } = null;
        public Node Right { get; set; } = null;
    }

    class Program
    {
        public static Node root;
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] currentString = Console.ReadLine().Split(' ');

                if (currentString[0].Equals("insert"))
                { root = Insert(root, int.Parse(currentString[1]), int.Parse(currentString[2])); }
                else if (currentString[0].Equals("find"))
                { Console.WriteLine(Find(root, int.Parse(currentString[1]))); }
                else if (currentString[0].Equals("delete"))
                { root = Delete(root , int.Parse(currentString[1])); }
                else if (currentString[0].Equals("print"))
                { InOrder(root); Console.WriteLine(); PreOrder(root); Console.WriteLine(); }
                else { throw new ArgumentException("currentString must be insert, find, delete, print but is " + currentString); }

            }
        }

        private static String Find(Node node, int value)
        {
            if (node == null) { return "no"; }
            else if (node.Value == value) { return "yes"; }
            else if (value < node.Value) { return Find(node.Left, value); }
            else { return Find(node.Right, value); }
        }
        private static Node RightRotate(Node t)
        {
            Node s = t.Left;
            t.Left = s.Right;
            s.Right = t;
            return s;
        }
        private static Node LeftRotate(Node t)
        {
            Node s = t.Right;
            t.Right = s.Left;
            s.Left = t;
            return s;
        }
        private static Node Insert(Node t, int key, int priority)            // search the corresponding place recursively
        {
            if (t == null)
                return new Node(key, priority);               // create a new node when you reach a leaf
            if (key == t.Value)
                return t;                                 // ignore duplicated keys

            if (key < t.Value) {                               // move to the left child
                t.Left = Insert(t.Left, key, priority);  // update the pointer to the left child
                if (t.Priority < t.Left.Priority)       // rotate right if the left child has higher priority
                    t = RightRotate(t);
            }
            else {                      // move to the right child
                t.Right = Insert(t.Right, key, priority); // update the pointer to the right child
                if (t.Priority < t.Right.Priority)         // rotate left if the right child has higher priority
                    t = LeftRotate(t);
            }
            return t; 
        }

        private static Node Delete(Node t, int key) {                       // seach the target recursively
            if (t == null)
                return null;
            if (key < t.Value)                             // search the target recursively
                t.Left = Delete(t.Left, key);
            else if (key > t.Value)
                t.Right = Delete(t.Right, key);
            else
                return _delete(t, key);
            return t;
        }
        private static Node _delete(Node t, int key) {              // if t is the target node
            if (t.Left == null && t.Right == null)        // if t is a leaf
                return null;
            else if (t.Left == null)                      // if t has only the right child, then perform left rotate
                t = LeftRotate(t);
            else if (t.Right == null)                     // if t has only the left child, then perform right rotate
                t = RightRotate(t);
            else                                         // if t has both the left and right child
                if (t.Left.Priority > t.Right.Priority)  // pull up the child with higher priority
                    t = RightRotate(t);
                else
                    t = LeftRotate(t);
            return Delete(t, key);
        }

        private static void PostOrder(Node node) 
        {
            if (node.Left != null){ PostOrder(node.Left); }
            if (node.Right != null) { PostOrder(node.Right); }
            Console.Write(" " + node.Value);
        }
        private static void InOrder(Node node)
        {
            if (node.Left != null) { InOrder(node.Left); }
            Console.Write(" " + node.Value);
            if (node.Right != null) { InOrder(node.Right); }
        }
        private static void PreOrder(Node node)
        {
            Console.Write(" " + node.Value);
            if (node.Left != null) { PreOrder(node.Left); }
            if (node.Right != null) { PreOrder(node.Right); }
        }
    }
}



Measured memory_bytes: 29552