#include <iostream>

using namespace std;

int main()
{
    string R;
    cin>>R;
    int N,k=0,K[100000]={0};
    cin>>N;
    for (int i=1; i<R.length(); i++)
        if (R[i-1]>R[i]) {
            K[k]=i;
            k++;
        }
    K[k]=R.length();
    for (int i=0; i<N; i++) {
        int a,b,j=0,kil=0;
        cin>>a>>b;
        while (b>K[j]) {
            if (K[j]>=a) kil++;
            j++;
        }
        cout<<kil+1<<endl;
    }
}
