#include <cstdio>
#include <vector>
#include <algorithm>

const int N = 20000;
char s[N + 1];
int q;

int main() {
	scanf("%s", s);
	std::vector<int> p;
	for (int i = 0; s[i];) {
		p.push_back(i);
		do {
			++i;
		} while (s[i] >= s[i - 1]);
	}
	scanf("%d", &q);
	while (q--) {
		int l, r;
		scanf("%d%d", &l, &r);
		--l;
		printf("%d\n", std::lower_bound(p.begin(), p.end(), r) - std::upper_bound(p.begin(), p.end(), l) + 1);
	}
	return 0;
}