#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 2e4;

string s;

struct node {
    int cnt = 0;
    char f, l;
};

node t[N << 2];

node add (node a, node b) {
    if (a.cnt == 0) return b;
    if (b.cnt == 0) return a;
    node c;
    c.cnt = a.cnt + b.cnt;
    c.f = a.f;
    c.l = b.l;
    if (a.l <= b.f)
        --c.cnt ;
    return c;
}

void build (int v, int l, int r) {
    if (l == r) {
        t[v].cnt = 1;
        t[v].f = t[v].l = s[l];
        return ;
    }
    int m = l + r >> 1;
    build (v + v, l, m);
    build (v + v + 1, m + 1, r);
    t[v] = add (t[v + v], t[v + v + 1]);
    return ;
}

node ask (int v, int tl, int tr, int l, int r) {
    if (l > r) {
        node c;
        return c;
    }
    if (tl == l && tr == r) return t[v];
    int m = tl + tr >> 1;
    return add (ask (v + v, tl, m, l, min (m, r)), ask (v + v + 1, m + 1, tr, max (m + 1, l), r));
}

int main()
{
    ios_base::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> s;
    int q, n = s.size ();
    build (1, 0, n - 1);
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r; --l, --r;
        cout << ask (1, 0, n - 1, l, r).cnt << "\n";
    }
    return 0;
}
