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

int main() {
    string c;
    cin>>c;
    long long n;
    cin>>n;
    vector<int>ans;
    vector<int>ch;
    ch.push_back(0);
    for(int i=0;i<c.size()-1;i++){
       if(c[i]>c[i+1]){
           ch.push_back(i+1);
       }
    }
    ch.push_back(c.size());
    /*/for(int i=0;i<ch.size();i++){
        cout<<ch[i]<<" ";
    }/*/
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        //cout<<a<<" "<<b<<" ";
        int a1,b1;
        a1=0;
        b1=0;
        for(int j=0;j<ch.size();j++){
            if(ch[j]>a){
                a1=j;
                break;
            }
        }
        for(int j=a1;j<ch.size();j++){
            if(ch[j]>b){
                b1=j;
                break;
            }
        }
        //cout<<b1<<" "<<a1<<" ";
        ans.push_back(b1-a1+1);
    }
    
    for(int i=0;i<n;i++){
        cout<<ans[i]<<endl;
    }
    
}
