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

int main() {
    string s;
    vector <long long> v;
    long long q, l, r, c;
    cin >> s;
    cin >> q;
    for (int i = 1; i < s.length(); i++) {
        if (s[i-1] > s[i]) v.push_back(i+1);
    }
    for (int i = 0; i < q; i++) {
        cin >> l >> r;
        c = 1;
        for (int j = 0; j < v.size(); j++) {
            if (v[j] > l && v[j] <= r) {
                c++;
            }
            if (v[j] > r) break;
        }
        cout << c << endl;
    }
}
