#include <bits/stdc++.h>

using namespace std;

string s;
int q, l, r;

int magic(int l, int r) {
    int x = 1;
    string word;
    for (int i = l+1; i < r+1; i++) {
        // cout << s[i] << " " << s[i-1] << endl;
        if (s[i] == s[i-1] || s[i] > s[i-1]) {
            continue;
        }
        if (s[i] < s[i-1]) {x++;}
    }

    return x;
}

int main () {
    cin >> s >> q;
    for (int i = 1; i <= q; i++) {
        cin >> l >> r;
        cout << magic(--l, --r) << endl;
    }
    return 0;
}