#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
    vector <ll> v;
    ll n=s.size(),lng=0;
    char last='0';
    for (ll i=0;i<n;i++){
        if (s[i]>=last) {
            last=s[i];
            lng++;
        } else {
            v.push_back(lng);
            lng=1;
            last=s[i];
        }
    }
    v.push_back(lng);ll sm=0,k=0;
    ll pref[200010];
    for (ll i=0;i<v.size();i++){
        sm++;
        for (ll j=k;j<k+v[i];j++){
            pref[j]=sm;
        }
        k+=v[i];
    }
    ll q,l,r;
    cin>>q;
    for (ll i=0;i<q;i++){
        cin>>l>>r;
        r--;
        cout<<pref[r]-pref[l]+1<<endl;
    }
}
