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

int intersection(int x1, int y1, int x2, int y2){
    // [x1, y1], [x2, y2]
    if (x1<=x2 && x2<=y1 || x1<=y2 && y2<=y1){
        return 1;
    }
    else {
        return 0;
    }
}

int main() {
    string s;
    cin>>s;
    int q;
    cin>>q;
    if (q<=60000){
        while (q--){
            int l, r;
            cin>>l>>r;
            if (l==r){
                cout<<1<<endl;
            }
            else {
                int result = 1;
                for (int i = l; i<r; i++){
                    if (s[i] < s[i-1]){
                        result++;
                    }
                }
                cout<<result<<endl;
            }
        }
    }
    else {
        int l[100000];
        l[0] = 1;
        int count = 1;
        for (int i = 0; i<s.length(); i++){
            if (s[i] < s[i-1]){
                l[count] = i;
                l[count+1] = i + 1;
                count+=2;
            }
        }
        l[count] = s.length() + 1;
        
        while (q--){
            int l1, r1;
            cin>>l1>>r1;
            int result = 0;
            //[l1, r1], [l[count], l[count+1]]
            for (int i = 0; i<=count; i+=2){
                if (intersection(l1, r1, l[i], l[i+1])==1){
                    result++;
                }
                else if (l[i]>r1){
                    break;
                }
            }
            cout<<result<<endl;
        }
    }
}