#include<stdio.h>
#include<stdlib.h>
struct tree {
  int key;
  int priority;
  struct tree *left;
  struct tree *right;
};
struct tree *root = NULL;
int Search(struct tree *p, int key);
struct tree *Lrotation(struct tree *p);
struct tree *Rrotation(struct tree *p);
struct tree *Insert(struct tree **p, int key, int priority);
struct tree *Delete(struct tree **p, int key);
struct tree *delete(struct tree **p, int key);
void Dispm(struct tree *p);
void Dispp(struct tree *p);
int main(void)
{
  int i, count, key, priority;
  char command[7];
  scanf("%d", &count);
  for (i = 0; i < count; i++) {
    scanf("%*c%s", command);
    switch (command[0]) {
    case 'f':
      scanf("%d", &key);
      if (Search(root, key)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
      break;
    case 'i':
      scanf("%d %d", &key, &priority);
      root = Insert(&root, key, priority);
      break;
    case 'd':
      scanf("%d", &key);
      Delete(&root, key);
      break;
    case 'p':
      Dispm(root);
      putchar('\n');
      Dispp(root);
      putchar('\n');
      break;
    }
  }
  return 0;
}
int Search(struct tree *p, int key)
{
  if (p == NULL) {
    return 0;
  } else if (p->key == key) {
    return 1;
  } else if (p->key > key) {
    return Search(p->left, key);
  } else {
    return Search(p->right, key);
  }
}
struct tree *Lrotation(struct tree *p)
{
  struct tree *q = p->right;
  p->right = q->left;
  q->left = p;
  return q;
}
struct tree *Rrotation(struct tree *p)
{
  struct tree *q = p->left;
  p->left = q->right;
  q->right = p;
  return q;
}
struct tree *Insert(struct tree **p, int key, int priority)
{
  if (*p == NULL) {
    *p = (struct tree*)malloc(sizeof(struct tree));
    (*p)->key = key;
    (*p)->priority = priority;
    (*p)->left = NULL;
    (*p)->right = NULL;
  } else if ((*p)->key > key) {
    (*p)->left = Insert(&(*p)->left, key, priority);
    if ((*p)->priority <  (*p)->left->priority) *p = Rrotation(*p);
  } else if ((*p)->key < key) {
    (*p)->right = Insert(&(*p)->right, key, priority);
    if ((*p)->priority <  (*p)->right->priority) *p = Lrotation(*p);
  }
  return *p;
}
struct tree *Delete(struct tree **p, int key)
{
  if ((*p) == NULL) return NULL;
  if ((*p)->key > key) (*p)->left = Delete(&(*p)->left, key);
  else if ((*p)->key < key) (*p)->right = Delete(&(*p)->right, key);
  else delete(p, key);
  return *p;
}
struct tree *delete(struct tree **p, int key)
{
  if ((*p)->left == NULL && (*p)->right == NULL) return *p = NULL;
  else if ((*p)->left == NULL) *p = Lrotation(*p);
  else if ((*p)->right == NULL) *p = Rrotation(*p);
  else {
    if ((*p)->left->priority > (*p)->right->priority) *p = Rrotation(*p);
    else *p = Lrotation(*p);
  }
  return Delete(p, key);
}
void Dispm(struct tree *p)
{
  if (p != NULL) {
    Dispm(p->left);
    printf(" %d", p->key);
    Dispm(p->right);
  }
}
void Dispp(struct tree *p)
{
  if (p != NULL) {
    printf(" %d", p->key);
    Dispp(p->left);
    Dispp(p->right);
  }
}
