#include<cstdio>
#include<algorithm>
using namespace std;
int i,j,m,r,last;
long long part[510],q[200010],ans,d,n;
int p(int x){return (x+499)/500;}
int main()
{
freopen("bzoj_1012.in","r",stdin);
freopen("bzoj_1012.out","w",stdout);
scanf("%d%lld",&m,&d);
while (m--){
char s=1;
while (s<'A'||s>'Z') s=getchar();
scanf("%lld",&n);
if (s=='A'){
q[++r]=(n+ans)%d;
i=p(r);part[i]=max(part[i],q[r]);
}
if (s=='Q'){
last=r-n+1;ans=q[r];
for (i=p(r)*500;i-499>=last;i-=500) ans=max(ans,part[i/500]);
for (;i>=last;i--) ans=max(ans,q[i]);
printf("%lld\n",ans);
}
}
return 0;
}