#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> pll;
const ll MAXNUM=100007;

ll n, m, q, k, cuts[MAXNUM], ans, l, r;
str s;
int main()
{
    cin >> s;
    for(int u=1; u<s.size(); u++){
        if(s[u]<s[u-1]) cuts[u]=cuts[u-1]+1;
        else cuts[u]=cuts[u-1];
    }
    cin >> q;
    for(int i=1; i<=q; i++){
        scanf("%lld %lld", &l, &r);
        ans=cuts[r-1]-cuts[l-1]+1;
        printf("%lld\n", ans);
    }

    return 0;
}
