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

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

    string s;
    cin >> s;

	int q;
	cin >> q;

    int words = 0;
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l;
        cin >> r;
        for (int j = l; j < r; ++j) {
            words += ((int)s[j-1] > (int)s[j]);
        }
        cout << words+1 << endl;
        words = 0;
    }
}
