Node = Struct.new(:key, :priority, :left, :right)

class Tree
  def initialize
    @root = nil
  end
  
  def insert(key, priority)
    unless @root
      @root = Node.new(key, priority)
      return
    end
    insert = ->(node) {
      return Node.new(key, priority) unless node
      return node if key == node.key
      if key < node.key
        node.left  = insert.(node.left)
        node = right_rotate(node) if node.priority < node.left.priority
      else
        node.right = insert.(node.right)
        node = left_rotate(node)  if node.priority < node.right.priority
      end
      node
    }
    @root = insert.(@root)
  end
  
  def search(key)
    node = @root
    while node
      if key < node.key
        node = node.left
      elsif key > node.key
        node = node.right
      else
        return node
      end
    end
    nil
  end
  
  def delete(key)
    delete1 = nil
    
    delete = ->(node) {
      return nil unless node
      if key < node.key
        node.left  = delete.(node.left)
      elsif key > node.key
        node.right = delete.(node.right)
      else
        return delete1.(node)
      end
      node
    }
    delete1 = ->(node) {
      return nil if !node.left and !node.right
      node = if node.left.nil?
        left_rotate(node)
      elsif node.right.nil?
        right_rotate(node)
      else
        if node.left.priority > node.right.priority
          right_rotate(node)
        else
          left_rotate(node)
        end
      end
      delete.(node)
    }
    @root = delete.(@root)
  end
  
  def right_rotate(node)
    tmp = node.left
    node.left = tmp.right
    tmp.right = node
    tmp
  end
  
  def left_rotate(node)
    tmp = node.right
    node.right = tmp.left
    tmp.left = node
    tmp
  end
  
  def preorder
    traverse = ->(node) {
      if node
        yield(node.key)
        traverse.(node.left)
        traverse.(node.right)
      end
    }
    traverse.(@root)
  end
  
  def inorder
    traverse = ->(node) {
      if node
        traverse.(node.left)
        yield(node.key)
        traverse.(node.right)
      end
    }
    traverse.(@root)
  end
end

t = Tree.new

$<.gets.to_i.times do
  com, key, priority = $<.gets.split
  key = key.to_i
  priority = priority.to_i
  case com
  when "insert" then t.insert(key, priority)
  when "print"
    t.inorder  {|key| print " #{key}"}
    puts
    t.preorder {|key| print " #{key}"}
    puts
  when "find"
    puts t.search(key) ? "yes" : "no"
  when "delete" then t.delete(key)
  end
end
