#pragma GCC Optimize("O3")
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<ll,ll>pairll;
typedef long double ld;

#define pb push_back
#define fr first
#define sc second
#define INF 1000000000000000007
#define MOD 1000000007
#define endl '\n'

ll n,d[20007];

string s;

void A(ll x){
    for(int i=x;i<=n;i+=(i&-i)){
        d[i]+=1;
    }
    return ;
}

ll R(ll x){
    ll res=0;
    for(int i=x;i>0;i-=(i&-i)){
        res+=d[i];
    }
    return res;
}

int main() {

    cin>>s;
    n=s.length();
    s=" "+s;
    for(int i=1;i<n;i++){
        if(s[i]>s[i+1])A(i);
    }
    ll q;
    cin>>q;
    for(int i=1;i<=q;i++){
        ll l,r;
        cin>>l>>r;
        cout<<R(r-1)-R(l-1)+1<<endl;
    }
    
    return 0;

}
