#include <bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	vector<int>v;
	cin>>s;
	int w=1;
	for(int i=0;i<s.size();++i)
	{
		if(int(s[i])<int(s[i-1]) and i>0)w++;
		v.push_back(w);
		//cout<<w;
	}
	//cout<<v[v.size()-1]-v[0]<<endl;
	//cout<<endl;
	int n,x,y;
	cin>>n;
	for(int i=0;i<n;++i)
	{
		cin>>x>>y;
		cout<<v[y-1]-v[x-1]+1<<endl;
	}
}