#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> b;

int main() {
    string s;
    int m, c, d;
    vector<b> a;
    int cnt=0;
    getline(cin, s);
    s.push_back('a');
    int n=s.length();
    int start=1;
    char prev=s[0];
    for(int i=1; i<=n-1; i++){
        if(int(s[i])<int(prev)){
            a.push_back({start, i});
            start=i+1;
        }
        prev=s[i];

    }

    cin>>m;
    int res=0;
    for(int i=1; i<=m; i++){
        cin>>c>>d;
        for(auto x: a){
            if(x.first>=c and x.second<=d){
                res+=1;
            }

        }
        cout<<res<<endl;
        res=0;
    }




}