#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 200010
#define MOD 99999937
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
typedef unsigned int usi;
usi Pow[N];int a[N],m;char s[N];
struct splaytree
{
  int lc[3*N],rc[3*N],tp[3*N],s[3*N],fa[3*N],key[3*N];
  usi sum[3*N];
  int root,len;
  void init(){root=1;len=1;}
  void upd(int x)
  {
    s[0]=0;
    s[x]=s[lc[x]]+s[rc[x]]+1;
    int ct=s[lc[x]];sum[0]=0;
    sum[x]=sum[lc[x]]+key[x]*Pow[ct+1]+sum[rc[x]]*Pow[ct+2];
  }
  void lt(int x)
  {
    int y=fa[x];fa[x]=fa[y];fa[y]=x;
    if (rc[x]!=0) {fa[rc[x]]=y;}
    lc[y]=rc[x];rc[x]=y;
    if (fa[x]==0) root=x;
    else {if (y==lc[fa[x]]) lc[fa[x]]=x;else rc[fa[x]]=x;}
    upd(y);upd(x);
  }
  void rt(int x)
  {
    int y=fa[x];fa[x]=fa[y];fa[y]=x;
    if (lc[x]!=0) {fa[lc[x]]=y;}
    rc[y]=lc[x];lc[x]=y;
    if (fa[x]==0) root=x;
    else {if (y==lc[fa[x]]) lc[fa[x]]=x;else rc[fa[x]]=x;}
    upd(y);upd(x);
  }
  void build(int x,int L,int R)
  {
    int mid=(L+R)>>1;
    key[x]=a[mid];s[x]=1;
    if (mid>L) 
    {
      lc[x]=++len;fa[lc[x]]=x;
      build(lc[x],L,mid-1);
    }
    if (mid<R)
    {
      rc[x]=++len;fa[rc[x]]=x;
      build(rc[x],mid+1,R);
    }
    upd(x);
  }
  int select(int x,int ks)
  {
    int ls=s[lc[x]];
    if (ks<=ls) return select(lc[x],ks);
    if (ks<=ls+1) return x;
    return select(rc[x],ks-ls-1);
  }
  int getid(int L,int R)
  {
    int pL=select(root,L-1+1);
    splay(pL);
    int troot=root;
    int pR=select(root,R+1+1);
    root=rc[root];
    fa[root]=0;
    splay(pR);
    fa[root]=troot;rc[troot]=root;
    root=troot;upd(root);
    return lc[rc[root]];
  }
  void splay(int x)
  {
    while (x!=root) 
    {
      if (rc[fa[x]]==x) rt(x);else lt(x);
    }
  }
  int gethash(int L,int R)
  {
    if (L>R) return 0;
    int id=getid(L,R);
    return sum[id];
  }
  void ins(int p,int Key)
  {
    int id=getid(p,p);
    lc[id]=++len;
    s[lc[id]]=1;
    key[lc[id]]=Key;
    sum[lc[id]]=Key;
    fa[lc[id]]=id;
    upd(id);upd(fa[id]);upd(fa[fa[id]]);
  }
  void ins2(int p,int Key)
  {
    int id=getid(p-1,p-1);
    rc[id]=++len;
    s[rc[id]]=1;
    key[rc[id]]=Key;
    sum[rc[id]]=Key;
    fa[rc[id]]=id;
    upd(id);upd(fa[id]);upd(fa[fa[id]]);
  }
  void change(int p,int Key)
  {
    int id=getid(p,p);
    key[id]=Key;sum[id]=Key;
    upd(fa[id]);upd(fa[fa[id]]);
  }
}tree;
bool check(int x,int p1,int p2)
{
  return tree.gethash(p1,p1+x-1)==tree.gethash(p2,p2+x-1);
}
int main()
{
  Pow[1]=1;char op[2],cc[2];int i,x,y,res;
  FOR(i,2,200000) Pow[i]=Pow[i-1]*29;
  int L=0;char ch;
  a[0]=0;
  for(ch=getchar();ch>='a'&&ch<='z';ch=getchar()) 
  {
    a[++L]=ch-'a'+1;
  }a[L+1]=0;
  tree.init();tree.build(1,0,L+1);
  scanf("%d",&m);
  FOR(i,1,m)
  {
    scanf("%s%d",op,&x);
    if (op[0]=='Q')
    {
      scanf("%d",&y);
      if (x>y) {int tmp=x;x=y;y=tmp;}
      int r=L-y+1,l=0;
      while (1)
      {
        if (r-l<=1)
        {
          if (check(r,x,y)) res=r;else res=l;
          break;
        }
        int mid=(l+r)>>1;
        if (check(mid,x,y)) l=mid;else r=mid;
      }
      printf("%d\n",res);
    }else if (op[0]=='I')
    {
      scanf("%s",cc);L++;x++;
      if (L==x)
      {
        tree.ins2(x,cc[0]-'a'+1);
      }else tree.ins(x,cc[0]-'a'+1);
    }else if (op[0]=='R')
    {
      scanf("%s",cc);
      tree.change(x,cc[0]-'a'+1);
    }
  }
}