//2020.1.9
//s1270188 xxxmk2
//alds1_08d

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

//struct
struct node{
  int k, p;
  struct node *l, *r;
};
typedef struct node *Node;

//global
Node root=NULL;

//prototype
Node mk_Node(int, int);
Node r_rotate(Node);
Node l_rotate(Node);
Node insert(Node,int, int);
Node find(int,Node);
void pr_pre(Node);
void pr_in(Node);
void del_all(Node);
Node del(Node,int);
Node ddel(Node,int);

//main
int main(){
  int num, i, a, b;
  char com[10];
  
  scanf("%d",&num);
  for(i=0;i<num;i++){
    scanf("%s",com);

    if(com[0]=='i'){
      scanf("%d %d",&a,&b);
      insert(root,a,b);
    }

    else if(com[0]=='p') {
      pr_in(root);
      printf("\n");
      pr_pre(root);
      printf("\n");
    }

    else if(com[0]=='f'){
      scanf("%d",&a);
      if(find(a,root)==NULL) printf("no\n");
      else printf("yes\n");
    }

    else if(com[0]=='d'){
      scanf("%d",&a);
      del(root,a);
    }
  }

  
  del_all(root);
  return 0;
}


//function
Node mk_Node(int k, int p){
  Node d;
  d=malloc(sizeof(struct node));
  d->k=k;
  d->p=p;
  return d;
}

Node r_rotate(Node t){
  Node s=t->l;
  t->l=s->r;
  s->r=t;
  if(root==t) root=s;
  return s;
}

Node l_rotate(Node t){
  Node s=t->r;
  t->r=s->l;
  s->l=t;
  if(root==t) root=s;
  return s;
}

Node insert(Node t,int k, int p){
  if(t==NULL){
    t=mk_Node(k,p);
    if(root==NULL) root=t;
    return t;
  } 
  if(k==t->k) return t;

  if(k < t->k){
    t->l=insert(t->l,k,p);
    if(t->p < t->l->p) t=r_rotate(t);
  }
  else{
    t->r=insert(t->r,k,p);
    if(t->p < t->r->p) t=l_rotate(t);
  }

  return t;
}

Node find(int n,Node d){
  if(d->k==n) return d;
  if(d->k>n){
    if(d->l==NULL) return NULL;
    else return find(n,d->l);
  }
  else {
    if(d->r==NULL) return NULL;
    else return find(n,d->r);
  }
}

void pr_pre(Node data){
  printf(" %d",data->k);
  if(data->l!=NULL) pr_pre(data->l);
  if(data->r!=NULL) pr_pre(data->r);
}

void pr_in(Node data){
  if(data->l!=NULL) pr_in(data->l);
  printf(" %d",data->k);
  if(data->r!=NULL) pr_in(data->r);
}

void del_all(Node data){
  if(data->l!=NULL) del_all(data->l);
  if(data->r!=NULL) del_all(data->r);
  free(data);
}

Node del(Node t,int n){
  if(t==NULL) return NULL;
  if(n<t->k) t->l=del(t->l,n);
  else if(n>t->k) t->r=del(t->r,n);
  else return ddel(t,n);
  return t;
}

Node ddel(Node t,int n){
  if(t->l==NULL && t->r==NULL) return NULL;
  else if(t->l==NULL) t=l_rotate(t);
  else if(t->r==NULL) t=r_rotate(t);
  else {
    if(t->l->p > t->r->p) t=r_rotate(t);
    else t=l_rotate(t);
  }
  return del(t,n);
}


