//Pranet Verma
#include <bits/stdc++.h>
using namespace std;
namespace Splay
{
typedef struct Node* pnode;
pnode null;
struct Node
{
int val,mx,sz;
pnode c[2],p;
};
bool getDir(pnode p,pnode x)
{
return p->c[1]==x;
}
void setLink(pnode x,pnode y,int d)
{
x->c[d]=y;
y->p=x;
}
void update(pnode u)
{
u->sz=1+u->c[0]->sz+u->c[1]->sz;
u->mx=max(u->val,max(u->c[0]->mx,u->c[1]->mx));
}
void rotate(pnode y,bool d)
{
pnode x=y->c[d],z=y->p;
setLink(y,x->c[d^1],d);
setLink(x,y,d^1);
setLink(z,x,getDir(z,y));
update(x);update(y);
}
void splay(pnode x)
{
while(x->p!=null)
{
pnode y=x->p,z=y->p;
int dy=getDir(y,x),dz=getDir(z,y);
if(z==null)
rotate(y,dy);
else if(dy==dz)
rotate(z,dz),rotate(y,dy);
else
rotate(y,dy),rotate(z,dz);
}
}
pnode get(pnode u,int key,int add)
{
while(1)
{
int rank=1+add+u->c[0]->sz;
if(rank==key)
{
splay(u);
return u;
}
else if(rank<key)
{
add=rank;
u=u->c[1];
}
else
u=u->c[0];
}
}
void split(pnode u,pnode &l,pnode &r,int key,int add)
{
if(key==add)
{
l=null;
r=u;
}
else
{
l=get(u,key,add);
r=l->c[1];
l->c[1]=r->p=null;
update(l);
}
}
int query(pnode u,int l,int r,int x,int y)
{
if(x>r || y<l)
return -1e9-9;
if(x<=l && r<=y)
return u->mx;
int rank=l+u->c[0]->sz;
int ret=-1e9-9;
if(x<=rank && rank<=y)
ret=u->val;
ret=max(ret,query(u->c[0],l,rank-1,x,y));
ret=max(ret,query(u->c[1],rank+1,r,x,y));
return ret;
}
Node buff[100005];
pnode ptr=buff;
pnode newNode()
{
return ++ptr;
}
}
using namespace Splay;
int main()
{
null= newNode();
null->val=null->mx=-1e9-9;
null->sz=0;
null->c[0]=null->c[1]=null->p=null;
int m;
scanf("%d\n",&m);
pnode root=null;
while(m--)
{
char ty;
int x,y;
scanf("%c %d %d\n",&ty,&x,&y);
if(ty=='A')
{
pnode a,b;
split(root,a,b,y-1,0);
root=newNode();
root->val=root->mx=x;
root->sz=1;
root->c[0]=a;
root->c[1]=b;
root->p=null;
update(root);
a->p=b->p=root;
}
else if(ty=='Q')
{
printf("%d\n",query(root,1,root->sz,x,y));
}
else
assert(0);
}
}
Ly9QcmFuZXQgVmVybWEKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cm5hbWVzcGFjZSBTcGxheQp7CiAgICB0eXBlZGVmIHN0cnVjdCBOb2RlKiBwbm9kZTsKICAgIHBub2RlIG51bGw7CiAgICBzdHJ1Y3QgTm9kZQogICAgewogICAgICAgIGludCB2YWwsbXgsc3o7CiAgICAgICAgcG5vZGUgY1syXSxwOwogICAgfTsKICAgIGJvb2wgZ2V0RGlyKHBub2RlIHAscG5vZGUgeCkKICAgIHsKICAgICAgICByZXR1cm4gcC0+Y1sxXT09eDsKICAgIH0KICAgIHZvaWQgc2V0TGluayhwbm9kZSB4LHBub2RlIHksaW50IGQpCiAgICB7CiAgICAgICAgeC0+Y1tkXT15OwogICAgICAgIHktPnA9eDsKICAgIH0KICAgIHZvaWQgdXBkYXRlKHBub2RlIHUpCiAgICB7CiAgICAgICAgdS0+c3o9MSt1LT5jWzBdLT5zeit1LT5jWzFdLT5zejsKICAgICAgICB1LT5teD1tYXgodS0+dmFsLG1heCh1LT5jWzBdLT5teCx1LT5jWzFdLT5teCkpOwogICAgfQogICAgdm9pZCByb3RhdGUocG5vZGUgeSxib29sIGQpCiAgICB7CiAgICAgICAgcG5vZGUgeD15LT5jW2RdLHo9eS0+cDsKICAgICAgICBzZXRMaW5rKHkseC0+Y1tkXjFdLGQpOwogICAgICAgIHNldExpbmsoeCx5LGReMSk7CiAgICAgICAgc2V0TGluayh6LHgsZ2V0RGlyKHoseSkpOwogICAgICAgIHVwZGF0ZSh4KTt1cGRhdGUoeSk7CiAgICB9CiAgICB2b2lkIHNwbGF5KHBub2RlIHgpCiAgICB7CiAgICAgICAgd2hpbGUoeC0+cCE9bnVsbCkKICAgICAgICB7CiAgICAgICAgICAgIHBub2RlIHk9eC0+cCx6PXktPnA7CiAgICAgICAgICAgIGludCBkeT1nZXREaXIoeSx4KSxkej1nZXREaXIoeix5KTsKICAgICAgICAgICAgaWYoej09bnVsbCkKICAgICAgICAgICAgICAgIHJvdGF0ZSh5LGR5KTsKICAgICAgICAgICAgZWxzZSBpZihkeT09ZHopCiAgICAgICAgICAgICAgICByb3RhdGUoeixkeikscm90YXRlKHksZHkpOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICByb3RhdGUoeSxkeSkscm90YXRlKHosZHopOwogICAgICAgIH0KICAgIH0KICAgIHBub2RlIGdldChwbm9kZSB1LGludCBrZXksaW50IGFkZCkKICAgIHsKICAgICAgICB3aGlsZSgxKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHJhbms9MSthZGQrdS0+Y1swXS0+c3o7CiAgICAgICAgICAgIGlmKHJhbms9PWtleSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3BsYXkodSk7CiAgICAgICAgICAgICAgICByZXR1cm4gdTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKHJhbms8a2V5KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBhZGQ9cmFuazsKICAgICAgICAgICAgICAgIHU9dS0+Y1sxXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB1PXUtPmNbMF07CiAgICAgICAgfQogICAgfQogICAgdm9pZCBzcGxpdChwbm9kZSB1LHBub2RlICZsLHBub2RlICZyLGludCBrZXksaW50IGFkZCkKICAgIHsKICAgICAgICBpZihrZXk9PWFkZCkKICAgICAgICB7CiAgICAgICAgICAgIGw9bnVsbDsKICAgICAgICAgICAgcj11OwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBsPWdldCh1LGtleSxhZGQpOwogICAgICAgICAgICByPWwtPmNbMV07CiAgICAgICAgICAgIGwtPmNbMV09ci0+cD1udWxsOwogICAgICAgICAgICB1cGRhdGUobCk7CiAgICAgICAgfQogICAgfQogICAgaW50IHF1ZXJ5KHBub2RlIHUsaW50IGwsaW50IHIsaW50IHgsaW50IHkpCiAgICB7CiAgICAgICAgaWYoeD5yIHx8IHk8bCkKICAgICAgICAgICAgcmV0dXJuIC0xZTktOTsKICAgICAgICBpZih4PD1sICYmIHI8PXkpCiAgICAgICAgICAgIHJldHVybiB1LT5teDsKICAgICAgICBpbnQgcmFuaz1sK3UtPmNbMF0tPnN6OwogICAgICAgIGludCByZXQ9LTFlOS05OwogICAgICAgIGlmKHg8PXJhbmsgJiYgcmFuazw9eSkKICAgICAgICAgICAgcmV0PXUtPnZhbDsKICAgICAgICByZXQ9bWF4KHJldCxxdWVyeSh1LT5jWzBdLGwscmFuay0xLHgseSkpOwogICAgICAgIHJldD1tYXgocmV0LHF1ZXJ5KHUtPmNbMV0scmFuaysxLHIseCx5KSk7CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KICAgIE5vZGUgYnVmZlsxMDAwMDVdOwogICAgcG5vZGUgcHRyPWJ1ZmY7CiAgICBwbm9kZSBuZXdOb2RlKCkKICAgIHsKICAgICAgICByZXR1cm4gKytwdHI7CiAgICB9Cn0gICAKdXNpbmcgbmFtZXNwYWNlIFNwbGF5OwppbnQgbWFpbigpCnsKICAgIG51bGw9IG5ld05vZGUoKTsKICAgIG51bGwtPnZhbD1udWxsLT5teD0tMWU5LTk7CiAgICBudWxsLT5zej0wOwogICAgbnVsbC0+Y1swXT1udWxsLT5jWzFdPW51bGwtPnA9bnVsbDsKICAgIGludCBtOwogICAgc2NhbmYoIiVkXG4iLCZtKTsKICAgIHBub2RlIHJvb3Q9bnVsbDsKICAgIHdoaWxlKG0tLSkKICAgIHsKICAgICAgICBjaGFyIHR5OwogICAgICAgIGludCB4LHk7CiAgICAgICAgc2NhbmYoIiVjICVkICVkXG4iLCZ0eSwmeCwmeSk7CiAgICAgICAgaWYodHk9PSdBJykKICAgICAgICB7CiAgICAgICAgICAgIHBub2RlIGEsYjsKICAgICAgICAgICAgc3BsaXQocm9vdCxhLGIseS0xLDApOwogICAgICAgICAgICByb290PW5ld05vZGUoKTsKICAgICAgICAgICAgcm9vdC0+dmFsPXJvb3QtPm14PXg7CiAgICAgICAgICAgIHJvb3QtPnN6PTE7CiAgICAgICAgICAgIHJvb3QtPmNbMF09YTsKICAgICAgICAgICAgcm9vdC0+Y1sxXT1iOwogICAgICAgICAgICByb290LT5wPW51bGw7CiAgICAgICAgICAgIHVwZGF0ZShyb290KTsKICAgICAgICAgICAgYS0+cD1iLT5wPXJvb3Q7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYodHk9PSdRJykKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIscXVlcnkocm9vdCwxLHJvb3QtPnN6LHgseSkpOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgIGFzc2VydCgwKTsKICAgIH0KfQ==