#include<bits/stdc++.h>
#define int long long
#define endl "\n";

using namespace std;

signed main() {
	
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	string str;
	cin >> str;
	int len = str.length();
	vector<int> rw;
	rw.push_back(1);
	for(int i = 1; i < len; i++) {
		if(str[i] < str[i-1]) rw.push_back(i+1);
	}

 	int k;
 	cin >> k;
 	
 	for(int i = 0; i < k; i++) {
 		int l, r;
 		cin >> l >> r;
 		int count = 1;
		
 		for(int i = 0; i < rw.size(); i++) {
			 if(rw[i] == l) count--;
			 if(rw[i] >= l && rw[i] <= r) count++;
		 }
		 cout << count << endl;
	 }
	
	return 0;
}