#include <iostream>

using namespace std;

int b[20008];
string s;
int n, words[100008];
int r[100008], l[100008];

void read_and_transform(){
    cin >> s;
    int i = 0;
    for (auto a : s) {
        if (a == 'a') b[i] = 1;
        if (a == 'b') b[i] = 2;
        if (a == 'c') b[i] = 3;
        i++;
    }

}

void read(){
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> l[i] >> r[i];
    }

}

void todo(int test_case){
    int prevnum = 4;
    for (int i = l[test_case] - 1; i < r[test_case]; ++i) {
        if (b[i] < prevnum) words[test_case]++;
        prevnum = b[i];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    read_and_transform();
    read();

    for (int i = 0; i < n; ++i) {
        todo(i);
    }

    for (int i = 0; i < n; ++i) {
        cout << words[i] << endl;
    }


    return 0;
}