#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi 3.14159265358979323846
#define sqr(a) (a)*(a)
#define v vector
#define ll long long
#define ld long double
#define f first
#define sec second
#define pb push_back
#define pf push_front
#define umap unordered_map
#define sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define mod 1000000007
#define inf 2*1e18

using namespace std;
using namespace __gnu_pbds;

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>uset;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    srand(time(NULL));
    string s;
    ll n,test,i,l,r;
    cin>>s>>test;
    s=" "+s;
    n=s.length();
    v<ll>pref(n);
    pref[1]=1;
    for (i=2;i<n;i++){
        pref[i]=pref[i-1];
        if (int(s[i])<int(s[i-1])){
            pref[i]++;
        }
    }
    while (test--){
        cin>>l>>r;
        if (pref[l]==pref[l-1]){
            cout<<pref[r]-pref[l-1]+1<<endl;
        }else{
            cout<<pref[r]-pref[l-1]<<endl;
        }
    }
    return 0;
}
