#include <bits/stdc++.h>
#define ll long long
#define ff first
#define pb push_back
#define ss second
#define endl '\n'
#define y1 y6
#define j1 j6
#define rr return 0;
using namespace std;
ll n,i,o,d,m,j,x,y;
const ll INF=1e9;
ll a[1000007];
ll dp[1000007];
ll c[1000007];
int main()
{
//    freopen("loganqueries.in","r",stdin);
//    freopen("loganqueries.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
    n=s.size();
    ll A[n];
    A[0]=1;
    for(i=1;i<n;i++)
    {
        if(s[i]>=s[i-1]) {A[i]=A[i-1];}
        else {A[i]=A[i-1]+1;}
    }
    ll q;
    cin>>q;
    while(q--)
    {
        ll l,r;
        cin>>l>>r;
        l--;
        r--;
        x=A[r]-A[l]+1;
        cout<<x<<endl;
    }
    return 0;
}
