#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define NMax 300011
#define oo 1<<30
struct Int { int M, m, D; };
struct Op { int Type, Time, X, NX;};
Int A[3*NMax];
Op Operatie[NMax];
int N, Maxim;
void Update (int K, int L, int R, int X, int V)
{
int Mid=(L+R)/2;
if (L==R)
{
A[K].M=A[K].m=V;
if (V==0)
A[K].M=-oo,
A[K].m=oo;
return;
}
if (X<=Mid)
Update (2*K, L, Mid, X, V);
else
Update (2*K+1, Mid+1, R, X, V);
A[K].m=min (A[2*K].m, A[2*K+1].m);
A[K].M=max (A[2*K].M, A[2*K+1].M);
A[K].D=min (min (A[2*K].D, A[2*K+1].D), A[2*K+1].m-A[2*K].M);
}
int Query (int K, int L, int R, int X)
{
int Mid=(L+R)/2;
if (L==R)
return (A[K].M==A[K].m);
if (X<=Mid)
return Query (2*K, L, Mid, X);
else
return Query (2*K+1, Mid+1, R, X);
}
void Build (int K, int L, int R)
{
int Mid=(L+R)/2;
A[K].M=-oo;
A[K].m=oo;
A[K].D=oo;
if (L==R) return;
Build (2*K, L, Mid);
Build (2*K+1, Mid+1, R);
}
bool Xsort (const Op &A, const Op &B)
{
if (A.Type>2)
return false;
if (B.Type>2)
return true;
return A.X<B.X;
}
bool Tsort (Op A, Op B)
{ return A.Time<B.Time; }
void Normalize()
{
sort (Operatie+1, Operatie+N+1, Xsort);
Operatie[1].NX=1;
for (int i=2; i<=N and Operatie[i].Type<=2; ++i)
{
if (Operatie[i].X>Operatie[i-1].X)
Operatie[i].NX=Operatie[i-1].NX+1;
else
Operatie[i].NX=Operatie[i-1].NX;
Maxim=max (Maxim, Operatie[i].NX);
}
sort (Operatie+1, Operatie+N+1, Tsort);
}
bool ReadType(char Type[5])
{
memset (Type, 0, sizeof (Type));
if (scanf ("%s", Type)!=1)
return false;
return true;
}
int main()
{
freopen ("zeap.in", "r", stdin);
freopen ("zeap.out", "w", stdout);
char Type[5];
while (ReadType (Type))
{
++N; Operatie[N].Time=N;
if (Type[0]=='I') Operatie[N].Type=0, scanf ("%d\n", &Operatie[N].X);
if (Type[0]=='S') Operatie[N].Type=1, scanf ("%d\n", &Operatie[N].X);
if (Type[0]=='C') Operatie[N].Type=2, scanf ("%d\n", &Operatie[N].X);
if (Type[0]=='M' and Type[1]=='A') Operatie[N].Type=3;
if (Type[0]=='M' and Type[1]=='I') Operatie[N].Type=4;
}
Normalize ();
Build (1, 1, Maxim);
for (int i=1; i<=N; ++i)
{
if (Operatie[i].Type==0)
Update (1, 1, Maxim, Operatie[i].NX, Operatie[i].X);
if (Operatie[i].Type==1)
{
if (Query (1, 1, Maxim, Operatie[i].NX)==0) printf ("-1\n");
else Update (1, 1, Maxim, Operatie[i].NX, 0);
}
if (Operatie[i].Type==2)
printf ("%d\n", Query (1, 1, Maxim, Operatie[i].NX));
if (Operatie[i].Type==3)
{
if (A[1].M>A[1].m)
printf ("%d\n", A[1].M-A[1].m);
else
printf ("-1\n");
}
if (Operatie[i].Type==4)
{
if (A[1].M>A[1].m)
printf ("%d\n", A[1].D);
else
printf ("-1\n");
}
}
}