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

typedef struct _node {
  unsigned int key;
  unsigned int priority;
  struct _node* left;
  struct _node* right;
} Node;

typedef Node* NodePointer;

NodePointer insert (NodePointer, unsigned int, unsigned int);
bool find (NodePointer, unsigned int);
NodePointer delete (NodePointer, unsigned int);
void print (NodePointer);

NodePointer makeNode (unsigned int, unsigned int);
NodePointer leftRotate (NodePointer);
NodePointer rightRotate (NodePointer);
void traverse (NodePointer, char);
NodePointer deleteNode (NodePointer, unsigned int);

int main () {
  Node* treap = NULL;
  unsigned int key, priority;
  
  int n;
  char command[10];
  
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", command);
    switch(command[0]) {
    case 'i':
      scanf("%d %d", &key, &priority);
      treap = insert(treap, key, priority);
      break;
    case 'f':
      scanf("%d", &key);
      printf("%s\n", find(treap, key) ? "yes" : "no");
      break;
    case 'd':
      scanf("%d", &key);
      treap = delete(treap, key);
      break;
    case 'p':
      print(treap);
      break;
    }
  }
  
  return 0;
}

NodePointer insert(NodePointer node, unsigned int key, unsigned int priority) {
  if (node == NULL) {
    return makeNode(key, priority);
  }
  
  if (node->key == key) {
    return node;
  }

  if (node->key > key) {
    node->left = insert(node->left, key, priority);
    if (node->priority < node->left->priority) {
      node = rightRotate(node);
    }
  } else {
    node->right = insert(node->right, key, priority);
    if (node->priority < node->right->priority) {
      node = leftRotate(node);
    }
  }

  return node;
}

NodePointer makeNode(unsigned int key, unsigned int priority) {
  NodePointer node;

  node = (NodePointer)malloc(sizeof(Node));
  node->key = key;
  node->priority = priority;
  node->left  = NULL;
  node->right = NULL;

  return node;
}

NodePointer leftRotate (NodePointer node) {
  NodePointer child = node->right;
  node->right = child->left;
  child->left = node;
  return child; // root of the subtree
}

NodePointer rightRotate (NodePointer node) {
  NodePointer child = node->left;
  node->left = child->right;
  child->right = node;
  return child; // root of the subtree
}

bool find(NodePointer node, unsigned int key) {
  while (node != NULL) {
    if (node->key == key) {
      return true;
    } else if (node->key > key) {
      node = node->left;
    } else {
      node = node->right;
    }
  }
  return false;
}

NodePointer delete(NodePointer node, unsigned int key) {
  if (node == NULL) {
    return NULL;
  }

  if (key < node->key) {
    node->left  = delete(node->left , key);
  } else if (key > node->key) {
    node->right = delete(node->right, key);
  } else {
    return deleteNode(node, key);
  }

  return node;
}

NodePointer deleteNode (NodePointer node, unsigned int key) {
  if (node->left == NULL && node->right == NULL) {
    free(node);
    return NULL;
  } else if (node->left  == NULL) {
    node = leftRotate(node);
  } else if (node->right == NULL) {
    node = rightRotate(node);
  } else {
    if (node->left->priority > node->right->priority) {
      node = rightRotate(node);
    } else {
      node = leftRotate(node);
    }
  }

  return delete(node, key);
}

void print(NodePointer root) {
  traverse(root, 'i');
  printf("\n");
  traverse(root, 'p');
  printf("\n");
}

void traverse(NodePointer node, char type) {
  if (node == NULL) {
    return;
  }

  if (type == 'p') printf(" %d", node->key);
  traverse(node->left , type);
  if (type == 'i') printf(" %d", node->key);
  traverse(node->right, type);
}

