#include <bits/stdc++.h>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
const int inf = 1e9;
char ch;
struct treap{
int key, priority;
int minn, maxx, minndif;
treap *left, *right;
treap(int key, int priority, treap *left, treap *right, int minn, int maxx, int minndif){
this->key = key;
this->priority = priority;
this->left = left;
this->right = right;
this->minn = minn;
this->maxx = maxx;
this->minndif = minndif;
}
}*r, *nil;
void init(){
srand(time(0));
r = nil = new treap(0, 0, nullptr, nullptr, inf, -inf, inf);
}
bool Search(treap *nod, int key){
if (nod == nil){
return false;
}
if (nod->key == key){
return true;
}
if (key < nod->key){
return Search(nod->left, key);
}
return Search(nod->right, key);
}
void rotleft(treap* &nod){
treap *aux = nod->left;
nod->left = aux->right;
aux->right = nod;
nod = aux;
}
void rotright(treap* &nod){
treap *aux = nod->right;
nod->right = aux->left;
aux->left = nod;
nod = aux;
}
void update(treap *nod){
nod->maxx = nod->minn = nod->key;
nod->maxx = max(nod->maxx, nod->right->maxx);
nod->minn = min(nod->minn, nod->left->minn);
nod->minndif = min(nod->left->minndif, nod->right->minndif);
if (nod->left != nil)
nod->minndif = min(nod->minndif, nod->key - nod->left->maxx);
if (nod->right != nil)
nod->minndif = min(nod->minndif, nod->right->minn - nod->key);
}
void balance(treap* &nod){
if (nod->left->priority > nod->priority){
rotleft(nod);
update(nod->right);
}
else if (nod->right->priority > nod->priority){
rotright(nod);
update(nod->left);
}
}
void Insert(treap* &nod, int key, int priority){
if (nod == nil){
nod = new treap(key, priority, nil, nil, key, key, inf);
return;
}
if (key < nod->key){
Insert(nod->left, key, priority);
}
else{
Insert(nod->right, key, priority);
}
balance(nod);
update(nod);
}
void Erase(treap* &nod, int key){
if (nod == nil){
return;
}
if (key < nod->key){
Erase(nod->left, key);
update(nod);
}
else if (key > nod->key){
Erase(nod->right, key);
update(nod);
}
else{
if (nod->left == nil && nod->right == nil){
delete nod;
nod = nil;
}
else{
treap *aux;
if (nod->left->priority > nod->right->priority){
aux = nod->left;
rotleft(nod);
}
else{
aux = nod->right;
rotright(nod);
}
Erase(nod, key);
update(aux);
}
}
}
int main(){
init();
int contor = 0;
while (fin >> ch){
if (ch == 'I'){
int x;
fin >> x;
if (!Search(r, x)){
Insert(r, x, rand() + 1);
++contor;
}
}
else if (ch == 'S'){
int x;
fin >> x;
if (!Search(r, x)){
fout << -1 << "\n";
}
else{
Erase(r, x);
--contor;
}
}
else if (ch == 'C'){
int x;
fin >> x;
fout << Search(r, x) << "\n";
}
else{
char a, b;
fin >> a >> b;
if (contor < 2){
fout << -1 << "\n";
continue;
}
if (a == 'A'){
fout << r->maxx - r->minn << "\n";
}
else{
fout << r->minndif << "\n";
}
}
}
fin.close();
fout.close();
return 0;
}