#include <iostream>
#include <vector>

int main()
{
    std::string s;
    std::cin >> s;
    std::vector<int> v;
    v.push_back(0);
    for (int i = 1; i < (int)s.size(); i++)
        if (s[i] < s[i - 1]) v.push_back(i);
    v.push_back(s.size());
    int pref[s.size()] = {};
    int i = 0, d = 1;
    while (i < (int)s.size())
    {
        while (i < v[d])
        {
            pref[i] = d;
            i++;
        }
        d++;
    }
    int q;
    scanf("%d", &q);
    for (int t = 0; t < q; t++)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        l--, r--;
        if (pref[l] > pref[l - 1]) printf("%d\n", pref[r] - pref[l - 1]);
        else printf("%d\n", pref[r] - pref[l - 1] + 1);
    }
    return 0;
}
