#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;
    long long n = 0; 
    vector <int> c;
    long long cnt = 0;
    while (cnt < s.size()) {
        long long numm = 0;
        long long i = cnt + 1;
        while (int(s[i]) >= int(s[i-1])) {
            i++;
            numm++;
        }
        c.push_back(numm + 1);
        cnt += numm + 1;
    }
    //for (long long i = 0; i < c.size(); i++) cout << c[i] << endl;
    cin >> n;
    while (n--) {
        long long l, r;
        cin >> l >> r;
        l = l - 1;
        r = s.size() - r;
        //cout << l << " " << r << endl;
        long long kol = c.size();
        long long i = 0;
        while (l > 0) {
            if (l - c[i] >= 0) {
                l -= c[i];
                i++;
                kol--;
            } else {
                l = 0;
            }
        }
        i = c.size() - 1;
        while (r > 0) {
            if (r - c[i] >= 0) {
                r-= c[i];
                i--;
                kol--;
            } else {
                r = 0;
            }
        }
        cout << kol << endl;
    }
}
