#include <bits/stdc++.h>
using namespace std;
int main() {
    string line;
    cin >> line;
    //cout << line << endl;
    char letters[line.size()];
    strcpy(letters, line.c_str());
    vector<short> begin;
    vector<short> end;
    begin.push_back(0);
    for(short i = 1; i < line.size(); i++)
        if (letters[i] < letters[i - 1]) {
            begin.push_back(i);
            end.push_back(i - 1);
        }
    end.push_back(line.size() - 1);
    //for (int i = 0; i < begin.size(); i++)
        //cout << begin[i] << ' ' << end[i] << endl;
    int q;
    cin >> q;
    //cout << q << endl;
    for (int i = 0; i < q; i++) {
        int l, s, b, e, j = 0;
        cin >> l >> s;
        l--;
        s--;
        while (l > begin[j])
            j++;
        b = j - 1;
        if (l == begin[j])
            b++;
        j--;
        while (s > end[j])
            j++;
        e = j;
        cout << e - b + 1 << endl;
    }
}