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

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    int n;
    cin >> n;

    std::vector <int> wordends;
    {
        char c = 'a';
        for (int i = 0; i < s.size(); i++) {
            if (s[i] < c) {
                wordends.push_back(i);
            }
            c = s[i];
        }
        wordends.push_back(s.size() - 1);
    }

    /*int q = 0;
    for (int i = 0; i < s.size(); i++) {
        if (i == wordends[q]) {
            cout << endl; q++;
        }
        cout << s[i];
    }*/

    for (int i = 0; i < n; i++) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        int ans = 0;
        for (int j = 0; j < wordends.size(); j++) {
            if (wordends[j] > l) ans++;
            if (wordends[j] > r) break;
        }
        cout << ans << endl;
    }

    return 0;
}
