#include <iostream>

using namespace std;

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

void read_and_transform(){
    cin >> s;
    long long 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){
    long long prevnum = 4;
    for (long long i = l[test_case] - 1; i < r[test_case]; ++i) {
        if (b[i] < prevnum) words[test_case]++;
        prevnum = b[i];
    }
}

int main() {
    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;
}