#include <bits/stdc++.h>

using namespace std;

string st;
int q;
int n;
vector<pair<int, int> > hv;

int bnL(int L, int R) {
    int l = -1, r = hv.size();
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (L <= hv[mid].first)r = mid;
        else l = mid;
    }
    if (L <= hv[r].first && hv[r].second <= R)return r;
    return 1;
}
int bnR(int L, int R) {
    int l = -1, r = hv.size();
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (hv[mid].second <= R)l = mid;
        else r = mid;
    }
    if (L <= hv[l].first && hv[l].second <= R)return l;
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> st;
    n = st.size();
    st = "a" + st;
    for (int i = 1; i <= n; i++) {
        if (st[i - 1] <= st[i])continue;
        hv.push_back({i - 1, i});
    }
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        if (r - l == 1){cout << 1 << '\n';continue;}
        cout << bnR(l, r) - bnL(l, r) + 2 << '\n';
    }
}
