class Node
  def initialize(key, priority)
    @key = key
    @priority = priority
    @parent = nil
    @left = nil
    @right = nil
  end
  attr_accessor :key, :priority, :parent, :left, :right
end

def right_rotate(node_t)
  node_s = node_t.left
  node_t.left = node_s.right
  node_s.right = node_t
  return node_s
end

def left_rotate(node_t)
  node_s = node_t.right
  node_t.right = node_s.left
  node_s.left = node_t
  return node_s
end

def insert(node_t, key, priority)
  if node_t == nil
    return Node.new(key,priority)
  end
  if key == node_t.key
    return node_t
  end

  if key < node_t.key
    node_t.left = insert(node_t.left, key, priority)
    if node_t.priority < node_t.left.priority
      node_t = right_rotate(node_t)
    end
  else
    node_t.right = insert(node_t.right, key, priority)
    if node_t.priority < node_t.right.priority
      node_t = left_rotate(node_t)
    end
  end
  return node_t
end

def find?(key)
  node_x = @root
  loop do
    if node_x == nil
      return false
    elsif node_x.key == key
      return node_x
    elsif key < node_x.key
      node_x = node_x.left
    else
      node_x = node_x.right
    end
  end
end

def delete(node_t, key)
  if node_t == nil
    return nil
  end
  if key < node_t.key
    node_t.left = delete(node_t.left, key)
  elsif key > node_t.key
    node_t.right = delete(node_t.right, key)
  else
    return _delete(node_t, key)
  end
  return node_t
end

def _delete(node_t, key)
  if node_t.left == nil && node_t.right ==nil
    return nil
  elsif node_t.left == nil
    node_t = left_rotate(node_t)
  elsif node_t.right == nil
    node_t = right_rotate(node_t)
  else
    if node_t.left.priority > node_t.right.priority
      node_t = right_rotate(node_t)
    else
      node_t = left_rotate(node_t)
    end
  end
  return delete(node_t, key)
end


def inorder_tree_walk(node)
  if node != nil
    inorder_tree_walk(node.left)
    print " "
    print node.key
    inorder_tree_walk(node.right)
  end
end

def preorder_tree_walk(node)
  if node != nil
    print " "
    print node.key
    preorder_tree_walk(node.left)
    preorder_tree_walk(node.right)
  end
end

def print_tree
  inorder_tree_walk(@root)
  puts
  preorder_tree_walk(@root)
  puts
end


@root = nil

m = gets.to_i
m.times do |t|
  input = gets.split(" ")
  command = input.shift
  input.map!{|x| x.to_i}
  if command == "insert"
    @root = insert(@root, input[0], input[1])
  elsif command == "find"
    if find?(input[0])
      puts "yes"
    else
      puts "no"
    end
  elsif command == "delete"
    @root = delete(@root, input[0])
  else
    print_tree
  end
end

