#include <bits/stdc++.h>

#define itn long long
#define int long long
//#define double long double
//#define endl '\n'
#define p_b push_back
#define fi first
#define se second
#define pii pair<int, int>
#define oo LLONG_MAX
#define elif else if

using namespace std;

int input()
{
    int x;
    cin>>x;
    return x;
}


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

//        freopen("divisor.dat", "r", stdin);
//        freopen("prpr.txt", "w", stdout);

    string s;
    cin>>s;
    itn n=s.size();
    int pr[n];
    pr[0]=0;
    for(int i=1;i<n;i++)
    {
        pr[i]=pr[i-1];
        if(s[i-1]>s[i])
            pr[i]++;
    }
    int q;
    cin>>q;
    while(q--)
    {
        int l, r;
        cin>>l>>r;
        r--;
        l--;
        cout<<pr[r]-pr[max(0ll, l)]+1;
        cout<<endl;
    }


}