#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;
        e[i] = -k;
    }
    for(int i = 0; i < len; i++)
    {
        e[i]+=k;
    }
    int n,l,r;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> l >> r;
        cout << k - b[l] - e[r] + 1 << endl;
    }
    return 0;
}


