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