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

struct Node{
  int key, priority;
  struct Node *right, *left;
};

struct Node *root, *NIL;

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

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

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 *insert(struct Node *t, int key, int priority){           // 再帰的に探索
  struct Node *z;
  if(t == NIL){
    z = malloc(sizeof(struct Node));
    z->key = key;
    z->priority = priority;
    z->left = NIL;
    z->right=NIL;
    return z;
  }

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

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

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

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 s[20];

  scanf("%d", &n);

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

