#include <stdio.h>

int main(void)
{
	char s[20000];
	int q;
	int l, r;
	int aw = 0;

	scanf("%s", &s);
	scanf("%d", &q);
	while (q--) {
		scanf("%d %d", &l, &r);
		aw = 0;
		for (l = l-1; l < r; l++)
			if (aw == 0 || s[l] < s[l-1])
				aw++;

		printf("%d\n", aw);
	}

	return 0;
}
