#include <bits/stdc++.h>

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;

#define ff first
#define ss second
#define pb push_back
#define el "\n"
#define se " "

const ll N=1e6+9;
const ll INF=1e18;

ll n,m,a,b,c,d,k,kk,i,j,cnt,cnt1,res,res1,sum,sum1,val,cur;
ll pref[N];
string s;
int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>s;
    pref[1]=1;
    for(i=1;i<s.size();i++){
        if(s[i]>=s[i-1]) pref[i+1]=pref[i];
        else pref[i+1]=pref[i]+1;
    }
//    for(i=1;i<=s.size();i++){
//        cout<<pref[i]<<se;
//    }
//    cout<<el;
    cin>>m;
    while(m--){
        ll l,r;
        cin>>l>>r;
        cout<<pref[r]-pref[l]+1<<el;
    }
}
