using System;

namespace AOJ
{
  public class Program
  {
    class Node
    {
      public int Key;
      public int Priority;
      public Node Left;
      public Node Right;
      public Node(int key, int priority)
      {
        Key = key;
        Priority = priority;
        Left = Right = null;
      }
    }
    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) return new Node(key, priority);
      if (t.Key == key) return t;

      if (key < t.Key)
      {
        t.Left = Insert(t.Left, key, priority);
        if (t.Priority < t.Left.Priority)
        {
          t = RightRotate(t);
        }
      }
      else
      {
        t.Right = Insert(t.Right, key, priority);
        if (t.Priority < t.Right.Priority)
        {
          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.Priority > t.Right.Priority)
        {
          t = RightRotate(t);
        }
        else
        {
          t = LeftRotate(t);
        }
      }
      return Delete(t, key);
    }
    static bool Find(Node t, int key)
    {
      if (t == null) return false;
      if (t.Key == key) return true;

      if (key < t.Key)
      {
        return Find(t.Left, key);
      }
      else
      {
        return Find(t.Right, key);
      }
    }
    static void PrintInOrder(Node t)
    {
      if (t == null) return;
      PrintInOrder(t.Left);
      Console.Write(" " + t.Key);
      PrintInOrder(t.Right);
    }
    static void PrintPreOrder(Node t)
    {
      if (t == null) return;
      Console.Write(" " + t.Key);
      PrintPreOrder(t.Left);
      PrintPreOrder(t.Right);
    }
    public static void Main()
    {
      int n, i, key, priority;
      string[] order;
      Node top = null;

      n = int.Parse(Console.ReadLine());

      for (i = 0; i < n; i++)
      {
        order = Console.ReadLine().Split(' ');
        switch (order[0][0])
        {
          case 'f':
            key = int.Parse(order[1]);
            if (Find(top, key)) Console.WriteLine("yes");
            else Console.WriteLine("no");
            break;
          case 'i':
            key = int.Parse(order[1]);
            priority = int.Parse(order[2]);
            top = Insert(top, key, priority);
            break;
          case 'd':
            key = int.Parse(order[1]);
            top = Delete(top, key);
            break;
          case 'p':
            PrintInOrder(top);
            Console.WriteLine();
            PrintPreOrder(top);
            Console.WriteLine();
            break;
        }
      }
    }
  }
}


Measured memory_bytes: 29972