1 #include<bits/stdc++.h>
2 using namespace std;
3 int read(){
4 int x=0,f=1;char ch=getchar();
5 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
6 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
7 return x*f;
8 }
9 #define N 100005
10 char s[N];
11 int n,num,m,root,sz[N],fa[N],v[N],ch[N][2],base[N],hash[N];
12 inline void pushup(int x){
13 int l=ch[x][0],r=ch[x][1];
14 sz[x]=sz[l]+sz[r]+1;
15 hash[x]=hash[l]+base[sz[l]]*(v[x]+hash[r]*27);
16 }
17 void build(int l,int r,int f){
18 int mid=l+r>>1;
19 fa[mid]=f;if(mid<f)ch[f][0]=mid;else ch[f][1]=mid;
20 if(mid!=1&&mid!=n)v[mid]=s[mid]-‘a‘+1;
21 sz[mid]=1;
22 if(l==r){hash[l]=v[l];return;}
23 if(l<mid)build(l,mid-1,mid);
24 if(r>mid)build(mid+1,r,mid);
25 pushup(mid);
26 }
27 void rotate(int x){
28 int y=fa[x],z=fa[y],k=ch[y][1]==x;
29 fa[ch[y][k]=ch[x][!k]]=y;
30 fa[ch[x][!k]=y]=x;
31 fa[x]=z;
32 if(z)ch[z][ch[z][1]==y]=x;
33 else root=x;
34 pushup(y);
35 }
36 void splay(int x,int f){
37 while(fa[x]!=f){
38 int y=fa[x],z=fa[y];
39 if(z==f)rotate(x);
40 else{
41 if((ch[z][1]==y)==(ch[y][1]==x))rotate(y);
42 else rotate(x);
43 rotate(x);
44 }
45 }
46 pushup(x);
47 }
48 void select(int k,int f){
49 int x=root;
50 while(sz[ch[x][0]]+1!=k){
51 if(sz[ch[x][0]]>=k)x=ch[x][0];
52 else k-=sz[ch[x][0]]+1,x=ch[x][1];
53 }
54 splay(x,f);
55 }
56 void change(int x,int y){
57 select(x+1,0);
58 v[root]=y;
59 pushup(root);
60 }
61 void insert(int x,int y){
62 select(x+1,0);select(x+2,root);
63 int z=ch[root][1];
64 ch[z][0]=num;fa[num]=z;
65 hash[num]=v[num]=y;sz[num]=1;
66 pushup(y);pushup(x);
67 }
68 bool check(int l,int x,int y){
69 select(x,0);select(x+l+1,root);
70 int z=ch[root][1];
71 int valx=hash[ch[z][0]];
72 select(y,0);select(y+l+1,root);
73 z=ch[root][1];
74 int valy=hash[ch[z][0]];
75 return valx==valy;
76 }
77 int lcp(int x,int y){
78 int l=0,r=min(num-x,num-y)-1,mid,ans;
79 while(l<=r){
80 int mid=l+r>>1;
81 if(check(mid,x,y))ans=mid,l=mid+1;
82 else r=mid-1;
83 }
84 return ans;
85 }
86 int main(){
87 scanf("%s",s+2);
88 n=strlen(s+2);n+=2;
89 base[0]=1;for(int i=1;i<N;i++)base[i]=base[i-1]*27;
90 build(1,n,0);root=(1+n)>>1;
91 m=read();num=n;char ch[5];
92 for(int i=1;i<=m;i++){
93 scanf("%s",s);
94 if(s[0]==‘Q‘){
95 int x=read(),y=read();
96 printf("%d\n",lcp(x,y));
97 }
98 if(s[0]==‘R‘){
99 int x=read();scanf("%s",ch);int y=ch[0]-‘a‘+1;
100 change(x,y);
101 }
102 if(s[0]==‘I‘){
103 num++;
104 int x=read();scanf("%s",ch);int y=ch[0]-‘a‘+1;
105 insert(x,y);
106 }
107 }
108 return 0;
109 }