#include<stdio.h>
#include<stdlib.h>
 
typedef struct node{
    int key;
    int priority;
    struct node *left;
    struct node *right;
}node;
 
int find(node *p,int x);
node * Delete(node **,int);
node * _delete(node **,int);
void inorder(node *p);
void print(node *p);
node * makenode(int x,int y);
node *rights(node *p);
node *lefts(node *p);
node* insert(node **t,int key,int priority);

int main()
{
  int i,n,p,q;
    char A[8];
    node *M=NULL;
     
    scanf("%d",&n);
     
    for(i=0;i<n;i++){
        scanf("%s",A);
         
        switch(A[0]){
         
            case 'i':scanf("%d %d",&p,&q);
                     M=insert(&M,p,q);
                     break;
             
            case 'd':scanf("%d",&p);
                     M=Delete(&M,p);
                     break;
             
             
            case 'f':scanf("%d",&p);
                     if(find(M,p))printf("yes\n");
                     else printf("no\n");
                     break;
 
            case 'p':print(M);
                     break;
                      
        }
    }
     
    return 0;
     
}
int find(node *p,int x)
{
 
    while(p!=NULL){
        if(p->key == x)return 1;
        else if(p->key > x)p=p->left;
        else p=p->right;
    }
     
    return 0;
     
}

node* insert(node **t,int key,int priority)
{
  
    node *new;
     
    if((*t)==NULL)
        return new=makenode(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)=rights(*t);
    }
    
    else {
        (*t)->right=insert(&(*t)->right,key,priority);
        if((*t)->priority < (*t)->right->priority)
            (*t)=lefts(*t);
    }
     
    return *t;
}

void preorder(node *p)
{    
    printf(" %d",p->key);
    
    if(p->left!=NULL)preorder(p->left);
    
    if(p->right!=NULL)preorder(p->right);     
    return;
     
}
 
void print(node *p)
{
    inorder(p);
    printf("\n");

    preorder(p);
    printf("\n");
     
    return;
     
}
 
node * makenode(int x,int y)
{ 
  node *new=malloc(sizeof(node));

    new->key=x;
    new->priority=y;
    new->left=NULL;
    new->right=NULL;
    return new;
}
 
node *rights(node *p)
{ 
    node *q=p->left;
    p->left=q->right;
    q->right=p;
    return q;
}
 
node *lefts(node *p)
{  
    node *q=p->right;
    p->right=q->left;
    q->left=p;
    return q;
}
 
 

node * _delete(node **a,int key)
{
    if((*a)->left==NULL && (*a)->right==NULL)
        return NULL;
    
    else if((*a)->left==NULL)
        (*a)=lefts(*a);
    
    else if((*a)->right==NULL)
        (*a)=rights(*a);
    
    else {
        if((*a)->left->priority > (*a)->right->priority)
            (*a)=rights(*a);
        else
            (*a)=lefts(*a);
    }
    
    return Delete(a,key);
}
 
 
node * Delete(node **a,int key)
{
    if((*a)==NULL)
        return NULL;
         
    if(key < (*a)->key)
        (*a)->left=Delete(&((*a)->left),key);
         
    else if(key > (*a)->key)
        (*a)->right=Delete(&((*a)->right),key);
         
    else return _delete(a,key);
     
    return *a;
}
 
void inorder(node *p)
{
 
    if(p->left!=NULL)inorder(p->left);     
    printf(" %d",p->key);
    
    if(p->right!=NULL)inorder(p->right);     
    return;
     
}



Measured memory_bytes: 8004