#include <bits/stdc++.h>

using namespace std;
long long n, q, l, r, k, a[20007];
string s;
int main()
{
    cin>>s>>q;
    n = s.size();
    k = 1;
    a[1] = 1;
    for(int i = 2; i <= n; i++){
        if(s[i-1] >= s[i-2]) a[i] = k;
        else{
            k++;
            a[i] = k;
        }
    }
    while(q--){
        cin>>l>>r;
        cout<<a[r]-a[l]+1<<endl;
    }
    return 0;
}
