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

const int maxx = 2e4;
int a[maxx] = {}, t[4 * maxx] = {};

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        build(v * 2, tl, (tl + tr) / 2);
        build(v * 2 + 1, (tl + tr) / 2 + 1, tr);
        t[v] = t[v * 2] + t[v * 2 + 1];
    }
}

int sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr)
        return t[v];
    return sum(v * 2, tl, (tl + tr) / 2, l, min((tl + tr) / 2, r)) + sum(v * 2 + 1, (tl + tr) / 2 + 1, tr, max((tl + tr) / 2 + 1, l), r);
}

int main() {
    string s;
    cin >> s;
    map <int, bool> ind;
    for (int i = 1; i < s.size(); i++) {
        if (s[i] < s[i - 1])
            a[i - 1] = 1, ind[i - 1] = 1;
    }
    build(1, 0, s.size() - 1);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        cout << ((a != b) ? sum(1, 0, s.size() - 1, a, b) + 1 - ind[a] - ind[b] : 1) << endl;
    }
    return 0;
}
//abc|aabb|acccc|b