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

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

#define NIL NULL
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 treeSearch(Node u, int k){
  if(u==NIL||k==u->key)return u;
  if(k<u->key)
    return treeSearch(u->left,k);
  else
    return treeSearch(u->right,k);

}

Node treeSuccessor(Node x){
  Node y=x->parent;
  if(x->right!=NIL)
    return treeMinimum(x->right);
  while(y!= NIL&&x==y->right){
    x=y;
    y=y->parent;
  }
  return y;

}

void treeDelete(Node z){
  Node y; // node to be deleted
  Node x; // child of y

  if(z->left==NIL || z->right==NIL)y=z;
  else y=treeSuccessor(z);

  if(y->left!=NIL)x=y->left;
  else x=y->right;

  if(x!=NIL)x->parent=y->parent;

  if(y->parent==NIL)root=x;
  else if(y==y->parent->left)y->parent->left=x;
  else y->parent->right=x;

  if(y!=z)z->key=y->key;

}

Node insertA(int k,int priority){
  Node y = NIL;
  Node x = root;
  Node z;

  z = malloc(sizeof(struct node));
  z->key = k;
  z->priority=priority;
  z->left = NIL;
  z->right = NIL;

  while(x!=NIL){
    y=x;
    if(z->key<x->key)x=x->left;
    else x=x->right;
  }
  z->parent=y;
  if(y==NIL)root=z;
  else if(z->key<y->key)y->left=z;
  else y->right=z;
  return z;
}


Node rightRotate(Node t){
  Node s;
  if(root==t)root=t->left;
  s= t->left;
  t->left = s->right;
  s->right = t;
  return s; // root of the subtree
}
	

Node leftRotate(Node t){
  Node s;
  if(t==root)root=t->right;
  s= t->right;
  t->right = s->left;
  s->left = t;
  return s; // root of the subtree
}





void inorder(Node u){
  if(u==NIL)return;
  inorder(u->left);
  printf(" %d",u->key);
  inorder(u->right);
}
void preorder(Node u){
  if(u==NIL)return;
  printf(" %d",u->key);
  preorder(u->left);
  preorder(u->right);
}
Node insert(Node t, int key, int priority){            // 再帰的に探索
  if (t == NIL)
    return insertA(key,priority);               // 葉に到達したら新しい節点を生成して返す
  if (key == t->key)
    return t;                              // 重複したkeyは無視

  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;
}




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




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

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

  return 0;
}



Measured memory_bytes: 11112