#include <bits/stdc++.h>
using namespace std;
int a[20000], r1[20000];

int main() {
    string s;
    cin >> s;
    int h = 0;
    for(int i = 0; i < s.size(); i++){
        if(s[i]=='a'){
            a[i]=1;
        }
        else if(s[i]=='b'){
            a[i]=2;
        }
        else{
            a[i]=3;
        }
        if(a[i]<a[i-1] and i!=0){
            r1[h]=i-1;
            h++;
        }
    }
    int n;
    cin >> n;
    for(int i = 0; i<n; i++){
        int l, r;
        cin >> l >> r;
        int m = 0;
        for(int j = 0; j < h; j++){
            if(r-1>r1[j] and l-1<r1[j]){
                m++;
            }
        }
        cout << m+1 << endl;
    }
}
