#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
  int key;
  int priority;
  struct node *parent;
  struct node *child[2];
} *nodePointer;
nodePointer init(int,int);
nodePointer insert(nodePointer,int,int);
nodePointer find(nodePointer,int);
nodePointer delete(nodePointer,int);
nodePointer _delete(nodePointer,int);
void preorderWalk(nodePointer);
void inorderWalk(nodePointer);
nodePointer rightRotate(nodePointer);
nodePointer leftRotate(nodePointer);
nodePointer root=NULL;
int main(void)
{
  int i;//counter
  int n;//the number of operation
  int key,priority;
  char op[10];//operation
  nodePointer p;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    {
      scanf("%s",op);
      if(strcmp(op,"insert")==0)
        {
          scanf("%d%d",&key,&priority);
          root=insert(root,key,priority);
        }
      else if(strcmp(op,"find")==0)
        {
          scanf("%d",&key);
          if(find(root,key)!=NULL) printf("yes\n");
          else printf("no\n");
        }
      else if(strcmp(op,"delete")==0)
        {
          scanf("%d",&key);
          root=delete(root,key);
        }
      else if(strcmp(op,"print")==0)
        {
          inorderWalk(root);
          printf("\n");
          preorderWalk(root);
          printf("\n");
        }
      else
        {
          fprintf(stderr,"undefined operation\n");
          exit(1);
        }
    }
  return 0;
}
nodePointer init(int key,int priority)
{
  nodePointer p=(nodePointer)malloc(sizeof(struct node));
  p->key=key;
  p->priority=priority;
  p->parent=NULL;
  p->child[0]=NULL;
  p->child[1]=NULL;
  return p;
}
nodePointer insert(nodePointer p,int key,int priority)
{
  if(p==NULL) return init(key,priority);
  if(key == p->key) return p;
  if(key < p->key)
    {
      p->child[0]=insert(p->child[0],key,priority);
      if(p->priority < p->child[0]->priority) p=rightRotate(p);
    }
  if(key > p->key)
    {
      p->child[1]=insert(p->child[1],key,priority);
      if(p->priority < p->child[1]->priority) p=leftRotate(p);
    }
  return p;
}
nodePointer find(nodePointer p,int key)
{
  if(p==NULL) return NULL;
  if(p->key < key) return find(p->child[1],key);
  if(p->key == key) return p;
  if(p->key > key) return find(p->child[0],key);
}
nodePointer delete(nodePointer p,int key)
{
  if(p==NULL) return NULL;
  if(key < p->key) p->child[0]=delete(p->child[0],key);
  else if(key > p->key) p->child[1]=delete(p->child[1],key);
  else return _delete(p,key);
  return p;
}
nodePointer _delete(nodePointer p,int key)
{
  if(p->child[0]==NULL&&p->child[1]==NULL) return NULL;
  else if(p->child[0]==NULL) p=leftRotate(p);
  else if(p->child[1]==NULL) p=rightRotate(p);
  else
    {
      if(p->child[0]->priority > p->child[1]->priority) p=rightRotate(p);
      else p=leftRotate(p);
    }
  return delete(p,key);
}
void preorderWalk(nodePointer p)
{
  if(p==NULL) return;
  printf(" %d",p->key);
  preorderWalk(p->child[0]);
  preorderWalk(p->child[1]);
}
void inorderWalk(nodePointer p)
{
  if(p==NULL) return;
  inorderWalk(p->child[0]);
  printf(" %d",p->key);
  inorderWalk(p->child[1]);
}
nodePointer rightRotate(nodePointer p)
{
  nodePointer s=p->child[0];
  p->child[0]=s->child[1];
  s->child[1]=p;
  return s;
}
nodePointer leftRotate(nodePointer p)
{
  nodePointer s=p->child[1];
  p->child[1]=s->child[0];
  s->child[0]=p;
  return s;
}


Measured memory_bytes: 11140