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

using namespace std;

#define pb push_back
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define F first
#define S second
#define rs resize

using ll = long long;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using ppi = pair<int, pair<int, int>>;
using ppl = pair<ll, pair<ll, ll>>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
using vvpi = vector<vpi>;
using vpl = vector<pl>;
using vvl = vector<vl>;
using vvpl = vector<vpl>;
using vppl = vector<ppl>;

void solve()
{
    str s;
    cin>>s;
    int n=s.size();
    vl pref(n);
    vl nd;
    for(int i=0;i<n;i++){
        if(i==0){
            pref[i]++;
        }
        else{
            pref[i]=pref[i-1];
            if(s[i]<s[i-1]){
                pref[i]++;
                nd.pb(i-1);
            }
        }
    }
    nd.pb(n-1);
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        int l, r;
        cin>>l>>r;
        l--, r--;
        ll kol=pref[r];
        if(l>0){
            kol-=pref[l-1];
            int tp=*lower_bound(all(nd), l-1);
            if(tp!=l-1){
                kol++;
            }
        }
        cout<<kol<<endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while (t--)
        solve();

}
/*

*/
