#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 q;
    std::cin>>q;
    for(int i = 0;i<q;++i){
        int l1,r1;
        std::cin>>l1>>r1;
        --l1;
        --r1;
        int il = 0;
        for(;l1<w[il].l;++il){}
        int ir = il;
        for(;r1>w[ir].r;++ir){}
        int dif = ir-il+(w[il-1].r>l1)+(w[ir+1].l<r1);
        /*w[il].print();
        w[ir].print();*/
        
        std::cout<<dif<<std::endl;
    }

}
