#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second

typedef long long ll;
const ll INF = 100000000007, DIM = 200007, MAXN = 100000;

ll tt, a[DIM], b[DIM], t[DIM], n,m,res, c, l, r;
string s;

ll pref(ll x) {
	x++;
	ll res=0;
	for(int i=x;i>0;i-=i&-i)
		res+=t[i];
	return res;
}

void add(ll pos, ll x) {
	pos++;
	for(int i=pos;i<=MAXN;i+=i&-i)
		t[i]+=x;
}

ll sum(ll l, ll r) {
	if(l>r)return 0;
	if(l==1) return pref(r);
	return pref(r)-pref(l-1);
}

void solve(){
	cin>>s;n=s.size();s="&"+s;
	cin>>m;
	for(int i=2;i<=n;++i){
		b[i]=(s[i]<s[i-1]);
		add(i,b[i]);
	}
	for(int i=1;i<=m;++i){
		cin>>l>>r;
		cout<<sum(l+1,r)+1<<'\n';
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	tt=1;
	//cin>>tt;
	while(tt--)
		solve();
	return 0;
}