#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fst first
#define snd second
#define pb push_back
#define forn(i,a,b) for(int i = a; i<b; i++)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename T>
using iset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#ifdef D
#define debug(x) cout<<x
#else
#define debug(x) //nothing
#endif
const int MAXN = 1000+5;
const int MAXP = 8;
string color;
ll n,q;
struct State{
ll azul=0;
ll rojo=0;
ll razul = 0;
ll rrojo = 0;
State(ll azul=0, ll rojo=0, ll razul = 0, ll rrojo = 0):azul(azul),rojo(rojo), razul(razul), rrojo(rrojo){}
};
State blift[MAXN][MAXP];
State merge(State a, State b){
State neww = State();
debug("Mergeando "<<a.azul<<" "<<a.rojo<<" "<<b.azul<<" "<<b.rojo<<'\n');
neww=a;
ll aux=min(a.azul,b.rrojo);
neww.azul=a.azul-aux;
neww.rojo=b.rrojo;
neww.rrojo=a.rrojo+b.rrojo-aux;
aux=min(a.rojo,b.razul);
neww.rojo+=a.rojo-aux;
neww.azul+=b.razul;
neww.razul=a.razul+b.razul-aux;
debug("= "<<neww.azul<<" "<<neww.rojo<<'\n');
return neww;
}
void precalc(){
forn(i,0,n) blift[i][0]=(color[i]=='P'?State(1,0,1,0):State(0,1,0,1));
forn(k,1,MAXP){
forn(i,0,n){
blift[i][k]=merge((i+(1<<(k-1))<n?blift[i+(1<<(k-1))][k-1]:State()), blift[i][k-1]);
debug(" State "<<i<<" "<<(1<<k)<<" "<<blift[i][k].azul<<" "<<blift[i][k].rojo<<" "<<blift[i][k].razul<<" "<<blift[i][k].rrojo<<'\n');
}
}
}
int main(){ cin>>n>>q;
cin>>color;
precalc();
forn(i,0,q){
ll l,r; cin>>l>>r;
l--; r--;
State res = State();
for(int k = MAXP-1; k>=0; k--){
if(l+(1<<k)-1<=r){
debug("haciendo "<<l<<" "<<(1<<k)<<'\n');
res=merge(blift[l][k],res);
l+=(1<<k);
}
if(l-1==r) break;
}
cout<<res.razul+res.rrojo<<'\n';
}
return 0;
}