#include <bits/stdc++.h>

using namespace std;
#define ll long long
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    string s;
    ll i,a[100005]={0},t;
    cin>>s;


    s=" "+s;
    t=s[1]-'a';
    for(i=2;i<=s.size();i++)
    {
        ll z=s[i]-'a';
        if(t>z)
        {
            a[i-1]=1;
            t=z;
        }
        else
            t=z;
    }

    ll e;
    cin>>e;

    for(int o=1;o<=e;o++)
    {
        ll l,r,s=0;
        cin>>l>>r;

        for(i=l;i<=r;i++)
        {
            s+=a[i];

        }

        if(a[r]==0) s++;

        cout<<s<<"\n";
    }
}
