/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

/// Updated by CodingKnight at Codeforces on Sept 23, 2018

#include <bits/stdc++.h>

using namespace std;

class AVL_tree
{
public:
    int size;
private:
    int key, height; AVL_tree *left, *right;

    friend int get_height( AVL_tree* t )
    {
        return t ? t->height : 0;
    }

    friend int get_balance( AVL_tree* t )
    {
        return t ? ( get_height(t->left) - get_height(t->right) ) : 0;
    }

    friend int get_size( AVL_tree* t )
    {
        return t ? t->size : 0;
    }

    AVL_tree* update()
    {
        return  height = max( get_height(left), get_height(right) ) + 1,
                size = get_size(left) + get_size(right) + 1, this;
    }

    AVL_tree* left_rotate()
    {
        AVL_tree *y = right, *t = y->left; return y->left = this, right = t, update(), y->update();
    }

    AVL_tree* right_rotate()
    {
        AVL_tree *y = left, *t = y->right; return y->right = this, left = t, update(), y->update();
    }

    AVL_tree* find_min()
    {
        AVL_tree *t = this;

        while( t->left != nullptr )
            t = t->left;

        return t;
    }

    AVL_tree* balance()
    {
        int balance = get_balance( this );

        if( balance >  1 and get_balance( left ) >= 0 )
            return right_rotate();

        if( balance < -1 and get_balance( right ) <= 0 )
            return left_rotate();

        if( balance >  1 and get_balance( left  ) <  0 )
            return left = left->left_rotate(), right_rotate();

        if( balance < -1 and get_balance( right ) >  0 )
            return right = right->right_rotate(), left_rotate();

        return this;
    }

    AVL_tree* insert( int k )
    {
        if( k < key )
            left = insert_key( left, k );
        else if( k > key )
            right = insert_key( right, k );
        else
            return this;

        int balance = get_balance( update() );

        if( balance >  1 and k < left->key )
            return right_rotate();

        if( balance < -1 and k > right->key )
            return left_rotate();

        if( balance >  1 and k > left->key )
            return left = left->left_rotate(), right_rotate();

        if( balance < -1 and k < right->key )
            return right = right->right_rotate(), left_rotate();

        return this;
    }

    AVL_tree* Delete( int k )
    {
        if( k < key )
            left = delete_key( left, k );
        else if( k > key )
            right = delete_key( right, k );
        else
        {
            AVL_tree *t = nullptr;

            if( left == nullptr or right == nullptr )
            {
                if( left != nullptr )
                    t = left;
                else
                    t = right;

                if( t == nullptr )
                    t = this, key = INT_MIN;
                else
                    *this = *t;

                if ( t != this )
                    delete t;
            }
            else
                t = right->find_min(), key = t->key, right = delete_key( right, key );
        }

        return this;
    }

public:
    AVL_tree( int k ) : size( 1 ), key( k ), height( 1 ), left( nullptr ), right( nullptr ) {}

    friend AVL_tree* insert_key( AVL_tree* t, int k )
    {
        return t ? t->insert( k ) : new AVL_tree( k );
    }

    friend AVL_tree* delete_key( AVL_tree* t, int k )
    {
        if ( t != nullptr )
            t = t->Delete( k );

        if ( t == nullptr )
            return nullptr;

        if ( t->key != INT_MIN )
            return t->update()->balance();

        delete t; return nullptr;
    }

    int find_k_smallest( int k )
    {
        int left_size = get_size( left ), l = left_size + 1;

        if( k == l )
            return key;

        if( left_size < k )
            return right->find_k_smallest( k - l );

        return left->find_k_smallest( k );
    }

    int count_smaller( int k )
    {
        int total = 0;

        if( k  < key )
        {
            if ( left )
                total = left->count_smaller( k );
        }
        else if( k > key )
        {
            total = get_size( left ) + 1;

            if ( right )
                total += right->count_smaller( k );
        }
        else
            total = get_size( left );

        return total;
    }
};

int main()
{
    ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );

    int q, a; char c; AVL_tree *root = nullptr;

    for( cin >> q; q > 0; q-- )
        switch( ( cin >> c >> a, c ) )
        {
        case 'I':
            root = insert_key( root, a ); break;
        case 'D':
            root = delete_key( root, a ); break;
        case 'K':
            if( root == nullptr or a > root->size )
                cout << "invalid" << '\n';
            else
                cout << root->find_k_smallest( a ) << '\n';
            break;
        case 'C':
            if ( root == nullptr )
                cout << '0' << '\n';
            else
                cout << root->count_smaller( a ) << '\n';
        }
}