#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define N 300005
using namespace std;
int mx,n,ri[N],ask[N][2],ans[N],qs[550];
int fd(int k)
{
if(ri[k]==k) return k;
if(!ri[k]) return 0;
return ri[k]=fd(ri[k]);
}
int main()
{
cin>>n;
mx=0;
memset(qs,107,sizeof(qs));
fo(i,1,n)
{
char ch;
int x;
scanf("\n%c %d",&ch,&x);
ask[i][0]=ch-'A';
ask[i][1]=x;
if(ch=='A') mx=max(mx,x),ri[x]=x;
}
int n1=sqrt(mx);
memset(ans,107,sizeof(ans));
fo(i,1,n)
{
if(ask[i][0]==0) fo(j,1,n1) qs[j]=min(qs[j],ask[i][1]%j);
else if(ask[i][1]<=n1) ans[i]=qs[ask[i][1]];
}
fod(i,mx,0) if(!ri[i]) ri[i]=ri[i+1];
fod(i,n,1)
{
if(!ask[i][0]) ri[ask[i][1]]=fd(ask[i][1]+1);
else if(ask[i][1]>n1)
{
for(int j=0;ask[i][1]*j<=mx;j++)
{
int p=fd(ask[i][1]*j);
if(p) ans[i]=min(ans[i],p%ask[i][1]);
}
}
}
fo(i,1,n) if(ask[i][0]==1) printf("%d\n",ans[i]);
}