#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
const int MAXN=140005;
struct node
{
int d,size;
int h;
node *parent,*ch[2];
}pool[MAXN],*rf,*root;
int cnt,pow[MAXN];
void GetPow()
{
pow[0]=1;
for(int i=1;i<MAXN;i++)
pow[i]=(long long)pow[i-1]*137%10000007;
}
int size(node *p)
{
if(p) return p->size;
return 0;
}
void update(node *p)
{
p->size=1+size(p->ch[0])+size(p->ch[1]);
p->h=0;
if(p->ch[0]) p->h=p->ch[0]->h;
p->h=((long long)p->h*pow[1]+p->d)%10000007;
if(p->ch[1]) p->h=((long long)p->h*pow[size(p->ch[1])]+p->ch[1]->h)%10000007;
}
void rotate(node *p,int type)
{
node *parent=p->parent,*son=p->ch[!type],*gp=p->parent->parent;
parent->ch[type]=son;
if(son) son->parent=parent;
p->ch[!type]=parent;
parent->parent=p;
p->parent=gp;
gp->ch[parent==gp->ch[1]]=p;
update(parent);
update(p);
if(parent==root) root=p;
}
void splay(node *p,node *target)
{
while(p->parent!=target)
{
if(p->parent->parent==target)
rotate(p,p==p->parent->ch[1]);
else
{
node *parent=p->parent,*gp=p->parent->parent;
int f=parent==gp->ch[0];
if(p==parent->ch[f]) rotate(p,f),rotate(p,!f);
else rotate(parent,!f),rotate(p,!f);
}
}
}
node *find(node *p,int k)
{
if(size(p->ch[0])>=k) return find(p->ch[0],k);
else if(size(p->ch[0])==k-1) return p;
else return find(p->ch[1],k-1-size(p->ch[0]));
}
void insert(int x,node *newnode)
{
node *p=find(root,x);
splay(p,rf);
p=find(root,x+1);
splay(p,root);
p->ch[0]=newnode;
newnode->parent=p;
update(p);
update(p->parent);
}
void change(int x,int d)
{
node *p=find(root,x);
splay(p,rf);
p->d=d;
update(p);
}
bool query(int x,int y,int dis)
{
if(dis==-1) return true;
node *p=find(root,x-1);
splay(p,rf);
p=find(root,x+dis+1);
splay(p,root);
int hash1=p->ch[0]->h;
p=find(root,y-1);
splay(p,rf);
p=find(root,y+dis+1);
splay(p,root);
if(p->ch[0]->h==hash1) return true;
return false;
}
int Query(int x,int y)
{
if(x>y) swap(x,y);
int s=-1,t=size(root)-y-1,mid;
while(s<t)
{
mid=(s+t+1)/2;
if(query(x,y,mid)) s=mid;
else t=mid-1;
}
return s+1;
}
int main()
{
char ch1[100000],sh[1],sh1[1];
int i,m,x,y,len;
node *p;
scanf("%s",ch1);
len=strlen(ch1);
GetPow();
rf=&pool[++cnt];
root=&pool[++cnt];
root->d=root->h=0;
root->size=1;
root->parent=rf;
rf->ch[0]=root;
for(i=0;i<len;i++)
{
p=&pool[++cnt];
p->d=p->h=ch1[i]-‘a‘+1;
p->size=1;
root->ch[1]=p;
p->parent=root;
splay(p,rf);
}
p=&pool[++cnt];
p->d=p->h=0;p->size=1;
root->ch[1]=p;
p->parent=root;
splay(p,rf);
scanf("%d",&m);
while(m --> 0)
{
scanf("%s",sh);
if(sh[0]==‘Q‘)
{
scanf("%d%d",&x,&y);
x++;y++;
if(x!=y) printf("%d\n",Query(x,y));
else printf("%d\n",size(root)-x);
}
else if(sh[0]==‘R‘)
{
scanf("%d",&x);scanf("%s",sh1);
x++;
change(x,sh1[0]-‘a‘+1);
}
else
{
scanf("%d",&x);scanf("%s",sh1);
x++;
p=&pool[++cnt];
p->d=p->h=sh1[0]-‘a‘+1;
p->size=1;
insert(x,p);
}
}
return 0;
}