#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	ll n, k=1, ar[20005];
	cin >> s;
	cin >> n;
	char b = s[0];
	ar[0] = k;
	for(int i=1;i<s.size();i++) {
		if(b < s[i] || b == s[i]) {
			b = s[i];
			ar[i] = k;
		}
		else {
			k++;
			b = s[i];
			ar[i] = k;
		}
	}
	for(int l, r, i=0;i<n;i++) {
		cin >> l >> r;
		cout << (ar[r-1] - ar[l-1]) + 1 << '\n';
	}
	return 0;
}