#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
using namespace std;
ll ch=1e18;
ll a,b,c,d,x,n,n1,n2,fl1,fl2,o1,o2,flag,flag1,flag2,g,m,m1,m2,i,j,lr,f,k,l,r,y,o,p,mx=-ch,mx1=-ch,mx2=-ch,q,q1,q2,q3,q4,mn=ch,mn1=ch,mn2=ch,f1,f2,pos,ans,sum,sz=1,MOD=1e9+7;
string s;
ll A[200005];
ll B[200005];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>s;
    for(i=0;i<s.size();i++)
    {
        if (i!=0&&s[i]<s[i-1]) f++;
        B[i]=f;
    }
    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        l--;r--;
        cout<<B[r]-B[l]+1<<"\n";
    }
}
