#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
	LL n = s.length();
    LL col[n];
    col[0] = 0;
    for(int i = 1; i < n; i += 1){
        if(s[i] < s[i - 1]){
            col[i] = 1;
        }
        else col[i] = 0;
    }
    for(int i = 1; i < n; i += 1){
        col[i] = col[i] + col[i - 1]; 
    }
    LL q;
    cin>>q;
    for(int i = 0; i < q; i += 1){
        LL l, r;
        cin >> l >> r;
        cout<<col[r - 1] - col[l - 1] + 1<<"\n";
    }
}  
