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

int main(){
    string s;
    cin >> s;
    int n = s.length();
    int q;
    cin >> q;
    int l0[n];
    int r0[n];
    int m = 1;
    for (int i = 0; i < (n - 1); i++){
        if (s[i] > s[(i + 1)]){
            l0[i] = m - 1;
            m++;
        }
        else{
            l0[i] = m - 1;
        }
    }
    l0[(n - 1)] = m - 1;
    for (int i = 0; i < n; i++){
        r0[i] = l0[(n - 1)] - l0[i];
    }
    for (int p = 0; p < q; p++){
        int l, r;
        cin >> l >> r;
        l--; r--;
        int ans = m - l0[l] - r0[r];
        cout << ans << endl;
    }
}
