#include <iostream>
#include <vector>

using namespace std;

int main() {
    string s;
    cin >> s;
    vector <int> v;
    v.resize(s.size() , 0);
    for (int j = 1; j < s.size(); j++)
            if ((s[j - 1] != 'a') and (s[j] == 'a'))
                v[j] = 1;
            else
                if ((s[j - 1] == 'c') and (s[j] == 'b'))
                    v[j] = 1;
    vector <int> pre_sum(s.size()+1);
    int sum = 0;
    for (int i = 0 ; i < s.size();i++)
    {
        pre_sum[i+1] = sum + v[i];
        sum += v[i]; 
    }
    pre_sum[0] = 0;
cout << endl;
    int q;
    cin >> q;
    int l, r;
    int res;
    for (int i = 0; i < q; i++)
    {
        cin >> l >> r;
        res = 0;
        cout << pre_sum[r] - pre_sum[l]+1 << endl;
    }
}
