#include <bits/stdc++.h>
using namespace std;

vector <pair <int, int> > words;

void getWords(string str)
{
    int i = 0;
    for (int j = 1; j < str.size(); ++j)
        if (str[j] < str[j - 1])
        {
            words.push_back({ i + 1, j });
            i = j;
        }
    words.push_back({ i + 1, str.size() });
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	string s;
    cin >> s;
    getWords(s);
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i)
    {
        int l, r, start = -1, end = -1;
        cin >> l >> r;
        for (int j = 0; end == -1; ++j)
        {
            if (l >= words[j].first)
                start = j;
            if (r <= words[j].second)
                end = j;
        }
        cout << end - start + 1 << '\n';
    }
}
