#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=101000;
int n,q;
char s[N];
struct node {
int pf,sf,s,f;
}nd[4*N];
node comb(node x,node y) {
node ret;
ret.pf=max(x.pf,x.s+y.pf);
ret.sf=max(y.sf,y.s+x.sf);
ret.s=x.s+y.s;
ret.f=max({x.f+y.s,x.s+y.f,x.pf+y.sf});
return ret;
}
void build(int p,int l,int r) {
if (l==r) {
if (s[l]=='C') {
nd[p].pf=nd[p].sf=nd[p].s=nd[p].f=1;
} else {
nd[p].pf=nd[p].sf=nd[p].s=nd[p].f=0;
}
} else {
int md=l+r>>1;
build(p+p,l,md);
build(p+p+1,md+1,r);
nd[p]=comb(nd[p+p],nd[p+p+1]);
}
}
node get(int p,int l,int r,int ql,int qr) {
if (ql>r||qr<l) return {0,0,0,0};
if (ql<=l&&r<=qr) return nd[p];
int md=l+r>>1;
return comb(get(p+p,l,md,ql,qr),get(p+p+1,md+1,r,ql,qr));
}
int main() {
scanf("%d",&n);
scanf("%s",s+1);
scanf("%d",&q);
build(1,1,n);
while (q--) {
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",r-l+1-get(1,1,n,l,r).f);
}
}