#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=2*1e5+1000;
ll n,d,st[N][30],a[N],x,t=0,num;
char ch;
int main(){
cin>>n>>d;
for(ll i=1;i<=n;i++){
cin>>ch>>x;
if(ch=='A'){
num++;
a[num]++;
st[num][a[num]]=(t%d+x%d)%d;
ll now=num-1,v=2;
while(now>=1){
a[now]++;
st[now][a[now]]=max(st[now][a[now]-1],st[now+(1<<(a[now]-2))][a[now]-1]);
now-=v;
v++;
}
}else if(ch=='Q'){
ll k=log2(x);
ll ans=max(st[num-x+1][k+1],st[num-(1<<k)+1][k+1]);
t=ans;
cout<<ans<<"\n";
}
}
return 0;
}