#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <cstring>

using namespace std;
typedef long long ll;


int main(){
    string a;
    cin >> a;
    
    vector<ll>d(a.size());
    
    for(int i = 1; i < a.size(); i++){
        ll c1 = (a[i - 1] - 'a');
        ll c2 = (a[i] - 'a');
        
        if(c2 < c1)
            d[i] = 1;
    }
    
    
    vector<ll>pref(a.size());
    pref[0] = d[0];
    for(int i = 1; i < a.size(); i++)
        pref[i] = pref[i - 1] + d[i];
    
    ll q;
    cin >> q;
    
    while(q--){
        ll l, r;
        cin >> l >> r;
        
        l--, r--;
        
        ll cnt = pref[r];
        if(l != 0)
            cnt -= pref[l - 1];
        
        if(d[l] == 1)
            cnt--;
        
        cout << cnt + 1 << endl;
    }
    
    return 0;
}
