using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lec8D
{

    class Node
    {
        public int key;
        public int pri;
        public Node left;
        public Node right;
    }

    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Node root = new Node();

            for (int i = 0; i < n; i++)
            {
                string input = Console.ReadLine();
                string[] split = input.Split(' ');

                if (split[0] == "insert")
                {
                    if ( i == 0) {
                        root.key = int.Parse(split[1]);
                        root.pri = int.Parse(split[2]);
                    }
                    else
                    {
                        root = Insert(root , int.Parse(split[1]) , int.Parse(split[2]));
                    }
                }
                else if (split[0] == "delete")
                {
                    root = Delete(root ,int.Parse(split[1]));
                }
                else if (split[0] == "find")
                {
                    bool flag = Sreach(root,int.Parse(split[1]));
                    if (flag == true)
                    {
                        Console.WriteLine("yes");
                    }
                    else if (flag == false)
                    {
                        Console.WriteLine("no");
                    }
                }

                else if (split[0] == "print")
                {
                    InorderPrint(root);
                    Console.WriteLine();
                    PreorderPrint(root);
                    Console.WriteLine();
                }

            }


            Console.ReadLine();

        }

        static bool Sreach(Node t , int key)
        {
            if (t == null)
            {
                return false;
            }
            else if (t.key == key)
            {
                return true;
            }
            else if (t.key > key)
            {
                return Sreach(t.left, key);
            }
            else if (t.key < key)
            {
                return Sreach(t.right, key);
            }

            return false;
        }

        static void InorderPrint(Node t)
        {
            if (t == null)
            {
                return;
            }
            InorderPrint(t.left);
            Console.Write(" {0}", t.key);
            InorderPrint(t.right);
        }

        static void PreorderPrint(Node t)
        {
            if (t == null)
            {
                return;
            }
            Console.Write(" {0}",t.key);
            PreorderPrint(t.left);
            PreorderPrint(t.right);
        }

        static  Node RightRotate(Node t)
        {
            Node s = t.left;
            t.left = s.right;
            s.right = t;
            return s;
        }

        static Node LeftRotate(Node t) {
            Node s = t.right;
            t.right = s.left;
            s.left = t;
            return s;
        }

        static Node Insert(Node t, int key, int priority) {      
            if (t == null) {
                Node r = new Node();
                r.key = key;
                r.pri = priority;
                return r;
            }
            if (key == t.key) {
                return t;                                
            }
            if (key < t.key) {                            
                t.left = Insert(t.left, key, priority);   
                if (t.pri < t.left.pri) {          
                    t = RightRotate(t);
                }
            } else {                                        
                t.right = Insert(t.right, key, priority); 
                if (t.pri < t.right.pri) {         
                    t = LeftRotate(t);
                }
            }
            return t;
        }

        static Node Delete(Node t, int key) {
            if (t == null) { 
                return null;
             }if (key < t.key)
            {                               
                t.left = Delete(t.left, key);
            }
            else if (key > t.key) { 
                t.right = Delete(t.right, key);
            }else {
                return _Delete(t, key);
            }
            return t;
        }

        static Node _Delete(Node t, int key) { 
            if (t.left == null && t.right == null) {           
                return null;
            } else if (t.left == null) {                       
                t = LeftRotate(t);
            } else if (t.right == null) {                       
                t = RightRotate(t);
            } else {                                        
                if (t.left.pri > t.right.pri)
                {    
                    t = RightRotate(t);
                }
                else
                {
                    t = LeftRotate(t);
                }
            }
        return Delete(t, key);
        }
    }
}


Measured memory_bytes: 30048