/// TANVIR HASAN
#include <bits/stdc++.h>
using namespace std;
#define hello printf("HELLO\n")
#define uu first
#define vv second
#define pb push_back
#define mp make_pair
#define LL long long
#define inf INT_MAX/3
#define mod 1000000007ll
#define PI acos(-1.0)
#define linf (1ll<<60)-1
#define pii pair<int,int>
#define vl vector<LL>
#define vi vector<int>
#define vs vector<string>
#define pii pair<int,int>
#define ALL(A) (A).begin(),(A).end()
#define mset(a,v) memset(a,v,sizeof a)
#define setinf(ar) memset(ar,126,sizeof ar)
#define vsort(v) sort(v.begin(),v.end())
#define FOR(I,A,B) for (__typeof (B) I = (A) ; I <= B ; ++I)
#define rof(i, a, b) for (__typeof (b)i = (b) ; i >= a ; --i)
#define rep(I, n) for (__typeof (n) I = 0 ; I < n ; ++I)
#define per(i, n) for (__typeof (n)i = (n-1) ; i >= 0 ; --i)
#define forstl(I, s) for (__typeof ((s).end ()) I = (s).begin (); I != (s).end (); ++I)
#define rofstl(I, s) for (__typeof ((s).end ()) I = (s).end ()-1; I != (s).begin (); --I)
#define Int ({int a; scanf("%d", &a); a;})
#define I64 ({LL a; scanf("%I64d", &a); a;})
#define Double ({double a; scanf("%lf", &a); a;})
#define Char ({char a; scanf("%c", &a); a;})
void FastIO() {ios::sync_with_stdio(0);cin.tie(0);}
#define En "\n"
#define round(a,b,n) ((a-1+b)% n + n)% n + 1 /// 1 base round,, a starting index ,,+b for left to right,,-b for left,|b| turn,,total n gor
#define tata return 0
/// error////////////////////
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); puts(""); }
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c))
v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << " ";
err(++it, args...);
}
#define dbg(x) cout<<#x<<" : "<<x<<endl
/// eeerrrrooooooorrrrrrr //////////////////
template <class T> inline T bigmod(T p,T e,T M){LL ret = 1;for(; e > 0; e >>= 1){if(e & 1) ret = (ret * p) % M;p = (p * p) % M;}return (T)ret;}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);} // M is prime}
template <class T> inline T bpow(T p,T e){LL ret = 1;for(; e > 0; e >>= 1){if(e & 1) ret = (ret * p);p = (p * p);}return (T)ret;}
#define EPS numeric_limits<double>::epsilon()
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Let's do it ///
/// Let's try it ///
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
#define maxN 1000005
struct bnode{
int x, prior,size, lmn,rmn;
bnode *l,*r;
bnode(int _x){
x=_x;
prior=((rand()<<16)^rand());
l=r=NULL;
size=1;
rmn=lmn=inf;
}
bnode(int _x,int p){
x=_x;
prior=p;
l=r=NULL;
size=1;
}
bnode(){}
};
typedef bnode * bspnode;
int sz(bspnode t){
return t?t->size:0;
}
void upd_sz(bspnode t){
if(t)t->size=sz(t->l)+1+sz(t->r);
}
void push(bspnode t){
if(!t) return;
t->rmn=t->lmn=inf;
if(t->l) t->lmn= abs(t->x - t->l->x);
if(t->r) t->rmn= abs(t->x - t->r->x);
upd_sz(t);
}
void bsplit(bspnode t,bspnode &l,bspnode &r,int value){
if(!t)return void(l=r=NULL);
push(t);
upd_sz(t);
if(value<t->x)//element at pos goes to "l"
bsplit(t->l,l,t->l,value),r=t;
else
bsplit(t->r,t->r,r,value),l=t;
upd_sz(l);
upd_sz(r);
push(l);
push(r);
}
void bmerge(bspnode &t,bspnode l,bspnode r){ //l->leftarray,r->rightarray,t->resulting array
upd_sz(l),upd_sz(r);
push(l),push(r);
if(!l || !r) t = l?l:r;
else{
if(l->prior>r->prior)bmerge(l->r,l->r,r),t=l;
else bmerge(r->l,l,r->l),t=r;
}
upd_sz(t);
push(t);
}
void insert (bspnode &tb,int x){
bspnode bl,br;
bsplit(tb,bl,br,x-1);
bmerge(bl,bl,new bnode(x));
bmerge(tb,bl,br);
}
void output(bspnode t){
if(!t) return;
output(t->l);
printf("%d ",t->x);
output(t->r);
}
bspnode unite(bspnode l,bspnode r){
bspnode ret,less,grt;
if(!l ||!r) return l?l:r;
upd_sz(l),upd_sz(r);
if(l->prior < r->prior) swap(l,r);
bsplit(r,less,grt,l->x);
ret=new bnode(l->x,l->prior);
ret->l=unite(l->l,less);
ret->r=unite(l->r,grt);
upd_sz(ret);
return ret;
}
/*
void unite (pnode &t,pnode l, pnode r) {
if (!l || !r) return void(t = l ? l : r);
if (l->prior < r->prior) swap (l, r);
pnode lt, rt;
split (r,lt, rt,l->val);
unite (l->l,l->l, lt);
unite (l->r,l->r, rt);
t=l;upd_sz(t);
}*/
int lmn,rmn;
int getKth(bspnode t,int k){
if(!t) return inf;
upd_sz(t);
if(k==sz(t->l)+1) {lmn=t->lmn,rmn=t->rmn;return t->x;}
if(sz(t->l)<k){
k-=sz(t->l)+1;
getKth(t->r,k);
}
else getKth(t->l,k);
}
void del(bspnode &t,int x){
if(!t) return;
upd_sz(t);
bspnode less,grt,midam;
bsplit(t,less,grt,x-1);
bsplit(grt,midam,grt,x); //equal and less
bmerge(t,less,grt);
upd_sz(t);
}
void split(bspnode t,bspnode &l,bspnode &r,int pos,int add=0){
if(!t)return void(l=r=NULL);
push(t);
int curr_pos = add + sz(t->l);
if(pos<=curr_pos)//element at pos goes to "l"
split(t->l,l,t->l,pos,add),r=t;
else
split(t->r,t->r,r,pos,curr_pos+1),l=t;
push(l);
push(r);
}
//////////////
bspnode root;
char cd[30];
int val;
int main(){
// freopen("in.txt","r",stdin);
int n=Int;
while(n--){
scanf(" %s %d",cd,&val);
if(cd[0]=='I'){
insert(root,val);
}
else if(cd[0]=='D'){
del(root,val);
}
else if(cd[0]=='X'){
int val2=Int+1;
val++;
if(val==val2) {cout<<-1<<En;continue;}
int m=getKth(root,val);
int k=getKth(root,val2);
//error(m,k);
cout<<abs(k-m)<<En;
}else if(cd[0]=='N'){
val++;
int val2=Int+1;
if(val==val2) {cout<<-1<<En;continue;}
int mid=(val+val2)>>1;
if(val2-val==1){
int m= abs(getKth(root,val2) -getKth(root,val));
cout<<m<<En;
continue;
}
bspnode less,grt,tmp;
split(root,less,grt,val-1+1);
split(grt,grt,tmp,val2-val+1-1);
int mn=inf;
if(grt)
mn=min(grt->lmn,grt->rmn);
bmerge(grt,grt,tmp);
bmerge(root,less,grt);
getKth(root,val);
mn=min(mn,rmn);
getKth(root,val2);
mn=min(mn,lmn);
cout<<mn<<En;
}
// output(root);
// hello;
}
return 0;
}
Ly8vICAgICAgVEFOVklSIEhBU0FOCgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgaGVsbG8gICAgICBwcmludGYoIkhFTExPXG4iKQojZGVmaW5lIHV1ICAgICAgICAgZmlyc3QKI2RlZmluZSB2diAgICAgICAgIHNlY29uZAojZGVmaW5lIHBiICAgICAgICAgcHVzaF9iYWNrCiNkZWZpbmUgbXAgICAgICAgICBtYWtlX3BhaXIKI2RlZmluZSBMTCAgICAgICAgIGxvbmcgbG9uZwojZGVmaW5lIGluZiAgICAgICAgSU5UX01BWC8zCiNkZWZpbmUgbW9kICAgICAgICAxMDAwMDAwMDA3bGwKI2RlZmluZSBQSSAgICAgICAgIGFjb3MoLTEuMCkKI2RlZmluZSBsaW5mICAgICAgICgxbGw8PDYwKS0xCiNkZWZpbmUgcGlpICAgICAgICBwYWlyPGludCxpbnQ+CiNkZWZpbmUgdmwgICAgICAgIHZlY3RvcjxMTD4KI2RlZmluZSB2aSAgICAgICAgdmVjdG9yPGludD4KI2RlZmluZSB2cyAgICAgICAgdmVjdG9yPHN0cmluZz4KI2RlZmluZSBwaWkgICAgICAgIHBhaXI8aW50LGludD4KCiNkZWZpbmUgQUxMKEEpICAgICAoQSkuYmVnaW4oKSwoQSkuZW5kKCkKI2RlZmluZSBtc2V0KGEsdikgICA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iAgICBibWVyZ2UodCxsZXNzLGdydCk7CiAgICB1cGRfc3oodCk7Cn0KCgoKdm9pZCBzcGxpdChic3Bub2RlIHQsYnNwbm9kZSAmbCxic3Bub2RlICZyLGludCBwb3MsaW50IGFkZD0wKXsKICAgIGlmKCF0KXJldHVybiB2b2lkKGw9cj1OVUxMKTsKICAgcHVzaCh0KTsKICAgIGludCBjdXJyX3BvcyA9IGFkZCArIHN6KHQtPmwpOwogICAgaWYocG9zPD1jdXJyX3BvcykvL2VsZW1lbnQgYXQgcG9zIGdvZXMgdG8gImwiCiAgICAgICAgc3BsaXQodC0+bCxsLHQtPmwscG9zLGFkZCkscj10OyAgCiAgICBlbHNlIAogICAgICAgIHNwbGl0KHQtPnIsdC0+cixyLHBvcyxjdXJyX3BvcysxKSxsPXQ7ICAKICAgICAgICAKICBwdXNoKGwpOwogIHB1c2gocik7CiAgIAp9CgovLy8vLy8vLy8vLy8vLwoKCmJzcG5vZGUgcm9vdDsKY2hhciBjZFszMF07CmludCB2YWw7CgppbnQgbWFpbigpewogLy8gZnJlb3BlbigiaW4udHh0IiwiciIsc3RkaW4pOwogICBpbnQgbj1JbnQ7CiAgIHdoaWxlKG4tLSl7CgogICAgc2NhbmYoIiAlcyAlZCIsY2QsJnZhbCk7CiAgICBpZihjZFswXT09J0knKXsKICAgICAgCiAgICAgIGluc2VydChyb290LHZhbCk7CiAgICB9CiAgICBlbHNlIGlmKGNkWzBdPT0nRCcpewogICAgICBkZWwocm9vdCx2YWwpOwoKICAgIH0KICAgIGVsc2UgaWYoY2RbMF09PSdYJyl7CiAgICAgIAogICAgICBpbnQgdmFsMj1JbnQrMTsKICAgICAgdmFsKys7CiAgICAgIGlmKHZhbD09dmFsMikge2NvdXQ8PC0xPDxFbjtjb250aW51ZTt9CiAgICAgIGludCBtPWdldEt0aChyb290LHZhbCk7CiAgICAgIGludCBrPWdldEt0aChyb290LHZhbDIpOwogICAgICAvL2Vycm9yKG0sayk7CiAgICAgIGNvdXQ8PGFicyhrLW0pPDxFbjsKCiAgICB9ZWxzZSBpZihjZFswXT09J04nKXsKICAgICAgCiAgICAgIHZhbCsrOwogICAgICBpbnQgdmFsMj1JbnQrMTsKICAgICAgaWYodmFsPT12YWwyKSB7Y291dDw8LTE8PEVuO2NvbnRpbnVlO30KICAgICAgaW50IG1pZD0odmFsK3ZhbDIpPj4xOwogICAgICBpZih2YWwyLXZhbD09MSl7CiAgICAgICAgaW50IG09IGFicyhnZXRLdGgocm9vdCx2YWwyKSAtZ2V0S3RoKHJvb3QsdmFsKSk7CiAgICAgICAgY291dDw8bTw8RW47CiAgICAgICAgY29udGludWU7CiAgICAgIH0KICAgICAgICBic3Bub2RlIGxlc3MsZ3J0LHRtcDsKICAgICAgICBzcGxpdChyb290LGxlc3MsZ3J0LHZhbC0xKzEpOwogICAgICAgIHNwbGl0KGdydCxncnQsdG1wLHZhbDItdmFsKzEtMSk7CiAgICAgICAgaW50IG1uPWluZjsKICAgICAgICBpZihncnQpCiAgICAgICAgIG1uPW1pbihncnQtPmxtbixncnQtPnJtbik7CiAgICAgICAgYm1lcmdlKGdydCxncnQsdG1wKTsKICAgICAgICBibWVyZ2Uocm9vdCxsZXNzLGdydCk7CiAgICAgICAgZ2V0S3RoKHJvb3QsdmFsKTsKICAgICAgICBtbj1taW4obW4scm1uKTsKICAgICAgICBnZXRLdGgocm9vdCx2YWwyKTsKICAgICAgICBtbj1taW4obW4sbG1uKTsKICAgICAgICBjb3V0PDxtbjw8RW47ICAgICAgICAKICAgICAgCgogICAgfQogIC8vIG91dHB1dChyb290KTsKICAgLy8gaGVsbG87CiAgIH0KCgoKCgogcmV0dXJuIDA7Cn0KCg==