use std::fmt;
use std::io::stdin;
use std::io::BufRead;
use std::str;
use std::str::FromStr;
#[derive(Debug)]
pub struct Node {
	pub key:u32,
	pub priority:u64,
	pub left:Box<Option<Node>>,
	pub right:Box<Option<Node>>,
}
impl Node {
	pub fn new(key:u32,priority:u64) -> Node {
		Node {
			priority:priority,
			key:key,
			left:Box::new(None),
			right:Box::new(None),
		}
	}

	pub fn right_rotate(t:Node) -> Node {
		match *t.left {
			Some(left) => {
				Node {
					priority: left.priority,
					key: left.key,
					left: left.left,
					right: Box::new(Some(Node {
						key: t.key,
						priority: t.priority,
						left: left.right,
						right: t.right,
					}))
				}
			},
			None => t
		}
	}

	pub fn left_rotate(t:Node) -> Node {
		match *t.right {
			Some(right) => {
				Node {
					priority: right.priority,
					key: right.key,
					right: right.right,
					left: Box::new(Some(Node {
						key: t.key,
						priority: t.priority,
						right: right.left,
						left: t.left,
					}))
				}
			},
			None => t
		}
	}

	pub fn insert(t:Box<Option<Node>>, key:u32, priority:u64) -> Node {
		match *t {
			None => Node::new(key,priority),
			Some(t) => {
				if key == t.key {
					t
				} else if key < t.key {
					let left = Node::insert(t.left, key, priority);

					if t.priority < left.priority {
						Node::right_rotate(Node {
							key: t.key,
							priority: t.priority,
							left: Box::new(Some(left)),
							right: t.right
						})
					} else {
						Node {
							key: t.key,
							priority: t.priority,
							left: Box::new(Some(left)),
							right: t.right
						}
					}
				} else {
					let right = Node::insert(t.right, key, priority);

					if t.priority < right.priority {
						Node::left_rotate(Node {
							key: t.key,
							priority: t.priority,
							left: t.left,
							right: Box::new(Some(right))
						})
					} else {
						Node {
							key: t.key,
							priority: t.priority,
							left: t.left,
							right: Box::new(Some(right))
						}
					}
				}
			}
		}
	}

	fn delete_at(t:Box<Option<Node>>,key:u32) -> Option<Node> {
		Node::delete(Box::new(match *t {
			None => None,
			Some(t) => {
				if t.left.is_none() && t.right.is_none() {
					return None;
				} else if t.left.is_none() {
					Some(Node::left_rotate(t))
				} else if t.right.is_none() {
					Some(Node::right_rotate(t))
				} else {
					let lp = match *t.left {
						None => 0,
						Some(ref n) => n.priority,
					};

					let rp = match *t.right {
						None => 0,
						Some(ref n) => n.priority,
					};

					if lp > rp {
						Some(Node::right_rotate(t))
					} else {
						Some(Node::left_rotate(t))
					}
				}
			}
		}), key)
	}

	pub fn delete(t:Box<Option<Node>>,key:u32) -> Option<Node> {
		match *t {
			None => {
				return None;
			},
			Some(t) => {
				if key < t.key {
					Some(Node {
						priority:t.priority,
						key:t.key,
						left:Box::new(Node::delete(t.left,key)),
						right:t.right
					})
				} else if key > t.key {
					Some(Node {
						priority:t.priority,
						key:t.key,
						left:t.left,
						right:Box::new(Node::delete(t.right,key)),
					})
				} else {
					Node::delete_at(Box::new(Some(t)),key)
				}
			}
		}
	}

	pub fn find(root:&Node,key:u32) -> bool {
		let mut t = root;

		while key != t.key {
			if key < t.key {
				match *t.left {
					None => {
						return false;
					},
					Some(ref n) => {
						t = n;
					}
				}
			} else if key > t.key {
				match *t.right {
					None => {
						return false;
					},
					Some(ref n) => {
						t = n;
					}
				}
			}
		}

		true
	}

	pub fn print(root:&Node) {
		print!(" {}\n",Node::in_order(root).into_iter().map(|k| k.to_string()).collect::<Vec<String>>().join(" "));
		print!(" {}\n",Node::pre_order(root).into_iter().map(|k| k.to_string()).collect::<Vec<String>>().join(" "));
	}

	fn in_order(t:&Node) -> Vec<u32> {
		let mut keys:Vec<u32> = Vec::new();

		if let Some(ref l) = *t.left {
			keys.append(&mut Node::in_order(l));
		}

		keys.push(t.key);

		if let Some(ref r) = *t.right {
			keys.append(&mut Node::in_order(r));
		}

		keys
	}

	fn pre_order(t:&Node) -> Vec<u32> {
		let mut keys:Vec<u32> = Vec::new();

		keys.push(t.key);

		if let Some(ref l) = *t.left {
			keys.append(&mut Node::pre_order(l));
		}

		if let Some(ref r) = *t.right {
			keys.append(&mut Node::pre_order(r));
		}

		keys
	}
}
fn main() {
	let stdin = stdin();
	let mut reader = InputScanner::new(stdin.lock(),256);

	let m:usize = reader.next();
	let mut root:Box<Option<Node>> = Box::new(None);

	for _ in 0..m {
		let command:String = reader.next();

		match command.as_str() {
			"insert" => {
				root = Box::new(Some(Node::insert(root, reader.next(), reader.next())));
			},
			"find" => {
				if let Some(ref root) = *root {
					if Node::find(root,reader.next()) {
						print!("yes\n");
					} else {
						print!("no\n");
					}
				} else {
					panic!("invalid state.");
				}
			},
			"delete" => {
				root = Box::new(Node::delete(root,reader.next()));
			},
			"print" => {
				if let Some(ref root) = *root {
					Node::print(root);
				}
			},
			_ => {
				panic!("invalid command.");
			}
		}
	}
}
struct InputScanner<R: BufRead> {
	reader: R,
	buf: Vec<u8>, // Should never be empty
	pos: usize,   // Should never be out of bounds as long as the input ends with '\n'
}

impl<R: BufRead> InputScanner<R> {
	fn new(reader: R, capacity: usize) -> Self {
		InputScanner {
			reader: reader,
			buf: Vec::with_capacity(capacity),
			pos: 0,
		}
	}

	#[inline]
	fn next<T: FromStr>(&mut self) -> T
	where
		T::Err: fmt::Debug,
	{
		if self.buf.is_empty() {
			self._read_next_line();
		}
		let mut start = None;
		loop {
			match (self.buf[self.pos], start.is_some()) {
				(b' ', true) | (b'\n', true) | (b'\r', true) => break,
				(_, true) | (b' ', false) => self.pos += 1,
				(b'\n', false) | (b'\r', false) => self._read_next_line(),
				(_, false) => start = Some(self.pos),
			}
		}
		let target = &self.buf[start.unwrap()..self.pos];
		unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
	}

	#[inline]
	fn _read_next_line(&mut self) {
		self.pos = 0;
		self.buf.clear();
		if self.reader.read_until(b'\n', &mut self.buf).unwrap() == 0 {
			panic!("Reached EOF");
		}
	}
}



Measured memory_bytes: 29492