// AOJ ALDS1_8_D Binary search trees - Treap
// 2019.3.30 bal4u

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

//// 高速入出力
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()  // 非負整数の入力
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void ins(char *s)  // 文字列の入力　スペース以下の文字で入力終了
{
	do *s = gc();
	while (*s++ > ' ');
	*(s - 1) = 0;
}

void out(int n)  // 非負整数の表示（出力）
{
	int i;
	char ob[20];

	if (!n) pc('0');
	else {
		i = 0; while (n) ob[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(ob[i]);
	}
}

void outs(char *s) { while (*s) pc(*s++); }  // 文字列の出力


//// 本問題関連
#define MAX 300000

typedef struct { int key, pri, left, right; } BST;
BST bst[MAX]; int sz = 1;
int root;

void inorder(int node)
{
	if (node == 0) return;
	inorder(bst[node].left);
	pc(' '), out(bst[node].key);
	inorder(bst[node].right);
}

void preorder(int node)
{
	if (node == 0) return;
	pc(' '), out(bst[node].key);
	preorder(bst[node].left);
	preorder(bst[node].right);
}

int leftRotate(int node)
{
	int t = bst[node].right;
	bst[node].right = bst[t].left;
	bst[t].left = node;
	return t;
}

int rightRotate(int node)
{
	int t = bst[node].left;
	bst[node].left = bst[t].right;
	bst[t].right = node;
	return t;
}

int insert(int node, int key, int pri)
{
	int new;

	if (node == 0) {
		new = sz++;
		bst[new].key = key, bst[new].pri = pri;
		bst[new].left = bst[new].right = 0;
		return new;
	}
	if (key == bst[node].key) return node;
	if (key < bst[node].key) {
		bst[node].left = insert(bst[node].left, key, pri);
		if (bst[node].pri < bst[bst[node].left].pri) node = rightRotate(node);
	}
	else {
		bst[node].right = insert(bst[node].right, key, pri);
		if (bst[node].pri < bst[bst[node].right].pri) node = leftRotate(node);
	}
	return node;
}

int find(int node, int key)
{
	while (node && bst[node].key != key) {
		if (key < bst[node].key) node = bst[node].left;
		else node = bst[node].right;
	}
	return node;
}

int delete(int node, int key)
{
	if (node == 0) return 0;
	if (key < bst[node].key) bst[node].left = delete(bst[node].left, key);
	else if (key > bst[node].key) bst[node].right = delete(bst[node].right, key);
	else if (bst[node].left && bst[node].right) {
		if (bst[bst[node].left].pri > bst[bst[node].right].pri)
			node = rightRotate(node);
		else node = leftRotate(node);
		node = delete(node, key);
	}
	else {
		if (bst[node].left) {
			node = rightRotate(node);
			bst[node].right = delete(bst[node].right, key);
		}
		else if (bst[node].right) {
			node = leftRotate(node);
			bst[node].left = delete(bst[node].left, key);
		}
		else node = 0;
	}
	return node;
}

int main()
{
	int M, key, pri;
	char cmd[20];
	static char *msg[2] = { "no", "yes" };

	M = in();
	while (M--) {
		ins(cmd);
		if (*cmd == 'i') {
			key = in(), pri = in();
			root = insert(root, key, pri);
		}
		else if (*cmd == 'f') {
			key = in();
			outs(msg[find(root, key) > 0]), pc('\n');
		}
		else if (*cmd == 'd') {
			key = in();
			root = delete(root, key);
		}
		else if (*cmd == 'p') {
			inorder(root), pc('\n');
			preorder(root), pc('\n');
		}
	}
	return 0;
}


Measured memory_bytes: 4720