#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 insert( int k ){
	struct Node *y = NIL;
	struct Node *x = root;
	struct Node *z;
	
	z = ( struct Node *)malloc( sizeof(struct Node) );
	z->key = k;
	z->l = NIL;
	z->r = NIL;
	
	while( x != NIL ){
		y = x;
		if( z->key < x->key ){
			x = x->l;
		}else{
			x = x->r;
		}
	}
	z->p = y;
	
	if( y == NIL ){
		root = z;
	}else{
		if( z->key < y->key ){
			y->l = z;
		}else{
			y->r = z;
		}
	}
}
*/

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 * treeMinimum( struct Node *x ){
	while( x->l != NIL ){
		x = x-> l;
	}
	return x;
}

struct Node * treeSuccessor( struct Node *x ){
	if( x->r != NIL ){
		return treeMinimum( x->r );
	}
	struct Node *y = x->p;
	
	while( y != NIL && x == y->r ){
		x = y;
		y = y->p;
	}
	return y;
}

void treeDelete(struct Node *z ){
	struct Node *y;
	struct Node *x;
	
	if( z->l == NIL || z->r == NIL ){
		y = z;
	}else{
		y = treeSuccessor(z);
	}
	
	if( y->l != NIL ){
		x = y->l;
	}else{
		x = y->r;
	}
	
	if( x != NIL ){
		x->p = y->p;
	}
	
	if( y->p == NIL ){
		root = x;
	}else{
		if( y == y->p->l ){
			y->p->l = x;
		}else{
			y->p->r = x;
		}
	}
	
	if( y != z ){
		z->key = y->key;
	}
	
	free(y);
}
*/

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;                                             // 重複したkeyは無視
	}

	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 ,"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" );
		}else if( strcmp(com ,"delete") == 0 ){
			scanf( "%d" , &x );
			root = Delete(&root,x);
		}
	}
	
	return 0;
}
		


Measured memory_bytes: 11108