#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 2e5+7;
int pref[N];
void solve(){
    string s;
    cin>>s;
    int n = s.length();
    for(int i = 1;i<n;i+=1){
        pref[i] = pref[i-1] + (s[i]<s[i-1]);
    }
    int q;
    cin>>q;
    for(int test = 1;test<=q;test+=1){
        int l,r;
        cin>>l>>r;
        cout<<pref[r-1]-pref[l-1]+1<<endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
