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


typedef struct node{
	int key;
	int priority;
	struct node *left, *right;
}NODE;


NODE *ROOT;


NODE *Find(int key)
{
	NODE *p = ROOT;

	while(p != NULL){
		if(key == p->key)
			return p;

		if(key < p->key)
			p = p->left;
		else
			p = p->right;
	}

	return NULL;
}


NODE *RightRotate(NODE *t)
{
	NODE *s = t->left;
	t->left = s->right;
	s->right = t;

	return s;	// ROOT of the subtree
}


NODE *LeftRotate(NODE *t)
{
	NODE *s = t->right;
	t->right = s->left;
	s->left = t;

return s;	// ROOT of the subtree
}


NODE *Make_node(int key, int priority)
{
	NODE *p;

	if( (p = (NODE *)malloc(sizeof(NODE))) == NULL)
		return NULL;
	p->key = key;
	p->priority = priority;
	p->left = NULL;
	p->right = NULL;

	return p;
}


NODE *Insert(NODE *t, int key, int priority)	// 再帰的に探索
{
	// 葉に到達したら新しい節点を生成して返す
	if(t == NULL)
		return Make_node(key, priority);

	// 重複したkeyは無視
	if(key == t->key)
		return t;

	// 左の子へ移動
	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*, int);

NODE *Delete(NODE *t, int key)
{
	if(t == NULL)
		return NULL;
	if(key < t->key)                                // 削除対象を検索
		t->left = Delete(t->left, key);
	else if(t->key < key)
		t->right = Delete(t->right, key);
	else
		return _Delete(t, key);

	return t;
}

NODE *_Delete(NODE *t, int key) // 削除対象の節点の場合
{
	if(t->left == NULL && t->right == NULL){
	// 葉の場合
		free(t);
		return NULL;
	}else if(t->left == NULL)	// 右の子のみを持つ場合左回転
		t = LeftRotate(t);
	else if(t->right == NULL)	// 左の子のみを持つ場合右回転
		t = RightRotate(t);
	else{
	// 左の子と右の子を両方持つ場合
		if(t->left->priority > t->right->priority)	// 優先度が高い方を持ち上げる
			t = RightRotate(t);
		else
			t = LeftRotate(t);
	}

	return Delete(t, key);
}


void Walk_tree(NODE *p, int kind)
{
	if(p == NULL)
		return;

	if(kind == 0)
		printf(" %d", p->key);

	Walk_tree(p->left, kind);

	if(kind == 1)
		printf(" %d", p->key);

	Walk_tree(p->right, kind);
}


int main()
{
	int n;
	char command[10];
	int key, priority;
	int i;

	ROOT = NULL;

	scanf("%d", &n);

	for(i = 0; i < n; i++){
		scanf("%s", command);

		if(!strcmp(command, "find")){
			scanf("%d", &key);
			if(Find(key))
				printf("yes\n");
			else
				printf("no\n");
		}else if(!strcmp(command, "print")){
			Walk_tree(ROOT, 1);
			printf("\n");
			Walk_tree(ROOT, 0);
			printf("\n");
		}else if(!strcmp(command, "insert")){
			scanf("%d %d", &key, &priority);
			ROOT = Insert(ROOT, key, priority);
		}else if(!strcmp(command, "delete")){
			scanf("%d", &key);
			ROOT = Delete(ROOT, key);
		}
	}


	return 0;
}
