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

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


node * Delete(node **,int);
node * _delete(node **,int);

int find(node *t,int x){

	while(t!=NULL){
		if(t->key == x) return 1;
		else if(t->key > x) t=t->left;
		else t=t->right;
	}

	return 0;

}

void inorder(node *t){

	if(t->left!=NULL)inorder(t->left);

	printf(" %d",t->key);

	if(t->right!=NULL)inorder(t->right);

	return;

}

void preorder(node *t){

	printf(" %d",t->key);

	if(t->left!=NULL)preorder(t->left);

	if(t->right!=NULL)preorder(t->right);

	return;

}

void print(node *t){

	inorder(t);
	printf("\n");

	preorder(t);
	printf("\n");

	return;

}

node * Node(int x,int y){
	node *n=malloc(sizeof(node));
	n->key=x;
	n->priority=y;
	n->left=NULL;
	n->right=NULL;
	return n;
}

node *rightRotate(node *t){
	node *s=t->left;
	t->left=s->right;
	s->right=t;
	return s;
}

node *leftRotate(node *t){
	node *s=t->right;
	t->right=s->left;
	s->left=t;
	return s;
}


node* insert(node **t,int key,int priority){
	node *n;

	if((*t)==NULL)
		return n=Node(key,priority);
	if ((*t)->key==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;
}


node * Delete(node **t,int key){
	if((*t)==NULL)
		return NULL;

	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==NULL && (*t)->right==NULL)
		return NULL;
	else if((*t)->left==NULL)
		(*t)=leftRotate(*t);
	else if((*t)->right==NULL)
		(*t)=rightRotate(*t);
	else {
		if((*t)->left->priority > (*t)->right->priority)
			(*t)=rightRotate(*t);
		else
			(*t)=leftRotate(*t);
	}
	return Delete(t,key);
}


int main(){
	int n,x,y;
	char c[8];
	node *t=NULL;

	scanf("%d",&n);

	for(int i=0;i<n;i++){
		scanf("%s",c);

		switch(c[0]){

			case 'i':scanf("%d %d",&x,&y);
					 t=insert(&t,x,y);
					 break;

			case 'd':scanf("%d",&x);
					 t=Delete(&t,x);
					 break;


			case 'f':scanf("%d",&x);
					 if(find(t,x))printf("yes\n");
					 else printf("no\n");
					 break;

			case 'p':print(t);
					 break;

		}

	}

	return 0;

}

