#include <bits/stdc++.h>
std::vector A{1};
int n;
int search(int x){
	int a{0}, b{n}, c;
	while(b - a > 1){
		c = (a + b)/2;
		if(A[c] <= x) a = c;
		else b = c;
	}
	return a;
}
int main(){
	int q, a, b, i{1}, x, y;
	char c, d{0};
	std::string s;
	std::cin >> s;
	for(auto c:s){
		if(c < d) A.push_back(i);
		d = c;
		++i;
	}
	n = A.size();
	for(std::cin >> q; q > 0; --q){
		std::cin >> a >> b;
		x = search(a);
		y = search(b);
		std::cout << y - x + 1 << "\n";
	}
}
