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

int main() {
    int a, b, l, r, m, ind1, ind2;
    string s;
    cin >> s;
    vector <int> pos;
    pos.push_back(1);
    char p = 'a';
    for (int i = 0; i < s.size(); i++) {
        if (s[i] < p)
            pos.push_back(i + 1);
        p = s[i];
    }
    cin >> a;
    while (cin >> a >> b) {
        l = 0;
        r = pos.size() - 1;
        while (l <= r) { 
            m = (l + r) / 2;
            if (pos[m] <= a) {
                if (m == pos.size() - 1) {
                    ind1 = pos.size() - 1;
                    break;
                }
                if (pos[m + 1] > a) {
                    ind1 = m;
                    break;
                }
                l = m + 1;
            } else {
                r = m - 1;
            }
        }

        l = 0;
        r = pos.size() - 1;
        if (b > pos[r])
            ind2 = r + 1;
        else while (l <= r) {
            m = (l + r) / 2;
            if (pos[m] >= b) {
                if (m == 0) {
                    ind2 = 0;
                    break;
                }
                if (pos[m-1] < b) {
                    ind2 = m;
                    break;
                }
                r = m - 1;
            } else {
                l = m + 1;
            }
        }
        //cout << ind1 << " " << ind2 << endl;
        cout << ind2 - ind1 + (pos[ind2] == b) << "\n";
    }
}