#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("avx2")

using namespace std;

#define x first
#define y second
#define pb push_back
#define all(a) a.begin(),a.end()
#define int long long

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef vector<ll>vll;
typedef vector<int>vi;
typedef vector<bool>vb;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;

const int N=2e4+44,mod=1e9+7;
const char nl='\n';

int a[N],pref[N];

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
    s+='$';
    int cur=1;
    a[1]=1;
    for(int i=1;i<s.size();++i){
        if(s[i]>=s[i-1]){
            a[i+1]=cur;
        }
        else{
            cur++;
            a[i+1]=cur;
        }
    }
    pref[1]=1;
    for(int i=2;i<s.size();++i){
        if(a[i]==a[i-1]){
            pref[i]=pref[i-1];
        }
        else{
            pref[i]=pref[i-1]+1;
        }
    }
    int q;
    cin>>q;
    while(q--){
        int l,r;
        cin>>l>>r;
        cout<<pref[r]-pref[l]+1<<nl;
    }
}
