#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NIL NULL

struct Node{
	int key;
	int priority;
	struct Node *r,*l,*p;
}Node;

struct Node *Delete(struct Node **t, int key);
struct Node *_delete(struct Node **t, int key);

void preorder( struct Node *u ){
	
	printf(" %d",u->key);
	if(u->l!=NIL){
		preorder(u->l);
	}
	if(u->r!=NIL){
		preorder(u->r);
	}
	return;
}

void inorder( struct Node *u ){
	
	if(u->l!=NIL){
		inorder(u->l);
	}
	printf(" %d",u->key);
	if(u->r!=NIL){
		inorder(u->r);
	}
	return;
	
}

int find( struct Node *u , int k ){
	while( u != NIL ){
		if( u->key == k ){
			return 1;
		}else if( k < u->key ){
			u = u->l;
		}else{
			u = u->r;
		}
	}
	return 0;
}

struct Node *rightRotate(struct Node *t){
    struct Node *s = t->l;
    t->l = s->r;
    s->r = t;
    return s;
}

struct Node *leftRotate(struct Node *t){
    struct Node *s = t->r;
    t->r = s->l;
    s->l = t;
    return s;
}

struct Node* node( int x , int y ){
	
	struct Node *new = malloc(sizeof(Node));
	new->key = x;
	new->priority = y;
	new->l = NIL;
	new->r = NIL;
	
	return new;
}

struct Node* insert(struct Node **t, int key, int priority){
	
	struct Node *new;
	if( *t == NIL ){
        return new = node(key, priority);
	}
	if( key == (*t)->key ){
        return *t;
	}

	if( key < (*t)->key ){
		(*t)->l = insert(&(*t)->l, key, priority);
		if ( (*t)->priority < (*t)->l->priority ){
            *t = rightRotate(*t);
		}
	}else{
		(*t)->r = insert(&(*t)->r, key, priority);
		if( (*t)->priority < (*t)->r->priority ){
            *t = leftRotate(*t);
		}
	}
	return *t;
}

struct Node *Delete(struct Node **t, int key){
	
	if ((*t) == NIL){
		return NIL;
	}
	if ( key < (*t)->key ){
		(*t)->l = Delete(&((*t)->l), key);
	}else if ( key > (*t)->key ){
		(*t)->r = Delete(&((*t)->r), key);
	}else{
        return _delete(t, key);
	}
    return *t;
}

struct Node *_delete(struct Node **t, int key){                
		if ((*t)->l == NIL && (*t)->r == NIL ){               
        	return NIL;
		}else if ( (*t)->l == NIL ){
        	*t = leftRotate(*t);
		}else if ((*t)->r == NIL){
        	*t = rightRotate(*t);
		}else{
			if ((*t)->l->priority > (*t)->r->priority ){
            	*t = rightRotate(*t);
			}
			else{
            	*t = leftRotate(*t);
			}
		}
    return Delete(t, key);
}

int main( void ){
	int n , i , x , y;
	char com[10];
	struct Node *root = NIL;
	
	scanf( "%d", &n );
	
	for( i=0;i<n;i++ ){
		
		scanf("%s",com );
		if( strcmp(com ,"find") == 0 ) {
			scanf( "%d",&x );
			if( find(root,x) ){
				printf( "yes\n" );
			}else{
				printf("no\n");
			}
		}else if( strcmp(com ,"delete") == 0 ){
			scanf( "%d" , &x );
			root = Delete(&root,x);
		}else if( strcmp(com,"insert") == 0 ){
			scanf("%d %d",&x,&y);
			root = insert(&root,x,y);
		}else if( strcmp(com ,"print") == 0 ){
			inorder(root);
			printf( "\n" );
			preorder(root);
			printf( "\n" );
		}
	}
	
	return 0;
}

