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

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

        l = 0;
        r = raz - 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 << "\n";
    }
}