#include <bits/stdc++.h>

using namespace std;

constexpr int BLOCK = 128;

int main() {
    string s; cin >> s;
    int q; cin >> q;

    set<int> terms = {0};
    for(int i = 1;i<s.size();i++){
        if(s[i] < s[i-1])
            terms.emplace(i);
    }

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

        auto it = terms.lower_bound(l);

        if(*it > l)
            it--;

        int c = 0;
        for(;it != terms.end() && *it <= r;it++){
            c++;
        }

        cout << c << endl;
    }

    return 0;
}