#include <bits/stdc++.h>

using namespace std;

const long long MAX = 1e18, MIN = -1e18;
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;
    cin >> s >> q;
    vector <pair <long long, long long>> v;
    for (long long i = 1; i < s.size(); i++)
    {
        if (prev - 'a' <= s[i] - 'a') r++, prev = s[i];
            else
            {
                v.push_back({l, r});
                l = i + 1;
                r = i + 1;
                prev = s[i];
            }
    }
    if (v.size() == 0) v.push_back({l, r});
        else if (v[v.size() - 1].second != s.size()) v.push_back({l, r});
    for (long long i = 0; i < q; i++)
    {
        long long a = 0, b = 0;
        cin >> l >> r;
        for (long long i = 0; i < v.size(); i++)
        {
            if (v[i].first <= l && v[i].second >= l) a = i;
            if (v[i].first <= r && v[i].second >= r) b = i + 1;
        }
        cout << b - a << endl;
    }
    return 0;
}
