#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    string s;
    vector < int > b,e;
    cin >> s;
    int len = s.length(),
        k = 0;
    b.resize(len);
    e.resize(len);
    bool started = false;
    for(int i = 0; i < len; i++)
    {
        if(started)
        {
            if(s[i] < s[i - 1])
            {
                started = false;
                k++;
            }
        } 
        else
        {
            started = true;
        }
        b[i] = k;
        //if(i == len - 1 || s[i] > s[i + 1])
        //    e[i] = -k;
        //else
        //{
            e[i] = -(k + 1);
        //    cout << i << endl;
        //}
    }
    k++;
    for(int i = 0; i < len; i++)
    {
        e[i]+=k;
    }
    int n,l,r;
    //cout << k;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> l >> r;
        //if(b[l] != e[r])
        //    cout << k - b[l] - e[r] + 1 << endl;
        //else
        cout << k - b[l-1] - e[r-1] << endl; //<< " k = " << k << " b[l] = " << b[l] << " e[r] = " << e[r] << endl;
    }
    return 0;
}
