#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    char o;
    long long n, i, j, l, r, ans=0;
    cin>>s>>n;
    long long a[n];
    for (i=0; i<n; i++)
    {
        ans=0;
        cin>>l>>r;
        o=s[r];
        s[r]='0';
        for (j=l-1; j<r; j++)
        {
            if (s[j]>s[j+1])
            {
                ans+=1;
                //cout<<s[j]<<" "<<s[j+1]<<endl;
            }
        }
        s[r]=o;
        //cout<<endl;
        a[i]=ans;
    }
    for (i=0; i<n; i++)
    {
        cout<<a[i]<<endl;
    }
}
