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

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

typedef struct node* Nodepointer;
Nodepointer nil=NULL, root;

Nodepointer delete(Nodepointer*, int);
Nodepointer _delete(Nodepointer*, int);

Nodepointer Node(int key, int priority){
 Nodepointer z;
  z = (Nodepointer)malloc(sizeof(struct node));
  z->key = key;
  z->priority = priority;
  z->left = nil;
  z->right = nil;
  return z;
}

Nodepointer rightRotate(Nodepointer t){
  Nodepointer s = t->left;
  t->left = s->right;
  s->right = t;
  return s;
}

Nodepointer leftRotate(Nodepointer t){
  Nodepointer s = t->right;
  t->right = s->left;
  s->left = t;
  return s;
}

Nodepointer insert(Nodepointer *t, int key, int priority){
  if( (*t) == nil ) return Node(key, priority);
  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;
}


Nodepointer delete(Nodepointer *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;
}

Nodepointer _delete(Nodepointer *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);
}


Nodepointer find(Nodepointer u, int k){
  while( u != nil && k != u->key ){
    if( k < u->key ) u = u->left;
    else u = u->right;
  }
  return u;
}

void inorder(Nodepointer u){
  if( u->left != nil ) inorder(u->left);
  printf(" %d", u->key);
  if( u->right != nil ) inorder(u->right);
  return;
}

void preorder(Nodepointer u){
  printf(" %d", u->key);
  if( u->left != nil ) preorder(u->left);
  if( u->right != nil ) preorder(u->right);
  return;
}

void print(Nodepointer u){
  inorder(u);
  printf("\n");
  preorder(u);
  printf("\n");
  return;
}

int main(){
  int m, i, x, y;
  char com[8];
  Nodepointer t=nil;

  scanf("%d", &m);

  for( i = 0 ; i < m ; i++ ){
    scanf("%s",com);
    if( com[0] == 'f' ){
      scanf("%d", &x);
      t = find(root, x);
      if( t != nil ) printf("yes\n");
      else printf("no\n");
    }
    else if( strcmp("insert",com) == 0 ){
      scanf("%d%d", &x, &y);
      insert(&root,x,y);
    }
    else if( strcmp("print",com) == 0 ){
      print(root);
    }
    else if( strcmp("delete", com) == 0 ){
      scanf("%d", &x);
      root = delete(&root, x);
    }
  }
  return 0;
}

