using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void solve()
    {
        var S = new TreapSet<int>();
        int Q = cin.nextint;
        for (int i = 0; i < Q; i++)
        {
            var s = cin.next[0];
            if (s == 'i')
            {
                S.Add(cin.nextint, cin.nextint);
            }
            else if (s == 'f')
            {
                int k = cin.nextint;
                if (S.UpperBound(k) - S.LowerBound(k) > 0)
                {
                    WriteLine("yes");
                }
                else
                {
                    WriteLine("no");
                }
            }
            else if (s == 'd')
            {
                S.Remove(cin.nextint);
            }
            else if (s == 'p')
            {
                Write(" "); S.Items(0).join();
                Write(" "); S.Items(1).join();
            }
        }
    }

}

class TreapSet<T>
{
    Node root;
    readonly IComparer<T> comparer;
    readonly Node nil;
    public bool IsMultiSet { get; set; }
    public TreapSet(IComparer<T> comparer)
    {
        nil = new Node(default(T), int.MinValue);
        root = nil;
        this.comparer = comparer;
    }
    public TreapSet(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
    public TreapSet() : this(Comparer<T>.Default) { }
    public bool Add(T v, int u)
    {
        key = v;
        priority = u;
        return insert(ref root);
    }
    public bool Remove(T v)
    {
        key = v;
        return remove(ref root);
    }
    public T this[int index] { get { return find(root, index); } }
    public int Count { get { return root.Count; } }
    public void RemoveAt(int k)
    {
        if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
        removeAt(ref root, k);
    }
    public T[] Items(int x)
    {
        var ret = new T[root.Count];
        var k = 0;
        if (x == 0) inorder_walk(root, ret, ref k);
        else if (x == 1) preorder_walk(root, ret, ref k);
        return ret;
    }
    void inorder_walk(Node t, T[] a, ref int k)
    {
        if (t.Count == 0) return;
        inorder_walk(t.lst, a, ref k);
        a[k++] = t.Key;
        inorder_walk(t.rst, a, ref k);
    }
    void preorder_walk(Node t, T[] a, ref int k)
    {
        if (t.Count == 0) return;
        a[k++] = t.Key;
        preorder_walk(t.lst, a, ref k);
        preorder_walk(t.rst, a, ref k);
    }

    T key; int priority;
    bool insert(ref Node t)
    {
        if (t.Count == 0) { t = new Node(key, priority); t.lst = t.rst = nil; t.Update(); return true; }
        var cmp = comparer.Compare(t.Key, key);
        bool res;
        if (cmp > 0) // key < t.Key
        {
            res = insert(ref t.lst);
            if(t.Priority < t.lst.Priority) rotR(ref t);
        }
        else if (cmp == 0) // key == t.Key
        {
            if (IsMultiSet) res = insert(ref t.lst);
            else return false;
        }
        else // key > t.key
        {
            res = insert(ref t.rst);
            if (t.Priority < t.rst.Priority) rotL(ref t);
        }

        //balance(ref t);
        t.Update();
        return res;
    }
    bool remove(ref Node t)
    {
        if (t.Count == 0) return false; //該当ノードなし
        var cmp = comparer.Compare(key, t.Key);
        bool ret;
        if (cmp < 0) ret = remove(ref t.lst);
        else if (cmp > 0) ret = remove(ref t.rst);
        else // 削除対象ノード
        {
            if (t.lst.Count == 0 && t.rst.Count == 0)
            {
                t = nil;
                return true;
            }
            else if (t.lst.Count == 0)
            {
                rotL(ref t);
            }
            else if (t.rst.Count == 0)
            {
                rotR(ref t);
            }
            else
            {
                if (t.lst.Priority > t.rst.Priority)
                {
                    rotR(ref t);
                }
                else
                {
                    rotL(ref t);
                }
            }
            ret = remove(ref t);
            //var k = t.lst.Count;
            //if (k == 0)
            //{
            //    t = t.rst;
            //    return true;
            //}
            //if (t.rst.Count == 0) { t = t.lst; return true; }


            //t.Key = find(t.lst, k - 1);
            //removeAt(ref t.lst, k - 1);
        }
        //balance(ref t);
        t.Update();
        return ret;
    }
    void removeAt(ref Node t, int k)
    {
        var cnt = t.lst.Count;
        if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
        else if (cnt > k) removeAt(ref t.lst, k);
        else
        {
            if (cnt == 0) { t = t.rst; return; }
            if (t.rst.Count == 0) { t = t.lst; return; }

            t.Key = find(t.lst, k - 1);
            removeAt(ref t.lst, k - 1);
        }
        //balance(ref t);
    }
    //void balance(ref Node t)
    //{
    //    var balance = t.lst.Height - t.rst.Height;
    //    if (balance == -2)
    //    {
    //        if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
    //        rotL(ref t);
    //    }
    //    else if (balance == 2)
    //    {
    //        if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
    //        rotR(ref t);
    //    }
    //    else t.Update();
    //}

    T find(Node t, int k)
    {
        if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
        for (; ; )
        {
            if (k == t.lst.Count) return t.Key;
            else if (k < t.lst.Count) t = t.lst;
            else { k -= t.lst.Count + 1; t = t.rst; }
        }
    }
    public int LowerBound(T v)
    {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
            else { k += t.lst.Count + 1; t = t.rst; }
        }
    }
    public int UpperBound(T v)
    {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
            else t = t.lst;
        }
    }
    void rotR(ref Node t)
    {
        var l = t.lst;
        t.lst = l.rst;
        l.rst = t;
        t.Update();
        l.Update();
        t = l;
    }
    void rotL(ref Node t)
    {
        var r = t.rst;
        t.rst = r.lst;
        r.lst = t;
        t.Update();
        r.Update();
        t = r;
    }

    class Node
    {
        public Node(T key, int priority)
        {
            Key = key;
            Priority = priority;
        }
        public int Count { get; private set; }
        //public sbyte Height { get; private set; }
        public int Priority { get; set; }
        public T Key { get; set; }
        public Node lst, rst;
        public void Update()
        {
            Count = 1 + lst.Count + rst.Count;
            //Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
        }
        public override string ToString()
        {
            return string.Format("Count = {0}, Key = {1}", Count, Key);
        }
    }
}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}



Measured memory_bytes: 57240