#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	string str;
	cin >> str;
    int ln = str.length();
    char ch[20000];
    int a[20000];
    strcpy(ch, str.c_str());
    for (int i = 0; i < ln; i++) {
        a[i] = (int) ch[i] - (int) 'a';
    }
    vector<int> r;
    for (int i = 1; i < ln; i++) {
        if (a[i] < a[i - 1]) {
            r.push_back(i);
        }
    }
    int q;
    int k;
    int fr;
    int sc;
    cin >> q;
    for (int z = 0; z < q; z++) {
        k = 1;
        cin >> fr >> sc;
        for (int i = 0; i < r.size(); i++) {
            if ((r[i] >= fr) && (r[i] + 1 <= sc)) {
                k += 1;
            }
        }
        cout << k << "\n";
    }
}
