	#include <algorithm>
	#include <iostream>
	#include <cstdlib>
	#include <cstdarg>
	#include <cassert>
	#include <climits>
	#include <cstring>
	#include <complex>
	#include <cstdio>
	#include <vector>
	#include <string>
	#include <queue>
	#include <cmath>
	#include <ctime>
	#include <set>
	#include <map>
	#include <iomanip>
	#include <unordered_map>
	#include <bitset>
	#include<stack>
	#include <type_traits>
	#include <random>
	#include <chrono>
	#define int long long
	using namespace std;
	long long a, b, c, d, e, f, g;
	long long h[1000005];
	void solve()
	{
		string s;
		cin >> s;
		for (int i = 1; i < s.size(); i++)
		{
			if (s[i] < s[i - 1]) { h[i - 1]++; }
			h[i] = h[i - 1];
		}
		
		cin >> a;
		for (int i = 1; i <= a; i++)
		{
			int l, r;
			cin >> l >> r;
			l--;
			r--;
			if (l == 0) cout << h[r - 1] + 1;
			else cout << h[r - 1] - h[l - 1]+1;
			cout << '\n';
		}
	}
	/*
	6
	3 3 3 3 4 5
	*/
	signed main() {
		int q = 1;
		//cin >> q;
		while (q != 0)
		{
			solve();
			q--;
		}
	}
