#include <iostream>
#include <string>
#include "math.h"

using namespace std;


string predlozhenie;

int countwords(int s, int e)
{
    s--;
    e--;
    int count = 0;
    for(int i=s;i<=e;i++)
    {
        
        char current = predlozhenie[i];
        char next = predlozhenie[i+1];
        
        if(next>=current && (i+1)<=e)
            continue;
        else
        {
            count++;
        }
        
        
    }
    return count;
}


int main()
{
    
    int numberofpar=0;
    int start, end;

    cin>>predlozhenie;
    cin>>numberofpar;
    
    for(int i=0;i<numberofpar;i++)
    {
        cin>>start>>end;
        cout<<countwords(start,end)<<endl;
        
    }
        
 
    return 0;
}