
#include <string>
#include <iostream>
using namespace std;

string inp_str;

char cast(char inp) {
	switch (inp)
	{
	case 'a':
		return 1;
	case 'b':
		return 2;
	case 'c':
		return 3;
	}
}

void calc_all(unsigned int *words_ptr, size_t str_len)
{
	char last_char = -1;
	char cur_char = -1;
	unsigned int itr = 1;

	for (unsigned int i = 0; i < str_len; i++)
	{
		cur_char = cast(inp_str[i]);
		if (last_char != -1 && cur_char < last_char) {
			itr++;
		}
		last_char = cast(inp_str[i]);
		words_ptr[i] = itr;
	}
}

int main() {
	ios_base::sync_with_stdio(0);

	cin >> inp_str;
	size_t str_len = inp_str.length();
	unsigned int *all_words = new unsigned int[str_len];
	calc_all(all_words, str_len);
	
	int n, a, b;
	cin >> n;

	for (unsigned int i = 0; i < n; i++)
	{
		cin >> a;
		cin >> b;
		cout << all_words[b-1] - all_words[a-1] + 1 << endl;
	}
	return 0;
}