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

std::vector<int> gen_dict(const std::string &source) {
    std:vector<int> dict(source.length(), 0);

    int a = 1, l = 'a';
    for (int i = 0; i < source.length(); ++i) {
        if (source[i] < l) {
            a++;
        }

        l = source[i];
        dict[i] = a;
    }

    return dict;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	string data; cin >> data;
    const auto dict = gen_dict(data); 

    int c; cin >> c;

    for (int q = 0; q < c; ++q) {
        int i, j; cin >> i >> j;
        std::cout << (dict[j - 1] - (dict[i - 1] - 1)) << '\n';
    }

    return 0;
}
