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

using namespace std;
typedef long long ll;
const int VERYLONGINT = 1000000001;

struct tf {
    vector<int> tree;
    int size;
    tf(int n) {
        size = n + 1;
        tree.assign(n + 1, 0);
    }
    void add(int p, int v) {
        for (int i = p + 1; i < size; i += (i & (-i)))
            tree[i] += v;
    }

};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    cin >> s;
    int q;
    cin >> q;
    vector<int> ps(s.size());
    for (int i = 1; i < s.size(); i++) {
        ps[i] = ps[i - 1];
        if (s[i] < s[i - 1])
            ps[i]++;
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        cout << (1 + ps[r - 1] - ps[l - 1]) << "\n";
    }
    return 0;
}