#include <bits/stdc++.h>

using namespace std;

constexpr int BLOCK = 128;

#define CEIL(a, b) ((((a)%(b)) == 0) ? ((a)/(b)) : (((a)/(b))+1))

int main() {
    assert(CEIL(5, 3) == 2);
    assert(CEIL(4, 2) == 2);
    assert(CEIL(0, 3) == 0);

    string s; cin >> s;
    int q; cin >> q;

    vector<int> blocks,prev;

    int cc = 1, p = 0, pp = 0;
    for(int i = 1;i<s.size();i++) {
        if(i%BLOCK == 0){
            blocks.emplace_back(cc);
            prev.emplace_back(pp);
            pp = p;
            cc = 0;
            //cc = (s[i] >= s[i-1]);
        }

        if(s[i] < s[i-1]) {
            cc++;
            p = i;
        }
    }

    blocks.emplace_back(cc);
    prev.emplace_back(pp);

    vector<int> answers(q);

    for(int i = 0;i<q;i++){
        int l, r, c = 0; cin >> l >> r; l--, r--;

        if(r-l+1 <= BLOCK){
            c = 1;
            for(++l;l<=r;l++){
                if(s[l] < s[l-1])
                    c++;
            }
        } else {
            if(l%BLOCK != 0){
                c = 1;
                for(++l;l <= r && l%BLOCK != 0;l++){
                    if(s[l] < s[l-1])
                        c++;
                }
            }

            if(l <= r){
                int j = l/BLOCK;
                for(;(j+1)*BLOCK-1<=r;j++){
                    c += blocks[j];
                }

                l = j*BLOCK;
                for(;l<=r;l++){
                    if(s[l] < s[l-1])
                        c++;
                }
            }
        }

        answers[i] = c;
    }

    for(int i = 0;i<q;i++){
        cout << answers[i] << endl;
    }

    return 0;
}