#include <bits/stdc++.h>
using namespace std;
int arr[20004];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s;
    cin >> s;
    arr[0] = 1;
    for (int i = 1; i < s.size(); i++) {
        if (s[i] - 'a' >= s[i - 1] - 'a') {
            arr[i] = arr[i - 1];
        }
        else {
            arr[i] = arr[i - 1] + 1;
        }
    }
    int q;
    cin >> q;
    while (q) {
        q--;
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        cout << arr[r] - arr[l] + 1 << "\n";
    }
}
