#include <iostream>

using namespace std;

int main()
{
    int n,x,y,k;
    string s;
    cin >> s;
    cin >> n;
    int a[1000];
    for (int j=2; j<=s.size(); ++j)
        {
            a[j]=0;
            if (s[j-1]>s[j])
            {
                a[j]=1;
            }
        }
   /* for (int i=1; i<=s.size(); ++i)
    {
        cout << a[i] << " ";
    }
    cout << endl;*/
    for (int i=1; i<=n; ++i)
    {
        k=0;
        cin >> x >> y;
        for (int j=x; j<=y; ++j)
        {
            k=k+a[j];
        }
        if (a[y]==0 && y!=s.size())
            k=k+1;
        cout << k << endl;
    }

    return 0;
}
