#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll countMinimalNumber(string str){
ll counter = 1;
for(int i = 0; i < str.length()-1;i++){
    if(str[i + 1 ] < str[i] && str[i+1] != str[i]){
        counter++;
    }
}
return counter;

}
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    string sentence; cin >>sentence;
    ll q; cin>>q;
    string result = "";
    for(int i = 0; i < q; i++){
        ll Start, End; cin >> Start >> End;
        Start -=1;
        End -=1;
        result+= to_string(countMinimalNumber(sentence.substr(Start, End-Start+1))) + '\n';
            }
    cout << result;
return 0;
}


