#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long

using namespace std;
string s;
ll l,r,n,k;
vector <ll> v;
int main(){
   //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);
    cin >> s;
    	for(int i = 1;i < s.size();i++){
    		if(s[i] < s[i - 1])v.push_back(i);
	}
	cin >> n;
	while(n--){
		k = 1;
		cin >> l >> r;
	for(int i = 0;i < v.size();i++){
		if(v[i] >= l && v[i] < r)k++;
		//cout << v[i] << ' ';
	}
cout << k << endl;
}
  return 0;
}

