#include <stdio.h>
#include <stdlib.h>

struct node{
  struct node *right;
  struct node *left;
  int key;
  int priority;
};

typedef struct node * Node;

#define NIL NULL

Node root;

Node treeMinimum(Node x){
  while (x->left!=NIL) x=x->left;
  return x;
}

Node treeMaximum(Node x){
  while (x->right!=NIL) x=x->right;
  return x;
}

Node treeSearch(Node u, int k){
  Node ret = NIL;
  if (u->key==k) return u;
  else if (u->key>k) {
    if (u->left!=NIL)
      return treeSearch(u->left, k);
  } else if (u->right!=NIL)
    return treeSearch(u->right, k);
  return ret;
}

Node rightRotate(Node t)
{
  Node s = t->left;
  if (t==root) root=s;
  t->left = s->right;
  s->right = t;
  return s;
}

Node leftRotate(Node t)
{
  Node s = t->right;
  if (t==root) root=s;
  t->right = s->left;
  s->left = t;
  return s;
}

Node _delete(Node, int);

Node delete(Node t, int key)
{
  if (t==NIL)
    return NIL;
  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;
}

Node _delete(Node t, int key)
{
  if (t->left == NIL && t->right==NIL) // has no child
    return NIL;
  if (t->left == NIL)
    t=leftRotate(t);
  else if (t->right == NIL)
    t=rightRotate(t);
  else {
    if (t->left->priority > t->right->priority)
      t = rightRotate(t);
    else
      t = leftRotate(t);
  }
  return delete(t, key);
}

Node insert(Node t, int key, int priority)
{
  if (t==NIL) {
    Node z = malloc(sizeof(struct node));
    z->key = key;
    z->priority = priority;
    z->left = NIL;
    z->right=NIL;
    if (root==NIL) {
      root = z;
    }
    return z;
  }
  if (key==t->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;
}

void inorder(Node u){
  if (u->left!=NIL) inorder(u->left);
  printf(" %d", u->key);
  if (u->right!=NIL) inorder(u->right);
}
void preorder(Node u){
  printf(" %d", u->key);
  if (u->left!=NIL) preorder(u->left);
  if (u->right!=NIL) preorder(u->right);
}

void print()
{
  if (root==NIL) return;
  preorder(root);
  printf("\n");
  inorder(root);
  printf("\n");
}

int main(){
  int n, i, x, y;
  Node t;
  char com[20];
  root = NIL;
  scanf("%d", &n);
  
  for (i = 0; i < n; i++){
    scanf("%s", com);
    switch (com[0]) {
    case 'f':
      scanf("%d", &x);
      t = treeSearch(root, x);
      if (t != NIL) printf("yes\n");
      else printf("no\n");
      break;
    case 'i':
      scanf("%d %d", &x, &y);
      insert(root, x, y);

      break;
    case 'p':
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
      break;
    case 'd':
      scanf("%d", &x);
      t = treeSearch(root, x);
      if (t!=NIL)  delete(root, x);
    }
  }
  return 0;
}
