#include <bits/stdc++.h>

using namespace std;

string st;
int q;
int n;
int pref[201010];
map<pair<int, int>, bool> mark;

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;
        pref[i]++;
        mark[{i - 1, i}] = true;
    }
    for (int i = 1; i <= n; i++)
        pref[i] += pref[i - 1];
//    for (int i = 1; i <= n; i++) {
//        cout << pref[i] << " ";
//    }
//    cout << endl;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        cout << pref[r] - pref[l - 1] + 1 - (mark.count({l - 1, l}) != 0) << '\n';
    }
}
