#include <string>
#include <iostream>
int main()
{
    std::string source;
    std::cin >> source;

    int count = 0;
    std::cin >> count;
    for (int i = 0; i < count; i++) {
        int l = 0, r = 0;
        std::cin >> l >> r;
        l -= 1;

        std::string split = source.substr(l, r);
        char prevChar = 0;
        int wordCount = 0;
        for (int j = 0; j < r - l; j++) {
            if (split.at(j) < prevChar && split.at(j) != prevChar) {
                wordCount++;
            }
            prevChar = split.at(j);
        }
        std::cout << (wordCount + 1) << std::endl;
    }
    return 0;
}