#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define N 200001
#define NI NULL

typedef struct node{
	int key;
	int pri;
	struct node *right , *left;
}Node;

Node * Delete(Node **,int);
Node * del(Node **,int);

int find(Node *p,int x)
{
	while(p!=NI)
	{
		if(p->key==x)return 1;
		else if(p->key > x)p = p->left;
		else p = p->right;
	}
	return 0;
}

void inorder(Node *a)
{
	if(a->left!=NI) inorder(a->left);
	printf(" %d",a->key);
	if(a->right!=NI) inorder(a->right);
	return;
}

void preorder(Node *p)
{
	printf(" %d",p->key);
	if(p->left!=NI) preorder(p->left);
	if(p->right!=NI) preorder(p->right);
	return;
}

void print(Node *p)
{
	inorder(p);
	printf("\n");
	preorder(p);
	printf("\n");
	return;
}

Node * makenode(int x , int y)
{
	Node *new = malloc(sizeof(Node));
	new->key = x;
	new->pri = y;
	new->left = NI;
	new->right = NI;
	return new;
}

Node *rightRotate(Node *p)
{
	Node *q = p->left;
	p->left = q->right;
	q->right = p;
	return q;
}

Node *leftRotate(Node *p)
{
	Node *q = p->right;
	p->right = q->left;
	q->left = p;
	return q;
}

Node* insert(Node **t , int key, int pri)
{
	Node *new;
	if((*t)==NI)
	return new = makenode(key,pri);
	if((*t)->key == key)
	return *t;

	if(key < (*t)->key)
	{
		(*t)->left = insert(&(*t)->left,key,pri);
		if((*t)->pri < (*t)->left->pri)
		(*t) = rightRotate(*t);
	}
	else{
		(*t)->right = insert(&(*t)->right,key,pri);
		if((*t)->pri < (*t)->right->pri)
		(*t) = leftRotate((*t));
	}
	return *t;
}

Node * Delete(Node **t , int key)
{
	if((*t)==NI){return NI;}
	if(key < (*t)->key){(*t)->left = Delete(&((*t)->left),key);}
	else if(key > (*t)->key){(*t)->right = Delete(&((*t)->right),key);}
	else{ return del(t,key);}
	return *t;
}

Node * del(Node **t , int key)
{
	if((*t)->left == NI && (*t)->right == NI){return NI;}
	else if((*t)->left == NI){(*t) = leftRotate(*t);}
	else if((*t)->right ==NI){(*t) = rightRotate(*t);}
	else{
		if((*t)->left->pri > (*t)->right->pri){(*t) = rightRotate(*t);}
		else{(*t) = leftRotate(*t);}
	}
	return Delete(t,key);
}

int main(void)
{
	int n,x,y;
	char c[8];
	Node *root = NI;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		scanf("%s",c);
		switch(c[0])
		{
			case 'i':scanf("%d %d",&x,&y);
			root = insert(&root,x,y);
			break;
			case 'd':scanf("%d",&x);
			root = Delete(&root,x);
			break;
			case 'f':scanf("%d",&x);
			if(find(root,x))printf("yes\n");
			else printf("no\n");
			break;
			case 'p':print(root);
			break;
		}
	}
	return 0;
}

