#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <unordered_set>

int main()
{
	std::string s;
	std::cin >> s;
	std::vector<int> t;
	t.push_back(1);
	for (size_t i = 0; i < s.length() - 1; i++)
	{
		if (s[i] != s[i + 1] && s[i + 1] - s[i] <= 0)
			t.push_back(i + 2);
	}
	int q;
	std::cin >> q;
	while (q--)
	{
		int l, r;
		std::cin >> l >> r;
		int res = 1;
		for (int i = 0; i < t.size(); i++)
		{
			if (t[i] >= l && t[i] <= r)
				res++;
			if (t[i] == l)
				res--;
		}
		std::cout << res << std::endl;
	}
}
