#include <iostream>
#include <map>
using namespace std;
int main()
{
    string sent;
    cin>>sent;
    int n,start,end,max,res;
    map<char,int> map1;
    map1.insert ( std::pair<char,int>('a',1) );
    map1.insert ( std::pair<char,int>('b',2) );
    map1.insert ( std::pair<char,int>('c',3) );
    cin>>n;
    for (int i=0;i<n;++i){
        cin>>start>>end;
        --start;
        max=0;
        res=1;
        for(int j=start; j<end; ++j){
            if(map1[sent[j]]>=max)
                max=map1[sent[j]];
            else{
                ++res;
                max=map1[sent[j]];
            }
        }
        cout<<res<<endl;
    }
    return 0;
}