#include <bits/stdc++.h>
using namespace std;
const long long MOD=10e9+7;
#define Std 100003
#define StdD 100007
#define StdINF 1000000007
#define endl "\n"
#define ll long long
#define ull unsigned long long
#define ld long double
#define st string
#define ch char
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define sl set<ll>
ll n,sum,L,R,res;
ll k[Std];
st s;
int poshyk()
{
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>s[i+1])
        {
            res++;
        }
        k[i]+=res;
    }
}
void Start()
{
    cin>>s;
    cin>>n;
    res=0;
    poshyk();
    for(int i=1;i<=n;i++)
        {
            cin>>L>>R;
            cout<<abs(k[L-2]-k[R-2])+1<<endl;
        }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Start();
    return 0;

}
