#include<bits/stdc++.h>
#define ll long long
using namespace std;

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 = "";
    vector <ll> vcStart;
    vector <ll> vcEnd;
    vcStart.push_back(1);

    for(int i = 0; i < sentence.length()-1;i++){
    if(sentence[i + 1 ] < sentence[i] && sentence[i+1] != sentence[i]){
        ll numberOfStartOfTheWord = i+2;
        ll numberOfEndOfTheWord = i+1;
        vcEnd.push_back(numberOfEndOfTheWord);
        vcStart.push_back(numberOfStartOfTheWord);
    }

}
vcEnd.push_back(sentence.length());

    for(int i = 0; i < q; i++){
        ll Start, End; cin >> Start >> End;
        ll temp = 0;
       for(int j = 0; j < vcStart.size(); j++) if((vcStart[j] >= Start && vcStart[j]<=End) || (vcEnd[j] >=Start && vcEnd[j] <=End)) temp++;

        result+= to_string(temp) + '\n';
            }
    cout << result;

return 0;
}


