#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <numeric>
#include <map>
#include <cassert>
#include <algorithm>

using namespace std;

#define int long long

signed main()
{
	string s;
	cin >> s;
	int n = s.size();
	vector<int> a(n);
	for (int i = 1; i < n; ++i)
	{
		if (s[i] < s[i - 1])
		{
			a[i]++;
			//cout << "x" << '\n';
		}
	}
	vector<int> pref(n + 1);
	for (int i = 1; i <= n; ++i)
	{
		pref[i] = pref[i - 1] + a[i - 1];
		//cout << pref[i] << '\n';
	}
	int q, c1, c2;
	cin >> q;
	for (int i = 0; i < q; ++i)
	{
		cin >> c1 >> c2;
		--c1;
		--c2;
		cout << 1 + pref[c2 + 1] - pref[c1 + 1] << '\n';
	}
	return 0;
}
