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

struct word{
    int l,r;
    word(int l, int r):l(l),r(r){

    }
    word(){}
    void print(){
        std::cout<<l<<" "<<r<<std::endl;
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	std::string s;
    std::cin>>s;

    int n = s.length();
    vector<word> w;

    for(int i = 0;i<n;){
        int l = i;
        int r;
        if(s[l+1]>=s[l]&& l+1<n){
            r = l+1;
            while(s[r]>=s[r-1]&&r<n){
                ++r;
            }
            i = r;
            --r;
        }
        else{
            r = l;
            ++i;
        }
        word a(l,r);
        w.push_back(a);
    }

    int  k = w.size();
    int q;
    std::cin>>q;
    int answers[q];
    for(int i = 0;i<q;++i){
        int l1,r1;
        std::cin>>l1>>r1;
        l1--;
        --r1;
        int il = 0;
        for(;w[il].l<l1;++il);
        int ir = il;
        for(;w[ir].r<=r1;++ir);
        --ir;
        bool left = 0;
        bool right=0;
        if(il>0){
            left = w[il-1].r >= l1 ;
        }
        if(ir<k-1){
            right = w[ir+1].l <= r1 ;
        }
        int dif = ir - il + 1 + left+right;
        /*w[il].print();
        w[ir].print();
        std::cout<< w[il-1].r<<" "<<l1<<" "<<il<<std::endl;
        std::cout<< w[ir+1].l<<" "<<r1<<" "<<ir<<std::endl;*/
        answers[i] = dif;

    }
    /*for(auto it = w.begin();it!=w.end();++it){
        it->print();
    }*/

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

}
