#include <stdio.h>
#include <stdlib.h>
#define NIL NULL
 
struct node{
    int key;
    int priority;
    struct node *right;
    struct node *left;
};
 
typedef struct node * Node;

Node delete(Node, int); 
Node root;

Node treeMinimum(Node x){
    while (x->left!=NIL)
    {
        x=x->left;
    }
    return x;
}
 
Node treeMaximum(Node x){
    while (x->right!=NIL)
    {
        x=x->right;
    }
    return x;
}
 
Node Search(Node u, int k){
    Node ret = NIL;
    if (u->key==k)
    {
        return u;
    }
    else if (u->key>k)
    {
        if (u->left!=NIL)
        {
            return Search(u->left, k);
        }
    }
    else if (u->right!=NIL)
    {
        return Search(u->right, k);
    }
    return ret;
}

Node rightRotate(Node t){
    Node s = t->left;
    if (t==root)
    {
        root=s;
    }
    t->left = s->right;
    s->right = t;
    return s;
}
 
Node leftRotate(Node t){
    Node s = t->right;
    if (t==root)
    {
        root=s;
    }
    t->right = s->left;
    s->left = t;
    return s;
}
 
Node _delete(Node t, int key){
    if (t->left == NIL && t->right==NIL)
    {
        return NIL;
    }
    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);
}

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 _delete(t, key);
    }
    return t;
}

Node insert(Node t, int key, int priority){
    if (t==NIL)
    {
        Node z = malloc(sizeof(struct node));
        z->key = key;
        z->priority = priority;
        z->left = NIL;
        z->right=NIL;
        if (root==NIL) root = z;
        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(Node u){
    if (u->left!=NIL)
    {  
        inorder(u->left);
    }
    printf(" %d", u->key);
    if (u->right!=NIL)
    {
        inorder(u->right);
    }
}
 
void preorder(Node u){
    printf(" %d", u->key);
    if (u->left!=NIL)
    {
        preorder(u->left);
    }
    if (u->right!=NIL)
    {
        preorder(u->right);
    }
}
 
void print(){
    if (root==NIL)
    {
        return;
    }
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
}
 
int main(){
    int n, i, x, y;
    Node t;
    char tmp[10];
    root = NIL;
    scanf("%d", &n);

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

    return 0;
}
