#include <bits/stdc++.h>
using namespace std;
struct nd{
int l,r,sum,ans;
void assign(int x){
l=max(0,x);
r=max(0,x);
sum=x;
ans=max(0,x);
}
nd operator+(nd other){
return {
max(l,sum+other.l),
max(other.r,other.sum+r),
sum+other.sum,
max({ans,other.ans,r+other.l})
};
}
};
constexpr int maxn=5e5 + 5;
nd tree[4*maxn];
int len=1;
nd rget(int v,int cs,int ce,int ls,int le){
if (cs>=ls && ce<=le) return tree[v];
if (cs>le || ce<ls) return {0,0,0,0};
int mid=(cs+ce)/2;
return rget(2*v,cs,mid,ls,le)+rget(2*v+1,mid+1,ce,ls,le);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q;
string s;
cin>>n>>s>>q;
while (len<n) len<<=1;
for (int i=0;i<n;i++) tree[i+len].assign(s[i]=='C' ? 1 : -1);
for (int i=len-1;i>=1;i--) tree[i]=tree[2*i]+tree[2*i+1];
while (q--){
int l,r;
cin>>l>>r;
l--; r--;
nd ans=rget(1,0,len-1,l,r);
cout<<max(0,ans.ans-ans.sum)<<"\n";
}
return 0;
}