#include <bits/stdc++.h>

using namespace std;

const long long MAX = 1e18, MIN = -1e18;
long long mas[10010];
int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    string s;
    char prev = s[0];
    long long q, l = 1, r = 1, h = 1;
    cin >> s >> q;
    for (long long i = 1; i < s.size(); i++)
    {
        if (prev - 'a' <= s[i] - 'a') r++, prev = s[i];
            else
            {
                for (long long i = l; i <= r; i++) mas[i] = h;
                l = i + 1;
                r = i + 1;
                h++;
                prev = s[i];
            }
    }
    if (mas[s.size()] == 0)
        for (long long i = l; i <= r; i++) mas[i] = h;
    for (long long i = 0; i < q; i++)
    {
        cin >> l >> r;
        cout << mas[r] - mas[l] + 1 << endl;
    }
    return 0;
}