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

#define BUF_LENGTH 100
#define OPE_LENGTH 10

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

node * left_rotate(node * t) {
	node * new_t = t->right;
	t->right = new_t->left;
	new_t->left = t;
	new_t->parent = t->parent;
	t->parent = new_t;

	if (new_t->parent == NULL) {
		return new_t;
	}

	node * parent = new_t->parent;
	if (parent->left == t) {
		parent->left = new_t;
	}
	else {
		parent->right = new_t;
	}
	return new_t;
}

node * right_rotate(node * t) {
	node * new_t = t->left;
	t->left = new_t->right;
	new_t->right = t;
	new_t->parent = t->parent;
	t->parent = new_t;

	if (new_t->parent == NULL) {
		return new_t;
	}

	node * parent = new_t->parent;
	if (parent->left == t) {
		parent->left = new_t;
	}
	else {
		parent->right = new_t;
	}
	return new_t;
}

node * create_node(int key, int priority) {
	node * p;
	p = (node *)malloc(sizeof(node));
	p->key = key;
	p->priority = priority;
	p->left = NULL;
	p->right = NULL;
	p->parent = NULL;
	return p;
}

node * insert(node * t, int key, int priority) {
	if (t == NULL) {
		node * p = create_node(key, priority);
		return p;
	}

	if (key < t->key) {
		if (t->left == NULL) {
			node * p = create_node(key, priority);
			t->left = p;
			p->parent = t;
		}
		else {
			insert(t->left, key, priority);
		}
				
		if (t->priority < t->left->priority) {
			t = right_rotate(t);
		}
		return t;
	}
	else if (key > t->key) {
		if (t->right == NULL) {
			node * p = create_node(key, priority);
			t->right = p;
			p->parent = t;
		}
		else {
			insert(t->right, key, priority);
		}

		if (t->priority < t->right->priority) {
			t = left_rotate(t);
		}
		return t;
	}
	
	return t;
}

node * delete(node * t, int key) {
	if (key == t->key) {
		if (t->left == NULL && t->right == NULL) {
			if (t->parent->left == t) {
				t->parent->left = NULL;
			}
			else {
				t->parent->right = NULL;
			}
			free(t);
			return NULL;
		}

		if (t->left == NULL) {
			t = left_rotate(t);	
		}
		else if (t->right == NULL) {
			t = right_rotate(t);
		}
		else if (t->left->priority > t->right->priority) {
			t = right_rotate(t);
		}
		else {
			t = left_rotate(t);
		}
		t = delete(t, key);
		return t;
	}

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

int find(node * t, int key) {
	if (key == t->key) {
		return 1;
	}
	if (key < t->key) {
		if (t->left == NULL) {
			return 0;
		}
		return find(t->left, key);
	}
	else {
		if (t->right == NULL) {
			return 0;
		}
		return find(t->right, key);
	}
}

void print_preorder(node * t) {
	if (t == NULL) {
		return;
	}
	printf(" %d", t->key);
	if (t->left != NULL) {
		print_preorder(t->left);
	}
	if (t->right != NULL) {
		print_preorder(t->right);
	}
}

void print_inorder(node * t) {
	if (t == NULL) {
		return;
	}
	if (t->left != NULL) {
		print_inorder(t->left);
	}
	printf(" %d", t->key);
	if (t->right != NULL) {
		print_inorder(t->right);
	}
}

int main() {
	int m;
	char buf[BUF_LENGTH];
	char * str;
	int k, p;
	node * root = NULL;

	scanf_s("%d ", &m);

	for (int i = 0; i < m; i++) {
		fgets(&buf, BUF_LENGTH, stdin);
		char * context;
		str = strtok_s(buf, " \n", &context);
		if (strcmp(str, "insert") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			str = strtok_s(NULL, " \n", &context);
			p = atoi(str);
			//printf("--insert %d %d\n", k, p);
			root = insert(root, k, p);
		}
		else if (strcmp(str, "find") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			//printf("--find %d\n", k);
			int result = find(root, k);
			if (result == 1) {
				printf("yes\n");
			}
			else {
				printf("no\n");
			}
		}
		else if (strcmp(str, "delete") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			//printf("--delete %d\n", k);
			root = delete(root, k);
		}
		else if (strcmp(str, "print") == 0) {
			//printf("--print\n");
			print_inorder(root);
			printf("\n");
			print_preorder(root);
			printf("\n");
		}
		else {
			//printf("error\n");
		}
	}
	
	return 0;
}
