#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#include <map>
#include <set>
#define MOD 1000000007
#define int long long
#define MAX 1000000000

using namespace std;

vector<int> p;
vector<bool> w;

int get(int a, int b)
{
	if (w[a] && !w[b])
		return p[b] - p[a] + 1;

	if (!w[a] && w[b])
		return p[b] - p[a] + 1;

	if (!w[a] && !w[b])
		return p[b] - p[a] + 1;

	if (w[a] && w[b])
		return p[b] - p[a] + 1;
}

void solve()
{
	string s;
	cin >> s;

	p.resize(s.size() + 1);
	w.resize(s.size() + 1);

	p[0] = 0;
	p[1] = 1;
	w[0] = false;
	w[1] = true;

	for (int i = 2;i < p.size();i++)
	{
		p[i] = p[i - 1];
		if (s[i - 2] > s[i - 1])
		{
			p[i]++;
			w[i] = true;
		}
	}


	
	int q;
	cin >> q;
	for (int i = 0;i < q;i++)
	{
		int a, b;
		cin >> a >> b;
		cout << get(a, b) << endl;
	}
}

signed main()
{
	int t = 1;
	for (int i = 0; i < t; i++)
	{
		solve();
	}
}