#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cassert>
using namespace std;
const int INF = 0x7fffffff;
struct node {
int p;
int v;
int vmin;
int vmax;
int difmin;
int siz;
node *left;
node *right;
};
node *defNode = new node{0, 0, 0, 0, INF, 0, NULL, NULL};
node *makeNode(node *old, node *left, node *right) {
old->vmin = min(old->v, min(left->vmin, right->vmin));
old->vmax = max(old->v, max(left->vmax, right->vmax));
old->difmin = min(right->vmin - left->vmax, min(left->difmin, right->difmin));
old->siz = left->siz + 1 + right->siz;
old->left = left;
old->right = right;
return old;
}
pair<node*, node*> split(node *t, int v) {
if(t == defNode) return make_pair(defNode, defNode);
node *left, *right;
pair<node*, node*> s;
if(t->v < v) {
s = split(t->right, v);
left = makeNode(t, t->left, s.first);
right = s.second;
}
else {
s = split(t->left, v);
left = s.first;
right = makeNode(t, s.second, t->right);
}
return make_pair(left, right);
}
node *join(node *left, node *right) {
if(left == defNode) return right;
if(right == defNode) return left;
if(left->p <= right->p)
return makeNode(left, left->left, join(left->right, right));
else
return makeNode(right, join(left, right->left), right->right);
}
node *ins(node *t, int v) {
node *newNode = new node{rand(), v, v, v, INF, 1, defNode, defNode};
pair<node*, node*> s = split(t, v);
return join(join(s.first, newNode), s.second);
}
node *del(node *t, int v) {
pair<node*, node*> s = split(t, v);
return join(s.first, split(s.second, v + 1).second);
}
bool fnd(node *t, int v) {
if(t == defNode) return 0;
if(v < t->v) return fnd(t->left, v);
if(v > t->v) return fnd(t->right, v);
return 1;
}
int main() {
freopen("zeap.in", "r", stdin);
freopen("zeap.out", "w", stdout);
int i, x, y;
char c1, c2, c3;
srand(time(NULL));
node *t = defNode;
while(scanf("%c", &c1) != EOF) {
if(c1 == 'M') {
scanf("%c%c\n", &c2, &c3);
if(c2 == 'I') {
y = (t->siz < 2 ? -1 : t->difmin);
}
if(c2 == 'A') {
y = (t->siz < 2 ? -1 : t->vmax - t->vmin);
}
}
else {
scanf("%d\n", &x);
if(c1 == 'I') {
if(!fnd(t, x)) {
t = ins(t, x);
}
y = INF;
}
if(c1 == 'S') {
if(fnd(t, x)) {
t = del(t, x);
y = INF;
}
else {
y = -1;
}
}
if(c1 == 'C') {
y = fnd(t, x);
}
}
if(y != INF) {
printf("%d\n", y);
}
}
return 0;
}