#include <cstdio>
#include <fstream>
#include <set>
#include <vector>
using namespace std;
#define nmax 300005
#define MOD 666013
#define x first
#define y second
#define inf (1000000004)
#define bit(x) (x&(-x))
char s[200];
string temp;
set<int> my_set;
multiset<int> diff;
//struct{int max, min;}aint[nmax*4];
struct{int max, min;} aib[nmax];
vector<pair<int,int> > lista[MOD+4];
int poz_aint = 0;
typedef set<int>::iterator it_set;
multiset<int>::iterator it_diff;
/*
void udpate(int nod, int st, int dr, int poz, int val, int val2){
if(st == dr){
aint[nod].max = val;
aint[nod].min = val2;
return;
}
int mij =(st +dr)/2;
if (poz <= mij) udpate(nod*2, st, mij, poz, val, val2);
else udpate(nod*2+1, mij+1, dr, poz, val, val2);
aint[nod].max = max(aint[nod*2].max, aint[nod*2+1].max);
aint[nod].min = min(aint[nod*2].min, aint[nod*2+1].min);
}
*/
vector<pair<int,int> >::iterator afla_iterator(int x) {
int rest = x % MOD;
for(vector<pair<int,int> >::iterator it=lista[rest].begin(); it!=lista[rest].end(); it++){
if (it->x == x){
return it;
}
}
return lista[rest].end();
}
pair<int,int> citeste_buf(){
int poz = 0;
pair<int,int> R;
temp.clear();
for(; s[poz]>='A'&&s[poz]<='Z'; poz++)temp += s[poz];
if (temp == "MAX"){
R.x = 5;
return R;
}else if (temp == "MIN"){
R.x = 4;
return R;
}
for(; s[poz] == ' '; poz++);
for(; s[poz]>='0' && s[poz]<='9'; poz++){
R.y = R.y*10 + (s[poz]-'0');
}
if (temp == "I"){
R.x = 1;
return R;
}else if (temp == "S"){
R.x = 2;
return R;
}else {
R.x = 3;
return R;
}
}
void udpate(int poz, int val, int val2){
int cpy = poz;
for(; poz<=poz_aint; poz+=bit(poz)) aib[poz].max+=val;
poz = cpy;
for(; poz<=poz_aint; poz+=bit(poz)) aib[poz].min+=val2;
}
int query(int cod){
if (cod == 0){
int x = -inf;
int poz = poz_aint;
for(;poz>=1; poz-=bit(poz)) x = max(x,aib[poz].max);
return x;
}else{
int x= inf;
int poz = poz_aint;
for(; poz>=1; poz-=bit(poz)) x = min(x, aib[poz].min);
return x;
}
}
void rezolva(){
//for(int i=1; i<nmax; i++) aib[i].min = inf;
while(gets(s)!=NULL){
pair<int,int> aux = citeste_buf();
if (aux.x == 1){//insert
int rest = aux.y % MOD;
if (afla_iterator(aux.y)==lista[rest].end()){
++poz_aint;
lista[rest].push_back(make_pair(aux.y, poz_aint));
//udpate(1,1,nmax, poz_aint, aux.y, aux.y);
//udpate(poz_aint,aux.y, aux.y);
my_set.insert(aux.y);
it_set i = my_set.find(aux.y);
it_set last = my_set.end();
--last;
if (i==my_set.begin()){
it_set dr = i;
dr++;
//printf("pasul : %d ; %d", pas, *dr-*i);
if (my_set.size()>=2)diff.insert(*dr-*i);
}
else if (i==last){//elemntul se afla la capat
it_set st;
st = i;
--st;
if (my_set.size()>=2)diff.insert(*i-*st);
}else{//e in interior
it_set st,dr;
st = i; dr = i;
--st; dr++;
if (my_set.size()>=2)diff.insert(*i-*st);
if (my_set.size()>=2)diff.insert(*dr-*i);
}
}
}
else if (aux.x == 2){//sterge
int rest = aux.y % MOD;
vector<pair<int,int> >::iterator ii = afla_iterator(aux.y);
if (ii == lista[rest].end()){
printf("-1\n");
}
else {
int poz = ii->y;
//udpate(1,1,nmax, poz, -inf, inf);
udpate(poz, -inf, inf);
it_set i = my_set.find(aux.y);
it_set last = my_set.end();
--last;
if (i==my_set.begin()){
it_set dr = i;
dr++;
it_diff = diff.find(*dr-*i);
diff.erase(it_diff);
}
else if (i==last){//elemntul se afla la capat
it_set st;
st = i;
--st;
it_diff = diff.find(*i-*st);
diff.erase(it_diff);
}else{//e in interior
it_set st,dr;
st = i; dr = i;
--st; dr++;
diff.insert(*dr-*st);
it_diff = diff.find(*i-*st);
diff.erase(it_diff);
it_diff = diff.find(*dr-*i);
diff.erase(it_diff);
}
my_set.erase(i);
}
}
else if (aux.x == 3){//cauta
int rest = aux.y%MOD;
if (afla_iterator(aux.y) == lista[rest].end()){//nu l-a gasit
printf("0\n");
}else printf("1\n");//exista
}
else if (aux.x == 5){//Max
//printf("size: %d\n", my_set.size());
if (my_set.size()<2){
printf("-1\n");
}else{
it_set i = my_set.begin();
int Min = *i;
i = my_set.end();
--i;
int Max = *i;
printf("%d\n", Max-Min);
}
}
else if (aux.x == 4){//Min
if (my_set.size()<2){
printf("-1\n");
}else printf("%d\n", (*diff.begin()) );
}
}
}
int main(){
freopen("zeap.in", "r", stdin);
freopen("zeap.out", "w", stdout);
rezolva();
}