#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    string s;
    int m;
    cin>>s;
    cin>>m;
    int n=s.size();
    vector<int> arr={};
    for (int i=0; i<n-1; i++){
        if (s[i]=='c'){
            if (s[i+1]=='a' || s[i+1]=='b'){
                arr.push_back(i);
            }
        }
        else if (s[i]=='b' && s[i+1]=='a'){
            arr.push_back(i);
        }
    }
    arr.push_back(n-1);
    int a,b;
    for (int i=0; i<m; i++){
        cin>>a>>b;
        a--;
        b--;
        vector<int>::iterator it=lower_bound(arr.begin(), arr.end(), a);
        int ind1=it-arr.begin();
        it=lower_bound(arr.begin(), arr.end(), b);
        int ind2=it-arr.begin();
        int sas=ind2-ind1+1;
        cout<<sas<<endl;
    }
}

