#include <iostream>
#include <vector>
#include <cmath>
#include <map>

using namespace std;

#define int long long


signed main() {
    string s;
    cin >> s;
    vector<int> v(s.size(), 0);
    for (int i = 1; i < s.size(); i++) {
        if (s[i - 1] > s[i])
            v[i - 1] = 1;
    }
    vector<int> pref(s.size() + 1);
    pref[0] = 0;
    for (int i = 0; i < s.size(); i++)
        pref[i + 1] = pref[i] + v[i];

    int q;
    cin >> q;
    while (q--) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        cout<<pref[b]-pref[a]+1<<endl;
    }
    return 0;
}
