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

struct Node{
	int key,priority;
	struct Node *right, *left;
};

struct Node *root,*nil;

struct Node *treeMinimum(struct Node *x){
	while(x->left!=nil){
		x=x->left;
	}
	return x;
}

struct Node *treeMaximum(struct Node *x){
	while(x->right!=nil){
		x=x->right;
	}
	return x;
}

struct Node *find(struct Node *u,int k){
	struct Node *ret=nil;
	if(u->key==k){
		return u;
	}
	else if(u->key>k){
		if(u->left!=nil){
			return  find(u->left,k);
		}
	}
	else if(u->right!=nil){
		return  find(u->right,k);
	}
	return ret;
}

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

struct Node *leftRotate(struct Node *t)
{
  struct Node *s=t->right;
  if (t==root){
  root=s;
  }
  t->right = s->left;
  s->left = t;
  return s;
}
struct Node *treeDelete2(struct Node *t, int key);

struct Node *treeDelete(struct Node *t,int key){
if (t==nil){
    return nil;
    }
  if (key <t->key){
    t->left=treeDelete(t->left,key);
    }
  else if (key > t->key){
    t->right=treeDelete(t->right, key);
    }
  else{
    return treeDelete2(t, key);
    }
  return t;
}

struct Node *treeDelete2(struct Node *t, int key)
{
  if (t->left==nil && t->right==nil){
    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 treeDelete(t, key);
}
struct Node *insert(struct Node *t,int key,int priority){
	if (t==nil) {
    struct Node *z;
    z=(struct Node*)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(struct Node *u){
 if (u->left!=nil){
 inorder(u->left);
 }
  printf(" %d", u->key);
  if (u->right!=nil){
  inorder(u->right);
  }
}

void preorder(struct 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(void){
	int n,i,y;
	scanf("%d",&n);
	int d;
	struct Node *t;
	root=nil;
	char string[10];
	
	for(i=0;i<n;i++){
		scanf("%s %d",string,&d);
		if(string[0]=='i'){
			scanf(" %d",&y);
			insert(root,d,y);
		}
		else if(string[0]=='p'){
			inorder(root);
			printf("\n");
			preorder(root);
			printf("\n");
		}
		else if(string[0]=='f'){
			t=find(root,d);
			if(t!=nil){
				printf("yes\n");
			} 
			else{
				printf("no\n");
			}
		}
		else if(string[0]=='d'){
			t=find(root,d);
			if(t!=nil){
				treeDelete(root,d);
			}
		}
	}
	
	return 0;
}
