#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
    int a[s.size()+1], k=0;
    for (int i=1; i<=s.size(); i++)
    {
        a[i]=k;
        if (s[i-1]>s[i])
        {
            k++;
        }
        //cout<<a[i]<<" ";
    }
    //cout<<endl;
    int l, r;
    int q;
    cin>>q;
    for (int i=1; i<=q; i++)
    {
        cin>>l>>r;
        if(a[r]!=a[l])
        cout<<a[r]-a[l]+1<<endl;
        else cout<<1<<endl;
    }
}

