#include <bits/stdc++.h>

using namespace std;

#define DIM 100007

long long n, a, c, i, q, l, r;

string s;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

cin>>s;


cin>>q;
while(q--){
    cin>>l>>r;
    c = 0;
    for(i=l-1; i<r; i++){
        if(s[i]>s[i+1]) c++;
    }
    cout<<c<<endl;
}

    return 0;
}
