#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define PII pair<int,int>
#define f first
#define s second
#define PB push_back
#define MIN(a,b) a=min(a,b)
#define MAX(a,b) a=max(a,b)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define V vector
#define PDI pair<double, int> 
#define Pll pair<ll,ll>
const int N=2e3;
const ll mod=1e9+7;
const double eps=1e-9;
const double PI=acos(-1);
#define INF 2000000000

int main(){
	string s;
	cin>>s;
	int n=s.size();
	V<int> ps(n+1);
	for(int i=0;i<n;i++)
		ps[i+1]=ps[i]+(i==0||s[i]<s[i-1]);
	int q;
	cin>>q;
	while(q--){
		int l,r;
		cin>>l>>r;
		cout<<ps[r]-ps[l]+1<<"\n";
	}
}