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

#define DIM 20007
#define fi first
#define se second


typedef long long ll;

ll t, c, a[DIM], n, res, x, y, b, k, x1, x2, m, res1, res2, q, p[DIM];
string s;


int main(){
    
    
    cin>>s;
    n = s.length();
    
    for(int i=1;i<n;i++){
        if(s[i]<s[i-1]){
            a[i] = 1;
        }
        p[i] = p[i-1]+a[i];
    }
    
    cin>>q;
    for(int i=1;i<=q;i++){
        cin>>x>>y;
        if(x==1){
            cout<<p[y-1] + 1<<endl;
        }
        else cout<<p[y-1]-p[x-1] + 1<<endl;
    }
    
    return 0;
}

