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

struct node{
    int key;
    int priority;
    struct node *right, *left, *parent;
};

struct node *root, *NIL;

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

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

}

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



struct node * find(struct node *u, int k){
    while(u != NIL && k != u->key){
        if(k < u->key){
            u=u->left;
        }
        else{
            u=u->right;
        }
    }

    return u;
}



struct node *  delete(struct node *t, int key){
    if(t==NIL){
       return NIL;
    }
    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;

}

struct node * _delete(struct node *t, int key){
    if(t->left==NIL && t->right==NIL){
        return NIL;
    }else 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 delete(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;
        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==NIL){
        return;
    }
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
}

void preorder(struct node *u){
    if(u==NIL){
        return;
    }
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
}

int main(void){
    int n, i, x, y;
    char com[10];
    scanf("%d", &n);

    for(int i=0; i<n; i++){
        scanf("%s", com);
        if(com[0]=='i'){
            scanf("%d %d", &x, &y);
            root=insert(root, x, y);
            x++;
        }else if(com[0]=='f'){
            scanf("%d", &y);
            struct node *t = find(root, y);
            if(t!=NIL){
                
                printf("yes\n");
            }
            else{
                printf("no\n");
            }
        }
         else if(com[0]=='d'){
            scanf("%d", &y);
            root=delete(root, y);
        }else if(com[0]=='p'){
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
       
    }
    return 0;
}
