
#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<map<int, int>> partitions;

    partitions.push_back({});

    partitions[0][0] = 0;
    partitions[0][1] = 0;

    int m = 0;

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

    int q;
    cin >> q;

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

        int n = 0;

        for (map<int, int> j : partitions) {
            if (s <= j[1] && e >= j[0]) {
                n++;
            }
            else if (j[0] > e) {
                break;
            }
        }

        printf("%i\n", n);
    }
    
    
}
