int main(){

    int i,j,k,l,test,t=1,val;

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    scanf("%d",&test);

    while(test--){

        scanf("%s",type);

        //printf("%s\n",type);

        if(type[0]=='I'){
            scanf("%d",&val);
            tree.insert(val);
        }

        if(type[0]=='D'){
            scanf("%d",&val);
            tree.erase(val);
        }

        if(type[0]=='N'){
            scanf("%d %d",&k,&l);
            printf("%d\n",find_min(k+1,l+1));
        }

        if(type[0]=='X'){
            scanf("%d %d",&k,&l);
            printf("%d\n",find_max(k+1,l+1));
        }

    }

    return 0;
}
 void disconnect(node*a,node*b){
     trp.erase(b->vir,a-&ns[0]);
 }