#include <iostream>

using namespace std;

int main()
{
    string s;
    long int q;
    long int l,r;
    cin >> s;

    cin >> q;

    for (long int i=1;i<=q;i++)
    {
        cin >> l >> r;

        long int j = l;
        long int k = 0;
        //cout << j << " ";
        while (j<=r)
        {
            while ((j<=r) && (s[j-1]<=s[j]))
            {
               // cout << j << " ";
                j++;
            }

             k++;
             j++;

        }
        cout << k << endl;
    }
}
