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

typedef struct P{
    int key,pri;
    struct P *left,*right,*parent;
}Node;

Node *root,*NIL;

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

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

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 pri){
    Node *z;
    if(t == NIL){
        z = (Node*)malloc(sizeof(Node));
        z->key=key; z->pri=pri; z->left = NIL; z->right=NIL;
        return z;
    }
    if( key < t->key){
        t->left = insert(t->left, key, pri);
        if(t->pri < t->left->pri){
            t = rightRotate(t);
        }
    }else{
        t->right = insert(t->right, key, pri);
        if(t->pri < t->right->pri){
            t = leftRotate(t);
        }
    }
    return t;
}

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

Node* Sdelete(Node*,int);

Node* delete(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 Sdelete(t, key);
    }
    return t;
}

Node* Sdelete(Node* t,int key){
    if(t->left == NIL && t->right == NIL){
        free(t);
        return NIL;
    }else if(t->left == NIL){
        t = leftRotate(t);
    }else if(t->right == NIL){
        t = rightRotate(t);
    }else{
        if(t->left->pri > t->right->pri){
            t = rightRotate(t);
        }else{
            t = leftRotate(t);
        }
    }
    return delete(t, key);
}

int main(void){
    int n,i,j,x,y;
    char str[7];
    Node *node;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",str);
        if(str[0]=='i'){
            scanf("%d %d",&x,&y);
            root=insert(root,x,y);
            x++;
        }else if(str[0]=='f'){
            scanf("%d",&y);
            node = find(root,y);
            if(node!=NIL){printf("yes\n");}
            else{printf("no\n");}
        }else if(str[0]=='d'){
            scanf("%d",&y);
            root=delete(root,y);
        }else{
            ino(root);
            printf("\n");
            pre(root);
            printf("\n");
        }
    }
    return 0;
}
