
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <stdio.h>
#include <limits>

using namespace std;

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

    
    string str;
    cin >> str;

    vector<int> partitions;


    partitions.push_back(0);
    int m = 1;

    for (int i = 1; i < str.length(); i++) {
        if (str[i] - 'a' < str[i - 1] - 'a') {
            partitions.push_back(i);
            m++;
        }
    }

    int q;
    cin >> q;

    while (q--) {
        int s, e;
        cin >> s >> e;
        s--;
        e--;


        int firstLessThanS;
        int firstLessThanE;

        int begin = 0;
        int end = m - 1;
        while (begin <= end) {
            int mid = (begin + end) / 2;
            if (partitions[mid] < s) {
                firstLessThanS = mid;
                begin = mid + 1;
            }
            else if (partitions[mid] > s) {
                end = mid - 1;
            }
            else {
                firstLessThanS = mid;
                break;
            }
        }

        begin = 0;
        end = m - 1;
        while (begin <= end) {
            int mid = (begin + end) / 2;
            if (partitions[mid] < e) {
                firstLessThanE = mid;
                begin = mid + 1;
            }
            else if (partitions[mid] > e) {
                end = mid - 1;
            }
            else {
                firstLessThanE = mid;
                break;
            }
        }

        printf("%i\n", firstLessThanE - firstLessThanS + 1);

    }
    
    
}
