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

int main() {
    string s;
    cin>>s;
    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;
    int q;
    cin>>q;
    while (q--){
        int l1, r1;
        cin>>l1>>r1;
        int result = 0;
        for (int i = 0; i<count; i+=2){
            int l2 = l[i];
            int r2 = l[i+1];
            if (l1 <= l2 && l2 <= r1 || l1<=r2 && r2<=r1){
                result++;
            }
        }
        cout<<result<<endl;
    }
}