#include <bits/stdc++.h>

using namespace std;

#define DIM 20007

string s;

long long n, p[DIM], q, l, r, ans;

int main(){
    cin>>s;
    n=s.length();
    s='d'+s;
    p[0]=0;
    for(int i=1; i<=n; i++){
        p[i]=p[i-1];
        if(s[i]<s[i-1]) p[i]++;
    }
    cin>>q;
    while(q--){
        cin>>l>>r;
        ans=p[r]-p[l-1]+1;
        if(p[l]==p[l-1]+1) ans--;
        cout<<ans<<endl;
    }
    return 0;
}
