using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

namespace CsharpSpiral {
    partial class Program {
        static void Main(string[] args) {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            new Program().Solve(new ConsoleInput(Console.In, ' '));
            Console.Out.Flush();
        }
        public void Solve(ConsoleInput cin) {
            var m = cin.ReadInt;
            var G = new Treap<int, int>();
            for(int i = 0; i < m; i++) {
                var s = cin.Read;
                int k, p;
                switch(s) {
                    case "insert":
                        k = cin.ReadInt;
                        p = cin.ReadInt;
                        G.Insert(k, p);
                        break;
                    case "find":
                        k = cin.ReadInt;
                        Console.WriteLine(G.Find(k) ? "yes" : "no");
                        break;
                    case "delete":
                        k=cin.ReadInt;
                        G.Delete(k);
                        break;
                    case "print":
                        G.MidEmit();
                        G.FrontEmit();
                        break;
                }
            }
        }
        
    }

    static class ExtendClass {
        public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
    }

    class ConsoleInput {
        private readonly TextReader _stream;
        private readonly char _separator = ' ';
        private readonly Queue<string> inputStream;
        public ConsoleInput(TextReader stream, char separator = ' ') {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read {
            get {
                if(inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for(int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }
        public string[] ReadStrArray(long N) {
            var ret = new string[N];
            for(long i = 0; i < N; ++i)
                ret[i] = Read;
            return ret;
        }
        public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
        public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
    }

	class Treap<V, P> where P : IComparable<P> where V : IComparable<V> {
		public class PriorityNode {
			internal V value { get; set; }
			internal P priority { get; set; }
			internal PriorityNode Left { get; set; }
			internal PriorityNode Right { get; set; }
			public PriorityNode RightRotate() {
				var leftNode = Left;
				Left = leftNode.Right;
				leftNode.Right = this;
				return leftNode;
			}
			public PriorityNode LeftRotate() {
				var rightNode = Right;
				Right = rightNode.Left;
				rightNode.Left = this;
				return rightNode;
			}
		}
		public PriorityNode top { get; set; } = null;
		private static PriorityNode InsertFrom(PriorityNode now, V newValue, P newPriority) {
			if(now == null) return new PriorityNode { value = newValue, priority = newPriority };
			switch(newValue.CompareTo(now.value)) {
				case -1:
					now.Left = InsertFrom(now.Left, newValue, newPriority);
					if(now.priority.CompareTo(now.Left.priority) < 0)
						now = now.RightRotate();
					break;
				case 1:
					now.Right = InsertFrom(now.Right, newValue, newPriority);
					if(now.priority.CompareTo(now.Right.priority) < 0)
						now = now.LeftRotate();
					break;
			}
			return now;
		}
		private static PriorityNode DeleteSearch(PriorityNode now, V key) {
			if(now == null)
				return null;
			switch(key.CompareTo(now.value)) {
				case -1:
					now.Left = DeleteSearch(now.Left, key);
					break;
				case 1:
					now.Right = DeleteSearch(now.Right, key);
					break;
				default:
					return DeleteLift(now, key);
			}
			return now;
		}
		private static PriorityNode DeleteLift(PriorityNode now, V key) {
			if(now.Left == null && now.Right == null)
				return null;    //execute delete
			else if(now.Left == null)
				now = now.LeftRotate();
			else if(now.Right == null)
				now = now.RightRotate();
			else {
				if(now.Left.priority.CompareTo(now.Right.priority) > 0)
					now = now.RightRotate();
				else
					now = now.LeftRotate();
			}
			return DeleteSearch(now, key);
		}
		public void Insert(V value, P priority) {
			top = InsertFrom(top, value, priority);
		}
		public bool Find(V key) {
			var now = top;
			while(now != null) {
				switch(key.CompareTo(now.value)) {
					case -1:
						now = now.Left;
						break;
					case 1:
						now = now.Right;
						break;
					default:
						return true;
				}
			}
			return false;
		}
		public void Delete(V key) {
			top = DeleteSearch(top, key);
		}
		private static void MidEmit(PriorityNode now) {
			if(now == null) return;
			MidEmit(now.Left);
			Console.Write($" {now.value}");
			MidEmit(now.Right);
		}
		public void MidEmit() { MidEmit(top); Console.Write("\n"); }
		private static void FrontEmit(PriorityNode now) {
			if(now == null) return;
			Console.Write($" {now.value}");
			FrontEmit(now.Left);
			FrontEmit(now.Right);
		}
		public void FrontEmit() { FrontEmit(top); Console.Write("\n"); }
	}
}



Measured memory_bytes: 30976