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