#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+10;
int head[maxn],to[maxn],nex[maxn],tot,cnt=1,m;
int ch[maxn][26],fail[maxn];
int son[maxn],siz[maxn],top[maxn],id[maxn],deep[maxn],f[maxn],sum[maxn],tag[maxn],a[maxn],Father[maxn];
char s1[maxn];
void addedge(int x,int y){
nex[++tot]=head[x];
to[tot]=y;
head[x]=tot;
}
void pushup(int x){
sum[x]=sum[x*2]+sum[x*2+1];
}
void pushdown(int x,int l,int r){
if(tag[x]){
int mid=(r+l)>>1;
sum[x*2]+=(mid-l+1)*tag[x];
sum[x*2+1]+=(r-mid)*tag[x];
tag[x*2]+=tag[x];
tag[x*2+1]+=tag[x];
tag[x]=0;
}
}
void insert(char *s){
int now=1,len=strlen(s+1),num=0;
for (int i=1;i<=len;i++){
int x=s[i]-'a';
if (s[i]=='P'){
a[++num]=now;
continue;
}
if(s[i]=='B'){
now=Father[now];
continue;
}
if(!ch[now][x]){
ch[now][x]=++cnt;
Father[cnt]=now;
}
now=ch[now][x];
}
}
void getfail(){
queue<int>q;
for(int i=0;i<26;i++){
if(ch[1][i]){
fail[ch[1][i]]=1;
q.push(ch[1][i]);
}
}
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=0;i<26;i++){
if(ch[now][i]){
fail[ch[now][i]]=ch[fail[now]][i];
q.push(ch[now][i]);
}
else{
ch[now][i]=ch[fail[now]][i];
}
}
}
for(int i=1;i<=cnt;i++){
addedge(fail[i],i);
}
}
void dfs1(int x,int fa,int dep){
int maxson=-1;
siz[x]=1;
deep[x]=dep;
f[x]=fa;
for(int i=head[x];i;i=nex[i]){
int v=to[i];
if(v==fa)continue;
dfs1(v,x,dep+1);
siz[x]+=siz[v];
if(siz[v]>maxson){
maxson=siz[v];
son[x]=v;
}
}
}
void dfs2(int x,int topf){
id[x]=++cnt;
top[x]=topf;
if(son[x])dfs2(son[x],topf);
for(int i=head[x];i;i=nex[i]){
int v=to[i];
if(v==f[x]||v==son[x])continue;
dfs2(v,v);
}
}
void update(int x,int l,int r,int L,int R,int k){
if(L<=l&&r<=R){
sum[x]+=(r-l+1)*k;
tag[x]+=k;
return ;
}
pushdown(x,l,r);
int mid=(l+r)>>1;
if(L<=mid)update(x*2,l,mid,L,R,k);
if(R>mid)update(x*2+1,mid+1,r,L,R,k);
pushup(x);
}
int query(int x,int l,int r,int L,int R){
if(L<=l&&r<=R)return sum[x];
pushdown(x,l,r);
int ans=0;
int mid=(l+r)>>1;
if(L<=mid)ans+=query(x*2,l,mid,L,R);
if(R>mid)ans+=query(x*2+1,mid+1,r,L,R);
return ans;
}
void urange(int x,int y,int k){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
update(1,1,cnt,id[top[x]],id[x],k);
x=f[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
update(1,1,cnt,id[x],id[y],k);
}
int qson(int x){
return query(1,1,cnt,id[x],id[x]+siz[x]-1);
}
int main(){
cin>>s1+1;
insert(s1);
getfail();
dfs1(1,0,1);
cnt=0;
dfs2(1,1);
cin>>m;
while(m--){
int x,y;
cin>>x>>y;
urange(1,a[y],1);
cout<<qson(a[x])<<endl;
urange(1,a[y],-1);
}
return 0;
}