#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main() {
	vector<double>arr;
	string s;
	cin >> s;
	int n;
	cin >> n;
	for (int i = 1; i < s.size(); i++) {
		if (int(s[i - 1]) > int(s[i])) {
			arr.push_back(i + 0.5);
		}
	}
	for (int i = 1; i <= n; i++) {
		int a, b, kol = 1, j = 0;
		cin>>a>>b;
		while (j < b) {
			if (arr[j] > a && arr[j] < b) {
				kol++;
			}
			j++;
		}
		cout << kol<<endl;
	}
}
