#include <iostream>

using namespace std;

string str;

void reBuild() {
    for(int i = 0; i < str.size(); i++) {
        if(str[i] == 'a')
            str[i] = '1';
        else if(str[i] == 'b')
            str[i] = '2';
        else
            str[i] = '3';
    }
}

int main() {
    int k;
    cin >> str >> k;
    reBuild();
    while(k != 0) {
        int n1, n2, sum=0;
        cin >> n1 >> n2;
        n1--;

        for(; n1 < n2; n1++) {
            if(str[n1] - '0' > str[n1+1]- '0')
                sum++;
            else if(n1+1 == n2)
                sum++;
        }
        cout << sum << '\n';
        k--;
    }
}