#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 1919810
using namespace std;
int seed,md=19260817;
void _srand(int x){seed=x;}
int _rand(){return seed=(seed*7%md+13)%md;}
int rt,n,m;
char s[N];
unsigned long long h[N],base=131;
void csh(int n){h[0]=1;for(int i=1;i<=n;i++)h[i]=h[i-1]*base;}
struct fhq_treap{
int rnd[N],ch[N][2],siz[N],tot;
unsigned long long val[N];
int c[N];
#define lc ch[x][0]
#define rc ch[x][1]
int update(int x){
siz[x]=siz[lc]+siz[rc]+1;
val[x]=val[lc]*h[siz[rc]+1]+c[x]*h[siz[rc]]+val[rc];
return x;
}
void split(int p,int k,int &x,int &y){
if(!p)return void(x=y=0);
if(siz[ch[p][0]]<k)split(ch[x=p][1],k-siz[ch[p][0]]-1,ch[p][1],y);
else split(ch[y=p][0],k,x,ch[p][0]);
update(p);
}
int merge(int x,int y){
if(!x||!y)return x+y;
if(rnd[x]<rnd[y]){rc=merge(rc,y);return update(x);}
else{ch[y][0]=merge(x,ch[y][0]);return update(y);}
}
int newnode(char v){
int x=++tot;
c[x]=v-'a'+1;lc=rc=0;
siz[x]=1;rnd[x]=_rand();
val[x]=1ull*v;
return x;
}
unsigned long long get(int x,int y){
int len=y-x+1;
int a,b,c,d;
split(rt,x-1,a,b);
split(b,len,c,d);
unsigned long long ans=val[c];
rt=merge(a,merge(c,d));
return ans;
}
void build(char *s,int n){for(int i=0;i<n;i++)rt=merge(rt,newnode(s[i]));}
void print(int x){
if(!x)return;
print(lc);
putchar(c[x]+'a'-1);
print(rc);
}
}t;
int lcp(int x,int y){
int l=1,r=min(n-x,n-y)+1,ans=0;
t.get(1,2);
while(l<=r){
int mid=(l+r)/2;
if(t.get(x,x+mid-1)==t.get(y,y+mid-1))l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
signed main(){
_srand(676767);
scanf("%s%d",s,&m);
n=strlen(s);
csh(n);
t.build(s,n);
while(m--){
char op[10],c[3];
int x,y;
scanf("%s",op);
if(op[0]=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",lcp(x,y));
}else if(op[0]=='R'){
scanf("%d%s",&x,c);
int a,b,d,e;
t.split(rt,x-1,a,b);
t.split(b,1,d,e);
rt=t.merge(a,t.merge(t.newnode(c[0]),e));
}else{
scanf("%d%s",&x,c);
int a,b,d;
t.split(rt,x,a,b);
rt=t.merge(a,t.merge(t.newnode(c[0]),b));
n++;
}
}
return 0;
}