#include<bits/stdc++.h>
#include<stdlib.h>
#define pb push_back
#define ff first
#define ss second
#define int long long
#define let long long
//#define mp make_pair
#define cin_arr(arr,n) for (int i=0; i<n;i++) cin>>arr[i];
#define cout_arr(arr,n) for (int i=0; i<n;i++) cout<<arr[i]<<" ";
#define cout_set(st) for (auto i:st) cout<<i<<" ";
#define endl '\n'
using namespace std;

int32_t main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

    string s;
    cin>>s;
    bool arr[s.size()]={0};
    for (int i=1; i<s.size();i++) {
        if (s[i]<s[i-1]) arr[i]=1;
    }
    int pref[s.size()]={0};
    for (int i=1; i<s.size();i++) {
        pref[i]=pref[i-1]+arr[i];
    }
    int q;
    cin>>q;
    while (q--) {
        int l,r;
        cin>>l>>r;
        r--;l--;
        cout<<pref[r]-pref[l]+1<<endl;
//        for (int i=l;i<=r;i++) cout<<pref[i];
//        cout<<endl;
//        for (int i=l; i<=r;i++) cout<<s[i];
//        cout<<endl<<endl;
    }
}

