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

using namespace std;

int binfind(int l, vector<int> &rw) {
	int lx = -1, rx = rw.size();
	while(rx != lx+1) {
		int m = (rx+lx)/2;
		if(rw[m] < l) lx = m;
		else if(rw[m] == l) return m;
		else rx = m;
	}
	return rx;
}

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 = 0;
		int j = binfind(l, rw);
		if(rw[j] != l) count = 1;
 		for(j; j < rw.size(); j++) {
			 if(rw[j] <= r) count++;
			 else break;
		 }
		 cout << count << endl;
	 }
	
	return 0;
}