
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int pref[20007];
inline void solve(){
    int n;
    string s;
    cin >> s;
    n = s.size();
    for (int i = 0; i < n-1; ++i) {
        if(i) pref[i] = pref[i-1];
        if(s[i] > s[i+1]) pref[i]++;
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int L,R;
        cin >> L >> R;
        L--;
        R--;
        cout << pref[R-1]+1-((L)?pref[L-1]:0) << "\n";
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
    return 0;
}
